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

Name Type Description
character Character The Character game object belonging to this class instance.
network Network A Network object that can be used to send messages between client and server.
data any Data from the "data" json element.

Server + Client

onCreate

CharacterClass:server_onCreate(  )
CharacterClass: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

CharacterClass:server_onDestroy(  )
CharacterClass:client_onDestroy(  )

Called when the scripted object is destroyed.

Parameters:

Name Type Description
self table The class instance.

onRefresh

CharacterClass:server_onRefresh(  )
CharacterClass: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

CharacterClass:server_onFixedUpdate( timeStep )
CharacterClass: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

CharacterClass: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.

Client-only

onUpdate

CharacterClass: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

CharacterClass: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

CharacterClass:client_onLocalPlayerChangedWorld( world )

Called when the client player changes world.

Parameters:

Name Type Description
self table The class instance.
world World The entered world.

onProjectile

CharacterClass:client_onProjectile(
    position,
    airTime,
    velocity,
    projectileName,
    shooter,
    damage,
    customData,
    normal,
    uuid,
    mass
)

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:

Name Type Description
self table The class instance.
position Vec3 The position in world space where the projectile hit the Character.
airTime number The time, in seconds, that the projectile spent flying before the hit.
velocity Vec3 The velocity of the projectile at impact.
projectileName string The name of the projectile. (Legacy, use uuid instead)
shooter Player/Shape/Harvestable/nil The shooter, can be a Player, Shape, Harvestable or nil if unknown. Projectiles shot by a Unit will be nil on the client.
damage number The damage value of the projectile.
customData any A Lua object that can be defined at shoot time using sm.projectile.customProjectileAttack or an other custom version.
normal Vec3 The normal at the point of impact.
uuid Uuid The uuid of the projectile.
mass number The mass of the projectile.

onMelee

CharacterClass:client_onMelee(
    position,
    attacker,
    damage,
    power,
    direction,
    normal
)

Called when the Character is hit by a melee hit.

Parameters:

Name Type Description
self table The class instance.
position Vec3 The position in world space where the Character was hit.
attacker Player/nil The attacker. Can be a Player or nil if unknown. Attacks made by a Unit will be nil on the client.
damage integer The damage value of the melee hit.
power number The physical impact impact of the hit.
direction Vec3 The direction that the melee attack was made.
normal Vec3 The normal at the point of impact.

onCollision

CharacterClass:client_onCollision(
    other,
    position,
    selfPointVelocity,
    otherPointVelocity,
    normal
)

Called when the Character collides with another object.

Parameters:

Name Type Description
self table The class instance.
other Shape/Character/Harvestable/Lift/nil The other object. Nil if terrain.
position Vec3 The position in world space where the collision occurred.
selfPointVelocity Vec3 The velocity that that the Character had at the point of collision.
otherPointVelocity Vec3 The velocity that that the other object had at the point of collision.
normal Vec3 The collision normal between the Character and the other other object.

onGraphicsLoaded

CharacterClass:client_onGraphicsLoaded(  )

Called when graphics are loaded for the Character.

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

Parameters:

Name Type Description
self table The class instance.

onGraphicsUnloaded

CharacterClass:client_onGraphicsUnloaded(  )

Called when graphics are unloaded for the Character.

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

Parameters:

Name Type Description
self table The class instance.

onInteract

CharacterClass:client_onInteract( character, state )

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

Parameters:

Name Type Description
self table The class instance.
character Character The Character of the Player that is interacting with this Character.
state boolean The interaction state. Always true. The CharacterClass only receives the key down event.

canInteract

CharacterClass:client_canInteract( character )

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:

Name Type Description
self table The class instance.
character Character The Character of the Player that is looking at this Character.

Returns:

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

onEvent

CharacterClass:client_onEvent( event )

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:

Name Type Description
self table The class instance.
event string The event name.