Skip to main content

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.

The class can receive events sent with sm.event.sendToHarvestable.

The fields below are accessed using self.fieldName in the HarvestableClass script:

Fields:
TypeNameDescription
HarvestableharvestableThe Harvestable game object belonging to this class instance.
NetworknetworkA Network object that can be used to send data between client and server.
StoragestorageA server-side Storage object that can be used to save and load data to/from the world database.
anydataData from the data entry in the harvestable's JSON file entry.
anyparamsParameters given to sm.harvestable.create or set in the terrain generation script.
Constants:

poseWeightCount

Callbacks:

Server + Client

onProjectile

function HarvestableClass.server_onProjectile( self, position, airTime, velocity, projectileName, shooter, damage, customData, normal, uuid )
end
function HarvestableClass.client_onProjectile( self, position, airTime, velocity, projectileName, shooter, damage, customData, normal, uuid )
end

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.

Arguments:
  • 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 [ int ]: The damage value of the projectile.
  • customData [ any ]: A Lua object that can be defined at shoot time using sm.projectile.customProjectileAttack or any other custom version.
  • normal [ Vec3 ]: The normal at the point of impact.
  • uuid [ Uuid ]: The uuid of the projectile.

onCollision

function HarvestableClass.server_onCollision( self, other, position, selfPointVelocity, otherPointVelocity, normal )
end
function HarvestableClass.client_onCollision( self, other, position, selfPointVelocity, otherPointVelocity, normal )
end

Called when the Harvestable collides with another object.

Arguments:
  • self [ table ]: The class instance.
  • other [ Shape / Character / Harvestable / Lift / nil ]: The other object. Nil if terrain.
  • position [ Player / nil ]: The position in world space where the collision occurred.
  • selfPointVelocity [ int ]: The velocity that that the Harvestable had at the point of collision.
  • otherPointVelocity [ number ]: The velocity that that the other object had at the point of collision.
  • normal [ Vec3 ]: The collision normal between the Harvestable and the other object.

onMelee

function HarvestableClass.server_onMelee( self, position, attacker, damage, power, direction, normal )
end
function HarvestableClass.client_onMelee( self, position, attacker, damage, power, direction, normal )
end

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.

Arguments:
  • 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 [ int ]: The damage value of the melee hit.
  • power [ number ]: The physical impact of the hit.
  • direction [ Vec3 ]: The direction of the melee attack.
  • normal [ Vec3 ]: The normal at the point of impact.

canErase

function HarvestableClass.server_canErase( self )
return true --true or false, default false
end
function HarvestableClass.client_canErase( self )
return true --true or false, default false
end

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

Arguments:
  • self [ table ]: The class instance.
Returns:
  • [ bool ]: A boolean indicating whether the Harvestable can be erased or not. Defaults to "removable" json value which defaults to false.

Server-only

onUnload

function HarvestableClass.server_onUnload( self )
end

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.

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

onReceiveUpdate

function HarvestableClass.server_onReceiveUpdate( self )
end

Called occasionally on the HarvestableClass to indicate that some time has passed.

For performance reasons it is 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.

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

onExplosion

function HarvestableClass.server_onExplosion( self, center, destructionLevel )
end

Called when the Harvestable is hit by an explosion.

Arguments:
  • self [ table ]: The class instance.
  • center [ Vec3 ]: The center of the explosion.
  • destructionLevel [ int ]: The level of destruction done by this explosion. Corresponds to the durability rating of a Shape.

onRemoved

function HarvestableClass.server_onRemoved( self, player )
end

Called when a Player wants to remove the Harvestable.

note

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

Arguments:
  • self [ table ]: The class instance.
  • player [ Player ]: The Player that wants to remove the Harvestable.

Client-only

onInteract

function HarvestableClass.client_onInteract( self, character, state )
end

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

Arguments:
  • self [ table ]: The class instance.
  • character [ Character ]: The Character of the Player that is interacting with the Harvestable.
  • state [ bool ]: The interaction state (true if pressed, false if released).

canInteract

function HarvestableClass.client_canInteract( self, character )
return true --true or false, default true if onInteract is implemented
end

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.

Arguments:
  • self [ table ]: The class instance.
  • character [ Character ]: The Character of the Player that is looking at the Harvestable.
Returns:
  • [ bool ]: A boolean indicating whether the Harvestable can be interacted with or not (Defaults to true if client_onInteract is implemented, otherwise false).

onAction

function HarvestableClass.client_onAction( self, action, state )
end

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

Details about the action value are in sm.interactable.actions.

Arguments:
  • self [ table ]: The class instance.
  • action [ int ]: The action as an integer value.
  • state [ bool ]: True on begin action, false on end action.