CharacterClass

A script class that is instanced for every Character in the game.

A Character is a temporary vessel controlled by a Player or Unit.

Can receive events sent with sm.event.sendToCharacter.

Fields:

TypeNameDescription
CharactercharacterThe Character game object belonging to this class instance.
NetworknetworkA Network object that can be used to send messages between client and server.
anydataData from the "data" json element.

Common callbacks:

Callbacks:


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:

TypeNameDescription
tableselfThe 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:

TypeNameDescription
tableselfThe class instance.

server_onDestroy(self) serverEventCallback

Called when the scripted object is destroyed.

Parameters:

TypeNameDescription
tableselfThe class instance.

client_onDestroy(self) clientEventCallback

Called when the scripted object is destroyed.

Parameters:

TypeNameDescription
tableselfThe 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:

TypeNameDescription
tableselfThe 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:

TypeNameDescription
tableselfThe 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:

TypeNameDescription
tableselfThe class instance.
numbertimeStepThe 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:

TypeNameDescription
tableselfThe class instance.
numbertimeStepThe 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:

TypeNameDescription
tableselfThe class instance.
numberdeltaTimeDelta 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:

TypeNameDescription
tableselfThe class instance.
anydataAny lua object set with Network.setClientData
integerchannelClient data channel, 1 or 2. (default: 1)

client_onProjectile(self, position, airTime, velocity, projectileName, shooter, damage, customData, normal, uuid) clientEventCallback

Called when the Character is hit by a projectile.

Note:

If the shooter is destroyed before the hit lands, the shooter value will be nil.

Parameters:

TypeNameDescription
tableselfThe class instance.
Vec3positionThe position in world space where the projectile hit the Character.
numberairTimeThe time, in seconds, that the projectile spent flying before the hit.
Vec3velocityThe velocity of the projectile at impact.
stringprojectileNameThe name of the projectile. (Legacy, use uuid instead)
Player/Shape/Harvestable/nilshooterThe shooter, can be a Player, Shape, Harvestable or nil if unknown. Projectiles shot by a Unit will be nil on the client.
numberdamageThe damage value of the projectile.
anycustomDataA Lua object that can be defined at shoot time using sm.projectile.customProjectileAttack or an other custom version.
Vec3normalThe normal at the point of impact.
UuiduuidThe uuid of the projectile.

client_onMelee(self, position, attacker, damage, power, direction, normal) clientEventCallback

Called when the Character is hit by a melee hit.

Parameters:

TypeNameDescription
tableselfThe class instance.
Vec3positionThe position in world space where the Character was hit.
Player/nilattackerThe attacker. Can be a Player or nil if unknown. Attacks made by a Unit will be nil on the client.
integerdamageThe damage value of the melee hit.
numberpowerThe physical impact impact of the hit.
Vec3directionThe direction that the melee attack was made.
Vec3normalThe normal at the point of impact.

client_onCollision(self, other, position, selfPointVelocity, otherPointVelocity, normal) clientEventCallback

Called when the Character collides with another object.

Parameters:

TypeNameDescription
tableselfThe class instance.
Shape/Character/Harvestable/Lift/nilotherThe other object. Nil if terrain.
Vec3positionThe position in world space where the collision occurred.
Vec3selfPointVelocityThe velocity that that the Character had at the point of collision.
Vec3otherPointVelocityThe velocity that that the other object had at the point of collision.
Vec3normalThe collision normal between the Character and the other other object.

client_onGraphicsLoaded(self) clientEventCallback

Called when graphics are loaded for the Character.

After this; graphics related functions can be called, like accessing animations.

Parameters:

TypeNameDescription
tableselfThe class instance.

client_onGraphicsUnloaded(self) clientEventCallback

Called when graphics are unloaded for the Character.

After this; graphics related functions no longer has an effect or will fail.

Parameters:

TypeNameDescription
tableselfThe class instance.

client_onInteract(self, character, state) clientEventCallback

Called when a Player is interacting with the Character by pressing the 'Use' key (default 'E').

Parameters:

TypeNameDescription
tableselfThe class instance.
CharactercharacterThe Character of the Player that is interacting with this Character.
booleanstateThe interaction state. Always true. The CharacterClass only receives the key down event.

client_canInteract(self, character) clientEventCallback

Called to check whether the Character can be interacted with at this moment.

Note:

This callback is also responsible for updating interaction text shown to the player using sm.gui.setInteractionText.

Parameters:

TypeNameDescription
tableselfThe class instance.
CharactercharacterThe Character of the Player that is looking at this Character.

Returns:

TypeDescription
booleanA boolean indicating whether the characer can be interacted with or not. (Defaults to true if client_onInteract is implemented, otherwise false)

client_onEvent(self, event) clientEventCallback

Called when the Character receives an event from Player.sendCharacterEvent or Unit.sendCharacterEvent.

This is usually for triggering animations on the character.

For more extensive events, see sm.event.sendToCharacter.

Parameters:

TypeNameDescription
tableselfThe class instance.
stringeventThe event name.