ShapeClass
A script class that is instanced for every "scripted" Interactable Shape in the game.
An interactable part is a Shape that is usually built by the player and can be interacted with. For instance a button or an engine.
Can receive events sent with sm.event.sendToInteractable.
Fields:
Type | Name | Description |
---|---|---|
Interactable | interactable | The Interactable game object belonging to this class instance. (The same as shape.interactable) |
Shape | shape | The Shape game object that the Interactable is attached to. (The same as interactable.shape) |
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. |
any | data | Data from the "data" json element. |
any | params | Parameter set with Interactable.setParams when created from a script. |
Constants:
- colorHighlight
- colorNormal
- connectionInput
- connectionOutput
- maxChildCount
- maxParentCount
- poseWeightCount
Common callbacks:
- server_onCreate
- client_onCreate
- server_onDestroy
- client_onDestroy
- server_onRefresh
- client_onRefresh
- server_onFixedUpdate
- client_onFixedUpdate
- client_onUpdate
- client_onClientDataUpdate
Callbacks:
- server_onUnload
- client_onInteract
- client_onTinker
- client_onInteractThroughJoint
- client_onAction
- client_onProjectile
- server_onProjectile
- server_onSledgehammer
- client_onMelee
- server_onMelee
- server_onExplosion
- client_onCollision
- server_onCollision
- client_canErase
- server_canErase
- client_canInteract
- client_canInteractThroughJoint
- client_canTinker
- client_getAvailableParentConnectionCount
- client_getAvailableChildConnectionCount
- client_canCarry
colorHighlight Color
Sets the connection-point highlight color. The connection-point is shown when using the Connect Tool and selecting the interactable. (Defaults to white)
colorNormal Color
Sets the connection-point normal color. The connection-point is shown when using the Connect Tool. (Defaults to gray)
connectionInput integer
Sets the connection input type flags. (See sm.interactable.connectionType) (Defaults to 0, no input)
connectionOutput integer
Sets the connection output type flags. (See sm.interactable.connectionType) (Defaults to 0, no output)
maxChildCount integer
Sets the maximum number of allowed child connections – the number of output connections. (Defaults to 0, no allowed child connections)
Note:
Implement client_getAvailableChildConnectionCount to control specific types.
maxParentCount integer
Sets the maximum number of allowed parent connections – the number of input connections. (Defaults to 0, no allowed parent connections)
Note:
Implement client_getAvailableParentConnectionCount to control specific types.
poseWeightCount integer
Sets the number of animation poses the shape'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 to move the lever on the engine.
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:
Type | Name | Description |
---|---|---|
table | self | The 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:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
server_onDestroy(self) serverEventCallback
Called when the scripted object is destroyed.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
client_onDestroy(self) clientEventCallback
Called when the scripted object is destroyed.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The 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:
Type | Name | Description |
---|---|---|
table | self | The 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:
Type | Name | Description |
---|---|---|
table | self | The 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:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
number | timeStep | The 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:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
number | timeStep | The 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:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
number | deltaTime | Delta 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:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
any | data | Any lua object set with Network.setClientData |
integer | channel | Client data channel, 1 or 2. (default: 1) |
server_onUnload(self) serverEventCallback
Called when the Interactable is unloaded from the game because no Player's Character is close enough to it. Also called when exiting the game.
Note:
The creation, consisting of one or more bodies, consisting of one or more shapes joined together with joints are always unloaded at the same time.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
client_onInteract(self, character, state) clientEventCallback
Called when a Player is interacting with the Interactable by pressing the 'Use' key (default 'E') or pressing '0–9' if the Interactable is connected to a seat. (See: Interactable.pressSeatInteractable)
Note:
If this method is defined, the player will see the interaction text "E Use" when looking at the Shape.
-- Example of interaction function MySwitch.client_onInteract( self, _, state ) if state == true then self.network:sendToServer( 'sv_n_toggle' ) end end function MySwitch.sv_n_toggle( self ) -- Toggle on and off self.interactable.active = not self.interactable.active end
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Character | character | The Character of the Player that is interacting with the Interactable. |
boolean | state | The interaction state. (true if pressed, false if released) |
client_onTinker(self, character, state) clientEventCallback
Called when a Player is tinkering with the Interactable by pressing the 'Tinker' key (default 'U').
Note:
Tinkering usually means opening the upgrade menu for seats.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Character | character | The Character of the Player that is tinkering with the Interactable. |
boolean | state | The interaction state. (true if pressed, false if released) |
client_onInteractThroughJoint(self, character, state, joint) clientEventCallback
Called when a Player is interacting with the Interactable through a connected Joint.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Character | character | The Character of the Player that is interacting with the Interactable. |
boolean | state | The interaction state. Always true. client_onInteractThroughJoint only receives the key down event. |
Joint | joint | The Joint that the Player interacted through. |
client_onAction(self, action, state) clientEventCallback
Called when the interactable receives input from a Player with the Character locked to the Interactable.
When a Character is seated in an Interactable Shape with a "seat" component, the Character is also considered locked to the Interactable.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
integer | action | The action as an integer value. More details in sm.interactable.actions. |
boolean | state | True on begin action, false on end action. |
client_onProjectile(self, position, airTime, velocity, projectileName, shooter, damage, customData, normal, uuid) clientEventCallback
Called when the Shape is hit by a projectile.
Note:
If the shooter is destroyed before the projectile hits, the shooter value will be nil.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Vec3 | position | The position in world space where the projectile hit the Shape. |
number | airTime | The time, in seconds, that the projectile spent flying before the hit. |
Vec3 | velocity | The velocity of the projectile at impact. |
string | projectileName | The name of the projectile. (Legacy, use uuid instead) |
Player/Shape/Harvestable/nil | shooter | The shooter, can be a Player, Shape, Harvestable or nil if unknown. Projectiles shot by a Unit will be nil on the client. |
number | damage | The damage value of the projectile. |
any | customData | A Lua object that can be defined at shoot time using sm.projectile.customProjectileAttack or an other custom version. |
Vec3 | normal | The normal at the point of impact. |
Uuid | uuid | The uuid of the projectile. |
server_onProjectile(self, position, airTime, velocity, projectileName, shooter, damage, customData, normal, uuid) serverEventCallback
Called when the Shape is hit by a projectile.
Note:
If the shooter is destroyed before the projectile hits, the shooter value will be nil.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Vec3 | position | The position in world space where the projectile hit the Shape. |
number | airTime | The time, in seconds, that the projectile spent flying before the hit. |
Vec3 | velocity | The velocity of the projectile at impact. |
string | projectileName | The name of the projectile. (Legacy, use uuid instead) |
Player/Unit/Shape/Harvestable/nil | shooter | The shooter. Can be a Player, Unit, Shape, Harvestable or nil if unknown. |
integer | damage | The damage value of the projectile. |
any | customData | A Lua object that can be defined at shoot time using sm.projectile.customProjectileAttack or an other custom version. |
Vec3 | normal | The normal at the point of impact. |
Uuid | uuid | The uuid of the projectile. |
server_onSledgehammer() serverEventCallback
Deprecated:
Use server_onMelee instead.
client_onMelee(self, position, attacker, damage, power, direction, normal) clientEventCallback
Called when the Shape is hit by a melee attack.
Note:
If the attacker is destroyed before the hit lands, the attacker value will be nil.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Vec3 | position | The position in world space where the Shape was hit. |
Player/nil | attacker | The attacker. Can be a Player or nil if unknown. Attacks made by a Unit will be nil on the client. |
integer | damage | The damage value of the melee hit. |
number | power | The physical impact impact of the hit. |
Vec3 | direction | The direction that the melee attack was made. |
Vec3 | normal | The normal at the point of impact. |
server_onMelee(self, position, attacker, damage, power, direction, normal) serverEventCallback
Called when the Shape is hit by a melee attack.
Note:
If the attacker is destroyed before the hit lands, the attacker value will be nil.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Vec3 | position | The position in world space where the Shape was hit. |
Player/Unit/nil | attacker | The attacker. Can be a Player, Unit or nil if unknown. |
integer | damage | The damage value of the melee hit. |
number | power | The physical impact impact of the hit. |
Vec3 | direction | The direction that the melee attack was made. |
Vec3 | normal | The normal at the point of impact. |
server_onExplosion(self, center, destructionLevel) serverEventCallback
Called when the Shape is hit by an explosion.
For more information about explosions, see sm.physics.explode.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Vec3 | center | The center of the explosion. |
integer | destructionLevel | The level of destruction done by this explosion. Corresponds to the 'durability' rating of a Shape. |
client_onCollision(self, other, position, selfPointVelocity, otherPointVelocity, normal) clientEventCallback
Called when the Shape collides with another object.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Shape/Character/Harvestable/Lift/nil | other | The other object. Nil if terrain. |
Vec3 | position | The position in world space where the collision occurred. |
Vec3 | selfPointVelocity | The velocity that that the Shape had at the point of collision. |
Vec3 | otherPointVelocity | The velocity that that the other object had at the point of collision. |
Vec3 | normal | The collision normal between the Shape and the other other object. |
server_onCollision(self, other, position, selfPointVelocity, otherPointVelocity, normal) serverEventCallback
Called when the Shape collides with another object.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Shape/Character/Harvestable/Lift/nil | other | The other object. Nil if terrain. |
Vec3 | position | The position in world space where the collision occurred. |
Vec3 | selfPointVelocity | The velocity that that the Shape had at the point of collision. |
Vec3 | otherPointVelocity | The velocity that that the other object had at the point of collision. |
Vec3 | normal | The collision normal between the Shape and the other other object. |
client_canErase(self) clientEventCallback
Called to check whether the Shape can be erased at this moment.
Note:
Can be used to override restrictions. (See Shape.erasable)
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Returns:
Type | Description |
---|---|
boolean | A boolean indicating whether the Shape can be erased or not. (Defaults to true) |
server_canErase(self) serverEventCallback
Called to check whether the Shape can be erased at this moment.
Note:
Can be used to override restrictions. (See Shape.erasable)
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Returns:
Type | Description |
---|---|
boolean | A boolean indicating whether the Shape can be erased or not. (Defaults to true) |
client_canInteract(self, character) clientEventCallback
Called to check whether the Interactable can be interacted with at this moment.
Note:
This callback can also be used to change the interaction text shown to the player using sm.gui.setInteractionText. (Defaults to "E Use")
Note:
Can be used to override restrictions. (See Shape.usable)
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Character | character | The Character of the Player that is looking at the Shape. |
Returns:
Type | Description |
---|---|
boolean | A boolean indicating whether the interactable can be interacted with or not. (Defaults to true if client_onInteract is implemented, otherwise false) |
client_canInteractThroughJoint(self, character) clientEventCallback
Called to check whether the Interactable can be interacted with through a child Joint at this moment.
Note:
This callback can also be used to change the interaction text shown to the player using sm.gui.setInteractionText. (Defaults to "E Use")
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Character | character | The Character of the Player that is looking at the Joint. |
Returns:
Type | Description |
---|---|
boolean | A boolean indicating whether the interactable can be interacted with or not. (Defaults to true if client_onInteractThroughJoint is implemented, otherwise false) |
client_canTinker(self, character) clientEventCallback
Called to check whether the Interactable can be tinkered with at this moment.
Note:
Tinkering usually means opening the upgrade menu for seats.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Character | character | The Character of the Player that is looking at the Shape. |
Returns:
Type | Description |
---|---|
boolean | A boolean indicating whether the interactable can be tinkered with or not. (Defaults to true if client_onTinker is implemented, otherwise false) |
client_getAvailableParentConnectionCount(self, flags) clientEventCallback
Called to check how many more parent (input) connections with the given type flag the Interactable will accept. Return 1 or more to allow a connection of this type.
-- Example of implementation where logic and power shares the same slot but electricity counts as separate MyIteractable.maxParentCount = 2 MyIteractable.connectionInput = sm.interactable.connectionType.logic + sm.interactable.connectionType.power + sm.interactable.connectionType.electricity function MyIteractable.client_getAvailableParentConnectionCount( self, flags ) if bit.band( flags, bit.bor( sm.interactable.connectionType.logic, sm.interactable.connectionType.power ) ) ~= 0 then return 1 - self:getParents( bit.bor( sm.interactable.connectionType.logic, sm.interactable.connectionType.power ) ) end if bit.band( flags, sm.interactable.connectionType.electricity ) ~= 0 then return 1 - self:getParents( sm.interactable.connectionType.electricity ) end return 0 end
Note:
maxParentCount must be 1 or more for this callback to be called.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
integer | flags | Connection type flags. |
Returns:
Type | Description |
---|---|
integer | The number of available connections. |
client_getAvailableChildConnectionCount(self, flags) clientEventCallback
Called to check how many more child (output) connections with the given type flag the Interactable will accept. Return 1 or more to allow a connection of this type.
-- Example of implementation that accepts 10 logic connections and 1 power connection MyInteractable.maxChildCount = 11 MyInteractable.connectionOutput = sm.interactable.connectionType.logic + sm.interactable.connectionType.power function MyIteractable.client_getAvailableChildConnectionCount( self, flags ) if bit.band( flags, sm.interactable.connectionType.logic ) ~= 0 then return 10 - self:getParents( sm.interactable.connectionType.logic ) end if bit.band( flags, sm.interactable.connectionType.power ) ~= 0 then return 1 - self:getParents( sm.interactable.connectionType.power ) end return 0 end
Note:
maxChildCount must be 1 or more for this callback to be called.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
integer | flags | Connection type flags. |
Returns:
Type | Description |
---|---|
integer | The number of available connections. |
client_canCarry(self) clientEventCallback
Called to check if the shape must be carried instead of put in the inventory.
Note:
Shapes with the "carryItem" attribute are always carried.
Parameters:
Type | Name | Description |
---|---|---|
table | self | The class instance. |
Returns:
Type | Description |
---|---|
boolean | A boolean indicating whether the interacable must be carried or not. (Defaults to false) |