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
| Name | Type | 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. |
data |
any | Game start data. |
Constants
| Name | Type | Description |
|---|---|---|
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) |
enableRecipes |
— | Enables or disables recipes being locked and needing to be learned to build items. (Defaults to true) |
enableRestrictions |
boolean | Enables or disables build restrictions. (Defaults to false) |
enableUpgrade |
boolean | Enables or disables interactable part upgrade. (Defaults to false) |
enableLimitedInventory
Value type: 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)
Server + Client
onCreate
GameClass:server_onCreate( )
GameClass:client_onCreate( )
Called when the scripted object is created. This occurs when a new object is built, spawned, or loaded from the save file.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onDestroy
GameClass:server_onDestroy( )
GameClass:client_onDestroy( )
Called when the scripted object is destroyed.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onRefresh
GameClass:server_onRefresh( )
GameClass:client_onRefresh( )
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:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onFixedUpdate
GameClass:server_onFixedUpdate( timeStep )
GameClass:client_onFixedUpdate( timeStep )
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:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
timeStep |
number | The time period of a tick. (Is always 0.025, a 1/40th of a second.) |
Server-only
onReceiveUpdate
GameClass:server_onReceiveUpdate( )
Called occasionally to indicate that some time has passed.
For performance reasons; it recommended to use this instead of server_onFixedUpdate for updates that do not need to happen frequently.
Use sm.game.getCurrentTick to calculate the time.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onPlayerJoined
GameClass:server_onPlayerJoined( player, newPlayer )
Called when a Player joins the game.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
player |
Player | The joining player. |
newPlayer |
boolean | True if the player has not been in this game before. |
onPlayerLeft
GameClass:server_onPlayerLeft( player )
Called when a Player leaves the game.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
player |
Player | The leaving player. |
onReset
GameClass:server_onReset( )
Challenge Mode only!
Called when the user wants to reset the challenge level.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onRestart
GameClass:server_onRestart( )
Challenge Mode only!
Called when the user wants to restart the challenge level.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onSaveLevel
GameClass:server_onSaveLevel( )
Challenge Builder only!
Called when the user wants to save the challenge level.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onTestLevel
GameClass:server_onTestLevel( )
Challenge Builder only!
Called when the user wants to save and test the challenge level.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onStopTest
GameClass:server_onStopTest( )
Challenge Builder only!
Called when the user wants to stop testing the challenge level.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onUnload
GameClass:server_onUnload( )
Called when a game session ends.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
Client-only
onUpdate
GameClass:client_onUpdate( deltaTime )
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:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
deltaTime |
number | Delta time since the last frame. |
onClientDataUpdate
GameClass:client_onClientDataUpdate( data, channel )
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:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
data |
any | Any lua object set with Network.setClientData |
channel |
integer | Client data channel, 1 or 2. (default: 1) |
onLocalPlayerChangedWorld
GameClass:client_onLocalPlayerChangedWorld( world )
Called when the client player changes world.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
world |
World | The entered world. |
onLoadingScreenLifted
GameClass:client_onLoadingScreenLifted( )
Called when the loading screen is lifted when entering a game.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
onLanguageChange
GameClass:client_onLanguageChange( language )
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:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |
language |
string | The new language. |
onUnstuck
GameClass:client_onUnstuck( )
Called when the user presses unstuck in the in-game main menu.
Parameters:
| Name | Type | Description |
|---|---|---|
self |
table | The class instance. |