Skip to main content

Common Fields and Callbacks

The callback functions listed below are available in every ScriptClass, in addition
to that ScriptClass's own fields and callbacks.

Server + Client

onCreate

function Class.server_onCreate( self )
end
function Class.client_onCreate( self )
end

Called when the scripted object is created.
This occurs when a new object is built, spawned, or loaded from the save file.

Arguments:
  • self [ table ]: The class instance.

onDestroy

function Class.server_onDestroy( self )
end
function Class.client_onDestroy( self )
end

Called when the scripted object is destroyed.
This occurs when an object is erased, destroyed by an explosion, robot attack, etc.

Arguments:
  • self [ table ]: The class instance.

onRefresh

function Class.server_onRefresh( self )
end
function Class.client_onRefresh( self )
end

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 launch option.
This will allow scripts to automatically refresh upon changes.

Arguments:
  • self [ table ]: The class instance.

onFixedUpdate

function Class.server_onFixedUpdate( self, timeStep )
end
function Class.client_onFixedUpdate( self, timeStep )
end

Called every game tick - 40 times per 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.

Arguments:
  • self [ table ]: The class instance.
  • timeStep [ number ]: The time period of a tick. (Always 0.025, a 1/40th of a second.)

Client-only

onUpdate

function Class.client_onUpdate( self, deltaTime )
end

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).

Arguments:
  • self [ table ]: The class instance.
  • deltaTime [ number ]: Delta time since the last frame.

onClientDataUpdate

function Class.client_onClientDataUpdate( self, data, channel )
end

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.

Arguments:
  • self [ table ]: The class instance.
  • data [ any ]: Any lua object set with Network:setClientData.
  • channel [ int ]: Client data channel, 1 or 2. (default: 1)