HarvestableClass
A harvestable class 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.
Instance Members
These members are set by the engine when instantiating the script class and are unique to each new instance of it.
harvestable
self.harvestable (Harvestable):
The harvestable that owns this script instance.
network
self.network (Network):
A network interface, for client/server instance communication.
storage
self.storage (Storage):
A data storage interface, used to store persistent data into the save file.
data
self.data (nil/boolean/number/string/table):
JSON data parsed from the data key in the harvestable's JSON file entry.
params
self.params (any):
Parameter passed to sm.harvestable.createHarvestable or set in the terrain generation script.
Class Constants
These constants may be set in the global script class table to configure certain parts of the class.
Note that changing these on an already-existing script instance will not update the settings.
poseWeightCount
HarvestableClass.poseWeightCount (int):
Sets the number of animation poses the harvestable's model is able to use.
Value are integers 0-3 (defaults to 0, no poses).
A value greater that 0 indicates that the renderable's mesh is set up to blend into pose0, pose1, pose2.
Server Callbacks
These callbacks are executed in server context.
server_onProjectile
function HarvestableClass.server_onProjectile(self, position, airTime, velocity, projectileName, shooter, damage, customData, normal, uuid)
end
Called when the Harvestable is hit by a projectile.
If the shooter is destroyed before the projectile hits, the shooter value will be nil.
Parameters:
self(table): The script class instance.position(Vec3): The projectile hit world position.airTime(number): The time, in seconds, that the projectile spent flying before the hit.velocity(Vec3): The impact velocity of the projectile.projectileName(string): The name of the projectile (legacy, use uuid instead).shooter(Player/Unit/Shape/Harvestable/nil): The source/owner of the projectile.damage(int): The projectile's damage value.customData(any): Custom data defined with thecustom*functions in sm.projectile.normal(Vec3): The hit point normal vector.uuid(Uuid): The UUID of the projectile.
server_onCollision
function HarvestableClass.server_onCollision(self, other, position, selfPointVelocity, otherPointVelocity, normal)
end
Called when the Harvestable collides with another object.
Parameters:
self(table): The script class instance.other(Shape/Character/Harvestable/Lift/nil): The other object. Nil if terrain.position(Vec3): The hit world position.selfPointVelocity(Vec3): The harvestable's velocity at the collision hit point.otherPointVelocity(Vec3): The other object's velocity at the collision hit point.normal(Vec3): The collision normal between the two objects.
server_onMelee
function HarvestableClass.server_onMelee(self, position, attacker, damage, power, direction, normal)
end
Called when the Harvestable is hit by a melee attack.
If the attacker is destroyed before the hit lands, the attacker value will be nil.
Parameters:
self(table): The script class instance.position(Vec3): The hit world position.attacker(Player/Unit/nil): The source/owner of the attack.damage(int): The attack's damage value.power(number): The physical impact strength of the hit.direction(Vec3): The direction of the attack.normal(Vec3): The hit point normal vector.
server_canErase
function HarvestableClass.server_canErase(self)
return true -- or false
end
Called to check if the Harvestable can be erased by players at this moment.
This defaults to the removable JSON value in the harvestableset, which defaults to false.
Parameters:
self(table): The script class instance.
Returns:
- boolean: Whether the harvestable can be erased or not.
server_onUnload
function HarvestableClass.server_onUnload(self)
end
Called when the Harvestable is unloaded from the world due to its surrounding terrain being unloaded.
This happens when no Player's Character is close enough to it.
Parameters:
self(table): The script class instance.
server_onReceiveUpdate
function HarvestableClass.server_onReceiveUpdate(self)
end
Called in random intervals on all loaded Harvestables to indicate some time has passed.
This interval is random, and random across different harvestables.
Multiple harvestables may receive an update at the same time and interval, but this is not guaranteed.
After each update, a new random time is chosen, after which the next update will be triggered.
For performance reasons, it is recommended to use this instead of server_onFixedUpdate, if the update does not need to happen frequently.
sm.game.getCurrentTick can be used to determine the time between updates.
Parameters:
self(table): The script class instance.
server_onExplosion
function HarvestableClass.server_onExplosion(self, center, destructionLevel)
end
Called when the Harvestable is hit by an explosion.
Parameters:
self(table): The script class instance.center(Vec3): The center position of the explosion.destructionLevel(int): The destruction level of the explosion. Corresponds to thedurabilityrating of a shape.
server_onRemoved
function HarvestableClass.server_onRemoved(self, player)
end
Called when a Player attempts to remove the Harvestable.
The harvestable script is responsible for the actual removal, using Harvestable:destroy().
Parameters:
self(table): The script class instance.player(Player): The player that attempted to remove the harvestable.
Client Callbacks
These callbacks are executed in client context.
client_onProjectile
function HarvestableClass.client_onProjectile(self, position, airTime, velocity, projectileName, shooter, damage, customData, normal, uuid)
end
Called when the Harvestable is hit by a projectile.
If the shooter is destroyed before the projectile hits, the shooter value will be nil.
Parameters:
self(table): The script class instance.position(Vec3): The projectile hit world position.airTime(number): The time, in seconds, that the projectile spent flying before the hit.velocity(Vec3): The impact velocity of the projectile.projectileName(string): The name of the projectile (legacy, use uuid instead).shooter(Player/Shape/Harvestable/nil): The source/owner of the projectile.damage(int): The projectile's damage value.customData(any): Custom data defined with thecustom*functions in sm.projectile.normal(Vec3): The hit point normal vector.uuid(Uuid): The UUID of the projectile.
client_onCollision
function HarvestableClass.client_onCollision(self, other, position, selfPointVelocity, otherPointVelocity, normal)
end
Called when the Harvestable collides with another object.
Parameters:
self(table): The script class instance.other(Shape/Character/Harvestable/Lift/nil): The other object. Nil if terrain.position(Vec3): The hit world position.selfPointVelocity(Vec3): The harvestable's velocity at the collision hit point.otherPointVelocity(Vec3): The other object's velocity at the collision hit point.normal(Vec3): The collision normal between the two objects.
client_onMelee
function HarvestableClass.client_onMelee(self, position, attacker, damage, power, direction, normal)
end
Called when the Harvestable is hit by a melee attack.
If the attacker is destroyed before the hit lands, the attacker value will be nil.
Parameters:
self(table): The script class instance.position(Vec3): The hit world position.attacker(Player/nil): The source/owner of the attack.damage(int): The attack's damage value.power(number): The physical impact strength of the hit.direction(Vec3): The direction of the attack.normal(Vec3): The hit point normal vector.
client_canErase
function HarvestableClass.client_canErase(self)
return true -- or false
end
Called to check if the Harvestable can be erased by players at this moment.
Parameters:
self(table): The script class instance.
Returns:
- boolean: Whether the harvestable can be erased or not.
client_canInteract
function HarvestableClass.client_canInteract( self, character )
return true -- or false
end
Called to check whether the Harvestable can be interacted with at this moment.
This callback is also responsible for updating the interaction text shown to the player using sm.gui.setInteractionText().
Parameters:
self(table): The script class instance.character(Character): The character of the player that is looking at the shape.
Returns:
- boolean: Whether the harvestable can be interacted with or not.
client_onInteract
function HarvestableClass.client_onInteract(self, character, state)
end
Called when a Player is interacting with the Harvestable by pressing the Use input (default: E).
Parameters:
self(table): The script class instance.character(Character): The interacting player's character.state(boolean): The interaction state (true= pressed,false= released).
client_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.
Parameters:
self(table): The script class instance.action(int): The action ID.state(boolean): The action state (true= begin,false= end).