Oakwood
  • Introduction
  • Server Setup
  • Public API
    • Events
    • Player
    • Vehicle
    • HUD
    • Camera
    • Chat
    • Vehicle-Player
    • Miscellaneous
  • Languages
    • NodeJS
  • Server API
  • Assets
    • Asset: Vehicle Models
    • Asset: Player models
  • Tutorials
    • Build Your Own Language Support
      • 1. Overview
      • 2. Connection
      • 3. Native Calls
      • 4. Events
      • 5. Wrap-up
Powered by GitBook
On this page
  • Set up your NodeJS environment
  • Things to note

Was this helpful?

  1. Languages

NodeJS

This page describes how to use the NodeJS package.

PreviousLanguagesNextServer API

Last updated 5 years ago

Was this helpful?

Set up your NodeJS environment

This small guide assumes you're already familiar with NodeJS environment.

The first step is to install the package via npm.

Afterwards, we need to create our resource script we plan to start in node:

const {createClient} = require('oakwood')
const oak = createClient()

oak.event('start', async () => {
    console.log("[info] connected to the server")
    oak.log("[info] hello world from nodejs")
})

oak.event('playerConnect', async pid => {
    console.log('[info] player connected', pid)

    oak.playerPositionSet(pid, [-1774.59301758, -4.88487052917, -2.40491962433])
    oak.playerHealthSet(pid, 200)
    oak.playerSpawn(pid)
})

oak.cmd('goto', async (pid, targetid) => {
    const tid = parseInt(targetid)

    if (tid === NaN) {
        return oak.chatSend(pid, `[error] provided argument should be a valid number`)
    }

    if (await oak.playerInvalid(tid)) {
        return oak.chatSend(pid, `[error] player you provided was not found`)
    }

    /* get target position */
    const pos = await oak.playerPositionGet(tid)

    /* set our player position */
    oak.playerPositionSet(pid, pos)
})

Things to note

All public API calls are asynchronous due to the nature of the communication. However, you can await the response of the call if you expect a return value.

You can look up the to see how to use the package.

Check out this primer on how to work with such environment:

oakwood
example code
Medium: Javascript Async/Await and Promises