In this tutorial, we will see how to write your first package for Onset. A package (view more on packages) is a collection of scripts (written in LUA) and assets files (HTML, JS, CSS). He has its own LuaVM state. A mechanism is available to allow packages to communicate between them.
Scripts allow you to personalize the game and can be executed on the server-side and/or the client-side. Assets files are used to build your game interfaces quickly.
How to create a package?
Let's go to create a simple package. First thing to know, a package this own directory in onset/packages
.
He must have a package.json
file that contains its description and the list of files that need to be loaded. This file will be read by Onset when the server starts (if we have enabled our package).
- Create a new folder; he will contain our new package. For example,
onset/packages/helloworld
.
Note: the name of the folder is the package name. - Create a
package.json
file inonset/packages/helloworld
. - The content of this file needs to be a valid JSON:
{ "author": "YourName", "version": "1.0", "server_scripts": [], "client_scripts": [], "files": [] }
- Currently, the package is not loaded on the server.
To enable the package, editonset/server_config.json
(see server config) and add the package name (folder name inonset/packages
) inpackages
section:"packages": [ ..., "helloworld" ],
- Reboot your Onset server. In server console, you will see:
[ERROR] Package "helloworld" does not have any scripts [ERROR] Package "helloworld" failed loading
The server try to load our package. Currently, our package is empty!
Writing your first Onset script
In a package, you can structure your files like you won't, but we strongly recommend (for maintainability) to have:
- A
client
folder for client-side scripts. - A
server
folder for server-side scripts. - A
assets
folder for the html, js, css and other files used to build your game interfaces.
When the server successfully loads the package, we will display a message in the server console. It will contain the name of the server.
Onset exposes an API which allow you to access native functions of the game as well as an event system making hook easy.
For our example, on our Onset server, we will hook OnPackageStart
event, and use internal GetServerName()
function to get the server name. This script will be executed on server-side:
- Create a
onset/packages/helloworld/server
folder. - In this folder, add
server.lua
file with the following content:function OnPackageStart() print("Hello World by mTxServ.com!") print(("Name of the Onset server: `%s`\n"):format(GetServerName())) end AddEvent("OnPackageStart", OnPackageStart)
Our first script is ready. - Now we need to add it in the
package.json
, on theserver_scripts
section:{ "author": "mTxServ", "version": "1.0", "server_scripts": [ "server/server.lua" ], "client_scripts": [], "files": [] }
Save it. - To try it, reboot your server. In server console, you will see:
[INFO] Starting package "helloworld" [SCRIPT] Hello World by mTxServ.com! [SCRIPT] Name of the Onset server: `mTxServ.com`
- Let's create a script that will announce the connections/disconnections of the players and add it in
server.lua
:function OnPlayerJoin(player) AddPlayerChatAll(GetPlayerName(player).." connected") end AddEvent("OnPlayerJoin", OnPlayerJoin) function OnPlayerQuit(player) AddPlayerChatAll(GetPlayerName(player).." disconnected") end AddEvent("OnPlayerQuit", OnPlayerQuit)
- Restart your server and try it ;)
- We have finished our first package!
Learn more about events and functions on official wiki.
You can see our whitelist package or our translation (i18n) package if you want to learn more.
Limitations of a package
- Package only support files with the following extensions:
lua, js, css, html, htm, png, jpg, jpeg, gif, wav, mp3, ogg, oga, flac, woff2, ttf, pak
- You can only load 32 packages on your server.
- You can only have 255 files per package.
Official Documentation
Many features are available (events, functions, etc.), allowing you to modify the game easily. Consult the official documentation!