HarvestableClass

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

A tree or a plant that can be harvested is a typical case of a harvestable.

Can receive events sent with sm.event.sendToHarvestable.

Fields

Name Type Description
harvestable Harvestable The Harvestable game object belonging to this class instance.
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 Data from the "data" json element.
params any Parameter sent to sm.harvestable.create or set in the terrain generation script.
tags any The tags set on the harvestable from the editor.

Constants

Name Type Description
poseWeightCount integer Sets the number of animation poses the harvestable's model is able to use.

poseWeightCount

Value type: integer

Sets the number of animation poses the harvestable's model is able to use.

Value can be are integers 0-3. (Defaults to 0, no poses)

A value greater that 0 indicates that the renderable's "mesh" is set up blend into "pose0", "pose1", "pose2".

This is, for instance, used for growing plants.

Server + Client

onCreate

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

HarvestableClass:server_onDestroy(  )
HarvestableClass:client_onDestroy(  )

Called when the scripted object is destroyed.

Parameters:

Name Type Description
self table The class instance.

onRefresh

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

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

onCollision

HarvestableClass:server_onCollision(
    other,
    position,
    selfPointVelocity,
    otherPointVelocity,
    normal
)
HarvestableClass:client_onCollision(
    other,
    position,
    selfPointVelocity,
    otherPointVelocity,
    normal
)

Called when the Harvestable collides with another object.

Parameters:

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

onProjectile

Server

HarvestableClass:server_onProjectile(
    position,
    airTime,
    velocity,
    projectileName,
    shooter,
    damage,
    customData,
    normal,
    uuid,
    mass
)

Called when the Harvestable is hit by a projectile.

Note: If the shooter is destroyed before the projectile hits, 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 Harvestable.
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/Unit/Shape/Harvestable/nil The shooter. Can be a Player, Unit, Shape, Harvestable or nil if unknown.
damage integer 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.

Client

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

Called when the Harvestable is hit by a projectile.

Note: If the shooter is destroyed before the projectile hits, 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 Harvestable.
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 integer 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

Server

HarvestableClass:server_onMelee(
    position,
    attacker,
    damage,
    power,
    direction,
    normal
)

Called when the Harvestable is hit by a melee attack.

Note: If the attacker is destroyed before the hit lands, the attacker value will be nil.

Parameters:

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

Client

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

Called when the Harvestable is hit by a melee attack.

Note: If the attacker is destroyed before the hit lands, the attacker value will be nil.

Parameters:

Name Type Description
self table The class instance.
position Vec3 The position in world space where the Harvestable 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 of the hit.
direction Vec3 The direction that the melee attack was made.
normal Vec3 The normal at the point of impact.

canErase

HarvestableClass:server_canErase(  )
HarvestableClass:client_canErase(  )

Called to check whether the Harvestable can be erased at this moment.

Parameters:

Name Type Description
self table The class instance.

Returns:

Type Description
boolean A boolean indicating whether the Harvestable can be removed or not. (Defaults to "removable" json value which defaults to false)

Server-only

onReceiveUpdate

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

onUnload

HarvestableClass:server_onUnload(  )

Called when the Harvestable is unloaded from the game because no Player's Character is close enough to it. Also called when exiting the game.

Parameters:

Name Type Description
self table The class instance.

onExplosion

HarvestableClass:server_onExplosion( center, destructionLevel, damage )

Called when the Harvestable is hit by an explosion.

Parameters:

Name Type Description
self table The class instance.
center Vec3 The center of the explosion.
destructionLevel integer The level of destruction done by this explosion. Corresponds to the "durability" rating of a Shape.
damage integer The damage value of the explosion.

onRemoved

HarvestableClass:server_onRemoved( player )

Called when a Player wants to remove the Harvestable.

Note: The HarvestableClass is responsible for doing the remove using Harvestable.destroy.

Parameters:

Name Type Description
self table The class instance.
player Player The Player that wants to remove the Harvestable.

onIgnite

HarvestableClass:server_onIgnite(  )

Called when the Harvestable is ignited by a nearby fire.

Parameters:

Name Type Description
self table The class instance.

onFloating

HarvestableClass:server_onFloating(  )

Called when the Harvestable is floating.

Parameters:

Name Type Description
self table The class instance.

Client-only

onUpdate

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

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

HarvestableClass:client_onLocalPlayerChangedWorld( world )

Called when the client player changes world.

Parameters:

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

onInteract

HarvestableClass:client_onInteract( character, state )

Called when a Player is interacting with the Harvestable 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 the Harvestable.
state boolean The interaction state. Always true. The HarvestableClass only receives the key down event.

canInteract

HarvestableClass:client_canInteract( character )

Called to check whether the Harvestable 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 the Harvestable.

Returns:

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

onAction

HarvestableClass:client_onAction( action, state )

Called when the harvestable receives input from a player with the Character locked to the Harvestable.

When a Character is seated in a Harvestable with a "seat" component, the Character is also considered locked to the Harvestable.

Parameters:

Name Type Description
self table The class instance.
action integer The action as an integer value. More details in sm.interactable.actions.
state boolean True on begin action, false on end action.