Example package: hello
hello is the smallest useful package. It has a server part and a client part and shows how they talk to each other. It does four things:
- greets every player when their client is ready;
- answers the chat command
/hello, typed in the built-in chat, with a greeting on the screen and a line in the chat; - sends the same greeting when the player presses H (an extra: the client script asks the server for it with an event);
- puts a marker and a blip five metres from the first player who becomes ready.
The example is the folder examples/hello of the server sources. Its layout matches the server folder you run, so you copy it over:
hello/
node/packages/hello/index.js server part
client_packages/index.js the entry of the client scripts: loads ./hello
client_packages/hello/index.js client part
client_packages/hello/ui/hello.html the page that shows the greeting on the screen
Install
- Stop the server if it is running.
- Copy the contents of the
hellofolder into your server folder:- Windows archive: copy
node\packages\hellointo the archive'snode\packagesfolder, andclient_packagesinto the archive'sclient_packagesfolder (merge the folders; if you already have aclient_packages\index.js, add the linerequire('./hello');to it instead of replacing the file). - Linux container: copy
node/packages/hellointo thepackagesfolder andclient_packagesinto theclient_packagesfolder next tocompose.yml.
- Windows archive: copy
- Start the server (see Server configuration). The log shows
loading package hello, then[hello] loaded.
The resources.watch setting rebuilds the client files by itself when they change, but a server script is read only at start: restart the server after you change node/packages/hello/index.js.
Run
- In the launcher use "Connect by address" and enter your server's address (
127.0.0.1:31130on the same computer). - When you are in the game, the greeting
Hello, <your name>!appears at the top of the screen for a few seconds. - Press H: the greeting appears again. The server log shows
[hello] greeted <name>. - Press T, type
/helloand press Enter: the same handler runs, the greeting appears on the screen and aHello, <your name>!line appears in the chat. The built-in chat sends a line that starts with/as a command. See Events.
The marker and the blip are created on the server once, five metres from the first player who becomes ready (the server waits for that player's first real position, at most 10 seconds). The marker is streamed to players who come within the server's streamingRange; the blip is sent to every player.
How it works
- On
playerReadythe server package callsplayer.spawn(position, heading)to put the player on an open street: the server decides where a player appears, as in RAGE MP (the game's own save may start a player inside a house). The marker goes five metres from that point. - The server package registers
mp.events.addCommand('hello', ...)for the command (it answers withplayer.outputChatBox) andmp.events.add('hello:request', ...)for the event the client sends. - To greet, the server calls
player.call('hello:greet', text). The client script receives it withmp.events.add('hello:greet', (text) => {})and passes the text to a browser page withbox.call('show', text). - The client script binds the H key with
mp.keys.bind(0x48, false, ...)and sendsmp.events.callRemote('hello:request'). - The marker and the blip come from
mp.markers.newandmp.blips.new, with the named VI colourmp.blips.colours.cyan. - The page is
package://hello/ui/hello.html, that is the fileclient_packages/hello/ui/hello.html, which players download when they join (Resources and packages).
Each step above was checked in the game: the greeting when the client is ready, the H key, the /hello command, the page loading from package://hello/ui/hello.html, the marker next to the player and the blip. For how the game shows each kind of marker and blip, see the member in the API reference.
Change the files, restart the server, join again, and players receive the changed client files.
A game mode: freeroam
freeroam is a small but complete game mode to start your own from. It is server-only: the player sees it through the built-in chat and the map, so there is nothing to put into client_packages.
- A player whose game has loaded appears at the spawn point and gets a welcome line in the chat; everyone sees who joins, who leaves and who was killed.
- Chat commands:
/help,/veh [name](a vehicle in front of you; your previous one goes away),/tp <x> <y> <z>,/tp <player id or name>,/tp spawn,/weapon <hash> [ammo](a weapon by the game's own hash: the example ships no list of weapons),/hp,/pos. - Damage: a hit a player's game reports is a request; the mode checks it (the same dimension, the distance, a sane amount) and applies it, armour first. A killed player appears at the spawn point again after a few seconds.
- Blips at the points listed in its
config.json, which also holds the spawn point, the vehicle names, the welcome line and the limits.
It is the folder examples/freeroam; copy its node/packages/freeroam into your server's node/packages (the Windows archive) or packages (the Linux container) and restart the server. Its README.md lists every command and setting. The vehicle names in its config.json are GTA VI vehicles; add a name only after you have seen it appear in the game.