GameClass
A script class that defines the game mode. Only one instance of this class is made.
This is the first script that will be run.
The game script is responsible for creating and managing worlds.
Can receive events sent with sm.event.sendToGame.
Fields:
Type | Name | Description |
---|---|---|
Network | network | A Network object that can be used to send messages between client and server. |
Storage | storage | (Server side only.) A Storage object that can be used to store data for the next time loading this object after being unloaded. |
any | data | Game start data. |
Constants:
- defaultInventorySize
- enableAggro
- enableAmmoConsumption
- enableFuelConsumption
- enableLimitedInventory
- enableRestrictions
- enableUpgrade
Common callbacks:
- server_onCreate
- client_onCreate
- server_onDestroy
- client_onDestroy
- server_onRefresh
- client_onRefresh
- server_onFixedUpdate
- client_onFixedUpdate
- client_onUpdate
- client_onClientDataUpdate
Callbacks:
- server_onPlayerJoined
- server_onPlayerLeft
- server_onReset
- server_onRestart
- server_onSaveLevel
- server_onTestLevel
- server_onStopTest
- client_onLoadingScreenLifted
- client_onLanguageChange
defaultInventorySize integer
Sets default player inventory size. (Defaults to 40)
enableAggro boolean
Enables or disables enemy aggression. (Defaults to true)
enableAmmoConsumption boolean
Enables or disables ammo consumption. (Defaults to false)
enableFuelConsumption boolean
Enables or disables fuel consumption. (Defaults to false)
enableLimitedInventory boolean
Enables or disables limited inventory. (Defaults to false)
When limited in inventory is on, items have a limited amount. When off, the player has access to all items. (Except for items with json value "hidden": true)
enableRestrictions boolean
Enables or disables build restrictions. (Defaults to false)
enableUpgrade boolean
Enables or disables interactable part upgrade. (Defaults to false)
server_onCreate(self) serverEventCallback
Called when the scripted object is created. This occurs when a new object is built, spawned, or loaded from the save file.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
client_onCreate(self) clientEventCallback
Called when the scripted object is created. This occurs when a new object is built, spawned, or loaded from the save file.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
server_onDestroy(self) serverEventCallback
Called when the scripted object is destroyed.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
client_onDestroy(self) clientEventCallback
Called when the scripted object is destroyed.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
server_onRefresh(self) serverEventCallback
Called if the Lua script attached to the object is modified while the game is running.
Note:
This event requires Scrap Mechanic to be running with the '-dev' flag. This will allow scripts to automatically refresh upon changes.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
client_onRefresh(self) clientEventCallback
Called if the Lua script attached to the object is modified while the game is running.
Note:
This event requires Scrap Mechanic to be running with the '-dev' flag. This will allow scripts to automatically refresh upon changes.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
server_onFixedUpdate(self, timeStep) serverEventCallback
Called every game tick – 40 ticks a second. If the frame rate is lower than 40 fps, this event may be called twice.
During a fixed update, physics and logic between interactables are updated.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
number | timeStep | The time period of a tick. (Is always 0.025, a 1/40th of a second.) |
client_onFixedUpdate(self, timeStep) clientEventCallback
Called every game tick – 40 ticks a second. If the frame rate is lower than 40 fps, this event may be called twice.
During a fixed update, physics and logic between interactables are updated.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
number | timeStep | The time period of a tick. (Is always 0.025, a 1/40th of a second.) |
client_onUpdate(self, deltaTime) clientEventCallback
Called every frame.
During a frame update, graphics, animations and effects are updated.
Warning:
Because of how frequent this event is called, the game's frame rate is greatly affected by the amount of code executed here.
For any non-graphics related code, consider using client_onFixedUpdate instead.
If the event is not in use, consider removing it from the script. (Event callbacks that are not implemented will not be called.)
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
number | deltaTime | Delta time since the last frame. |
client_onClientDataUpdate(self, data, channel) clientEventCallback
Called when the client receives new client data updates from the server set with Network.setClientData.
Data set in this way is persistent and the latest data will automatically be sent to new clients.
The data will arrive after client_onCreate during the same tick.
Channel 1 will be received before channel 2 if both are updated.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
any | data | Any lua object set with Network.setClientData |
integer | channel | Client data channel, 1 or 2. (default: 1) |
server_onPlayerJoined(self, player, newPlayer) serverEventCallback
Called when a Player joins the game.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Player | player | The joining player. |
boolean | newPlayer | True if the player has not been in this game before. |
server_onPlayerLeft(self, player) serverEventCallback
Called when a Player leaves the game.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Player | player | The leaving player. |
server_onReset(self) serverEventCallback
Challenge Mode only!
Called when the user wants to reset the challenge level.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
server_onRestart(self) serverEventCallback
Challenge Mode only!
Called when the user wants to restart the challenge level.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
server_onSaveLevel(self) serverEventCallback
Challenge Builder only!
Called when the user wants to save the challenge level.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
server_onTestLevel(self) serverEventCallback
Challenge Builder only!
Called when the user wants to save and test the challenge level.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
server_onStopTest(self) serverEventCallback
Challenge Builder only!
Called when the user wants to stop testing the challenge level.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
client_onLoadingScreenLifted(self) clientEventCallback
Called when the loading screen is lifted when entering a game.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
client_onLanguageChange(self, language) clientEventCallback
Called when the user changes language in the in-game menus.
Possible language values:
"Brazilian", "Chinese", "English", "French", "German", "Italian", "Japanese", "Korean", "Polish", "Russian", "Spanish"
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
string | language | The new language. |