Entity | CraytaKit
Skip to main content

Entity

Class Functions

Class Function NameReturn TypeDescriptionTags
Entity.IsValid(Entity entity)booleanReturns true if the parameter passed to it is a valid entityNone

Functions

Function NameReturn TypeDescriptionTags
IsA(table derivedType)booleanReturns true if the entity is the type passedNone
GetName()stringGet the name of this entityNone
GetWorld()WorldGet the World from an entityDeprecated
RevertClientProperty(string propertyName)NoneRevert a property that's been changed on the client back to the server's value for itClient Only
SetPosition(Vector position)NoneSet the position of this Entity in 3D spaceNone
AlterPosition(Vector position, number time)numberMove from current to position over timeNone
AlterPosition(Vector fromPosition Vector toPosition, number time)numberMove from fromPosition to toPosition over timeNone
GetPosition()VectorGet the position of this EntityNone
SetRotation(Rotation rotation)NoneSet the rotation of this EntityNone
AlterRotation(Rotation rotation, number time)numberRotate from current to rotation over timeNone
AlterRotation(Rotation fromRotation, Rotation toRotation, number time)numberRotate from fromRotation to toRotation over timeNone
GetRotation()RotationGet the rotation of this EntityNone
SetRelativePosition(Vector position)NoneSet the position of this Entity relative to whatever this entity is parented toNone
SetPivotPoint(Vector pivotPoint)NoneSet the piuvot point of this EntityNone
AlterRelativePosition(Vector position, number time)numberMove from current to position over time relative to whatever this entity is parented toNone
AlterRelativePosition(Vector fromPosition, Vector toPosition, number time)numberMove from fromPosition to toPosition over time relative to whatever this entity is parented toNone
GetRelativePosition()VectorGet the position of this Entity relative to whatever this entity is parented toNone
SetRelativeRotation(Rotation rotation)NoneSet the rotation of this Entity relative to whatever this entity is parented toNone
AlterRelativeRotation(Rotation rotation, number time)numberRotate from current to rotation over time relative to whatever this entity is parented toNone
AlterRelativeRotation(Rotation fromRotation, Rotation toRotation, number time)numberRotate from fromRotation to toRotation over time relative to whatever this entity is parented toNone
GetRelativeRotation()RotationGet the rotation of this Entity relative to whatever this entity is parented toNone
GetForward()VectorGet the forward facing vector of an Entity from its rotationNone
GetRight()VectorGet the right facing vector of an Entity from its rotationNone
GetUp()VectorGet the up facing vector of an Entity from its rotationNone
SetForward(Vector forward)NoneSet the rotation of an Entity to make its front face in a given directionNone
SetForward(Vector forward, Vector up)NoneSet the rotation of an Entity to make its front face in a given direction, and its top point in anotherNone
PlaySound(SoundAsset sound)SoundHandlePlay a sound Asset on this Entity, returning a Handle which can be used to stop the soundNone
PlaySound(SoundAsset sound, number fadeIn)SoundHandlePlay a sound Asset on this Entity, returning a Handle which can be used to stop the sound. Fades in over the given fadeIn timeNone
PlaySound(SoundAsset sound, number fadeIn, string group)SoundHandlePlay a sound Asset on this Entity, returning a Handle which can be used to stop the sound. Fades in over the given fadeIn time. Fades out any sound already playing in the GroupName with the given fadeIn timeNone
PlaySound(SoundAsset sound, table properties)SoundHandlePlay a sound Asset on this Entity, returning a Handle which can be used to stop the sound. Takes a table containing the optional parametersNone
PlaySound2D(SoundAsset sound)SoundHandlePlay a sound Asset on this Entity but without a 3D transform on the sound (useful for UI sounds, stereo music stings, etc)None
PlaySound2D(SoundAsset sound, number fadeIn)SoundHandlePlay a sound Asset on this Entity but without a 3D transform on the sound (useful for UI sounds, stereo music stings, etc). Fades in over the given fadeIn timeNone
PlaySound2D(SoundAsset sound, number fadeIn, string group)SoundHandlePlay a sound Asset on this Entity but without a 3D transform on the sound (useful for UI sounds, stereo music stings, etc). Fades in over the given fadeIn time. Fades out any sound already playing in the GroupName with the given fadeIn timeNone
PlaySound2D(SoundAsset sound, table properties)SoundHandlePlay a sound Asset on this Entity but without a 3D transform on the sound (useful for UI sounds, stereo music stings, etc). Tables a table containing the optional parametersNone
PlaySoundAtLocation(Vector location, SoundAsset sound)SoundHandlePlay a sound Asset on this Entity at the given locationNone
PlaySoundAtLocation(Vector location, SoundAsset sound, number fadeIn)SoundHandlePlay a sound Asset on this Entity at the given location. Fades in over the given fadeIn timeNone
PlaySoundAtLocation(Vector location, SoundAsset sound, number fadeIn, string group)SoundHandlePlay a sound Asset on this Entity at the given location. Fades in over the given fadeIn time. Fades out any sound already playing in the GroupName with the given fadeIn timeNone
PlaySoundAtLocation(Vector location, SoundAsset sound, table properties)SoundHandlePlay a sound Asset on this Entity at the given location. Takes a table containing the optional parameters.None
StopSound(SoundHandle handle)NoneGiven a sound Handle stop the sound on this EntityNone
StopSound(SoundHandle handle, number fadeOut)NoneGiven a sound Handle stop the sound on this Entity. Fade the sound out over the given fadeOut timeNone
PlayEffect(EffectAsset effect)EffectHandlePlay a particle effect Asset on this Entity, returning a Handle which can be used to stop the effectNone
PlayEffect(EffectAsset effect, boolean attached)EffectHandlePlay a particle effect Asset on this Entity, returning a Handle which can be used to stop the effect. Optionally the effect is attached to the entity and so all spawned particles are relative to itNone
PlayEffectAtLocation(Vector location, EffectAsset effect)EffectHandlePlay a particle effect Asset at a given world location and rotation, returning a Handle which can be used to stop the effectNone
PlayEffectAtLocation(Vector location, EffectAsset effect, boolean attached)EffectHandlePlay a particle effect Asset at a given world location and rotation, returning a Handle which can be used to stop the effect. Optionally the effect is attached to the entity and so all spawned particles are relative to itNone
StopEffect(EffectHandle handle)NoneStop the effect represented by the given handleNone
Clone()EntityClone the Entity returning the cloneServer Only
AttachTo(Entity entity)NoneAttach this Entity to another EntityServer Only
AttachTo(Character character, string socket)NoneAttach this Entity to a Character entity, using the named socketNone
Detach()NoneDetach this entity from its parentNone
ApplyDamage(number damageAmount, HitResult hitResult, Vector shootDirection, Entity fromEntity)NoneApply damageAmount damage to the Entity (by calling OnDamage on it on any scripts that override that), also pass a HitResult from a World Raycast function and a shootDirection Vector and fromEntity which will be passed to the OnDamage functionNone
ApplyDamage(number damageAmount, HitResult hitResult, Vector shootDirection, Entity fromEntity, table damageModifiers)NoneApply damageAmount damage to the Entity (by calling OnDamage on it on any scripts that override that), also pass a HitResult from a World Raycast function and a shootDirection Vector and fromEntity which will be passed to the OnDamage functionNone
ApplyDamage(number damageAmount, Vector shootDirection, Entity fromEntity)NoneApply damageAmount damage to the Entity (by calling OnDamage on it on any scripts that override that), also pass a shootDirection Vector and fromEntity which will be passed to the OnDamage functionNone
ApplyDamage(number damageAmount, Vector shootDirection, Entity fromEntity, table damageModifiers)NoneApply damageAmount damage to the Entity (by calling OnDamage on it on any scripts that override that), also pass a shootDirection Vector and fromEntity which will be passed to the OnDamage functionNone
GetParent()EntityGet a parent Entity that this Entity is attached to either within the world tree or using the Attach functionNone
GetChildren()tableGet all children directly below this. The order of children is not guaranteed, and may change randomlyNone
Destroy()NoneDestroy an Entity. Use with care as any variables referencing that Entity will now be invalidServer Only
SendToScripts(string eventName, ...)NoneCall eventName function with the given args on all scripts that have it as a function. If called on the server do it only on the server, if called on a client do it only on that clientNone
SendToAllClients(string eventName, ...)NoneCall eventName on all scripts of this Entity on all clients connected to the server with the given argsServer Only
SendToServer(string eventName, ...)NoneCall eventName on all script of this Entity on the server. Passes the local user as the last parameter implicitlyLocal Only
SendToLocal(string eventName, ...)NoneCall eventName on all scripts of this Entity on the client that owns the Player or User this script is attached toServer Only
IsLocal()booleanSee if this Entity is owned by the local clientLocal Only
IsClient()booleanCheck if this Entity is on the clientDeprecated
FindScript(string scriptName)ScriptThis is alternative to entity.scriptName which is the preferred way of getting a scriptNone
FindScript(string scriptName, boolean recursive)This is alternative to entity.scriptName which is the preferred way of getting a script. This can be recursive to find the script on any child entitiesNone
FindScript(ScriptAsset scriptAsset)ScriptFind a script by its script assetNone
FindScript(ScriptAsset scriptAsset, boolean recursive)ScriptFind a script by its script asset. This can be recursive to find the script on any child entitiesNone
FindScriptProperty(string propertyName)objectFind a script with the named property on it and return value from the propertyNone
FindWidgetProperty(string propertyName)objectFind a widget with the bnamed property on it and return the value from the propertyNone
FindAllScripts(string scriptName)table<Script>Find all scripts named scriptName recursively on this entity and all child entities. Most often used where multiple scripts are used to simulate an array of structuresNone
FindAllScripts(ScriptAsset scriptAsset)table<Script>Find all scripts of the given script asset recursively on this entity and all child entities. Most often used where multiple scripts are used to simulate an array of structuresNone
FindAllScripts(ScriptAsset scriptAsset, boolean recursive)table<Script>Find all scripts named scriptName on this entity (and optionally recursively on all child entities if recursive flag is set). Most often used where multiple scripts are used to simulate an array of structuresNone
FindAllScripts(string scriptName, boolean recursive)table<Script>Find all scripts of the given script asset on this entity (and optionally recursively on all child entities if recursive flag is set). Most often used where multiple scripts are used to simulate an array of structuresNone
FindWidget(string widgetName)WidgetThis is alternative to entity.widgetName which is the preferred way of getting a widgetNone
FindWidget(string widgetName, boolean recursive)WidgetThis is alternative to entity.widgetName which is the preferred way of getting a widget. This can be recursive to find the widget on any child entitiesNone
FindWidget(WidgetAsset widget)WidgetFind a widget by its widget assetNone
FindWidget(WidgetAsset widget, boolean recursive)WidgetFind a widget by its widget asset. This can be recursive to find the widget on any child entitiesNone
IsLocalReady()booleanWhen called with an Entity that is owned by a particular client this sees if that Entity has been inited on that client (by calling LocalInit)Server Only
SendTelemetry(string type, table parameters)NoneSend an type of telemetry event to the telemetry server with the given parametersTable for later analysis. Deprecated and will be removed - see Analytics.SendTelemetryDeprecated
PlayTimeline(...)numberPlay a timeline from variable argsNone
PlayTimelineLoop(...)NoneLoop a timeline from variable args. See PlayTimelineNone
PlayTimelinePingPong(...)NoneLoop a timeline back and forth from variable args. See PlayTimelineNone
PlayRelativeTimeline(...)numberPlay a timeline, relative to an entity's parent transform, from variable args. See PlayTimelineNone
PlayRelativeTimelineLoop(...)NoneLoop a timeline, relative to an entity's parent transform, from variable args. See PlayTimelineNone
PlayRelativeTimelinePingPong(...)NoneLoop a timeline back and forth, relative to an entity's parent transform, from variable args. See PlayTimelineNone
CancelTimeline()NoneCancel any running timelineNone
GetTemplate()TemplateGiven an entity, get the Template it is an instance of (if there is one). Warning - this will return the template even if lots of things have been adjusted on the instanceNone
GetVelocity()VectorGet Velocity. Centimeters per secondNone
SetVelocity(Vector velocity)NoneSet Velocity. Centimeters per secondNone
GetAngularVelocity()RotationGet AngularVelocity. Degrees per secondNone
SetAngularVelocity(Rotation velocity)NoneSet AngularVelocity. Degrees per secondNone
GetOccupySockets()table<string>Return the occupy sockets on this EntityNone
GetSockets()table<string>Return the available sockets on this EntityNone
GetSocketPosition(string socketName)VectorReturns the position of a socketNone
GetSocketRelativePosition(string socketName)VectorReturns the position of the socket, relative to the parentNone
GetSocketRotation(string socketName)VectorReturns the rotation of the given socketNone
GetSocketRelativeRotation(string socketName)VectorReturns the rotation of the given socket, relative to the parentNone
GetOccupyAssetForSocket(string socketName)OccupyAssetReturns the occupy asset for a given socketNone

Properties

Property NameReturn TypeDescriptionTags
visiblebooleanSet whether any physical aspect of the Entity (generally a mesh or a light) is visible within the worldNone
onInteractEventCalled when this entity is interacted with by a player, with the player Character and the HitResult passed as arguments, as well as the Entity from which the onInteract event was sent. An alternative to listening for OnInteract in a script on the entityNone
onDestroyEventCalled when this entity is destroyed, with the Entity which sent the event passed as an argument. An alternative to listening for OnDestroy in a script on the entityNone

Overrides

Override NameReturn TypeDescriptionTags
indexobjectGet a script or widget from the Entity by name, or a mesh, light, etc component on this entity. ie entity.myScript returns a script called myScript, entity.theHud returns a widget named theHud, entity.mesh returns the Mesh of this Entity, entity.light returns the property bag for a light component, etcNone

Examples

AttachTo

Available sockets:

spine_01
spine_02
spine_03
spine_04
spine_05
neck_01
neck_02
head
clavicle_l
shoulder_l
upperarm_l
lowerarm_l
hand_l
weapon_l
clavicle_r
shoulder_r
upperarm_r
lowerarm_r
hand_r
weapon_r
thigh_l
calf_l
foot_l
thigh_r
calf_r
foot_r

PlaySound

Takes a table containing the optional parameters fadeIn, pitch, volume and groupName. (Pitch: 1 = Default, 0.5 = half speed, 2 = 2 times faster) (Volume: 0 = Silent, 1 = Full volume)

SendToServer

SendToServer calls a method on the server, when running on the client. It implicitly passes the user as the last parameter.

local ExampleScript = {}

ExampleScript.Properties = {}

function ExampleScript:ClientInit()
self:GetEntity():SendToServer("Test")
end

function ExampleScript:Test(entity)
print(entity:GetName()) -- This will print `userEntity1234`
end

return ExampleScript

PlayTimeline

The following example shows a simple timeline with three positions over a ten second timeline. This will play once when the code is executed. Each change of position is interpolated with EaseInOut.

self:GetEntity():PlayTimeline( 0.0, -- time of first position Vector.New(0, 0, 0), -- first position 5.0, Vector.New(0, 0, 100), "EaseInOut", 10.0, Vector.New(100, 0, 100), "EaseInOut" ) The following example shows a timeline which changes both position and rotation of the script's entity.

self:GetEntity():PlayTimeline(
0.0,
Vector.New(0, 0, 0),
Rotation.New(0, 0, 0),
1.0,
Vector.New(0, 0, 100),
Rotation.New(180, 0, 0)
)

The following example shows a timeline which simulates a bouncing ball by moving linearly in the X axis but using EaseOut on the up movement and EaseIn on the down movement to simulate the top of a bounce.

self:GetEntity():PlayTimeline(
0.0,
Vector.New(0, 0, 0),
1.0,
Vector.New(100, 0, 100),
{ z = "EaseOut", },
2.0,
Vector.New(200, 0, 0),
{ z = "EaseIn", }
)

You can pass the values to PlayTimeline as a table, allowing for procedural generation of a path. This example generates a circlulr path by appending times and positions to a table, and then passes the resulting table to PlayTimeline

local timeline = {}
for time = 0, 8 do
-- insert time of this keyframe into the timeline table
table.insert(timeline, time)
-- insert position into timeline table using some maths to make a circle
table.insert(timeline,
Vector.New(
100 * math.cos(time * math.pi / 4),
-100 * math.sin(time * math.pi / 4),
0
)
)
end
self:GetEntity():PlayTimeline(timeline)

onInteract

The onInteract event is sent when a player iteracts with the entity.

ExampleScript.Properties = {
{ name = "entityToInteractWith", type = "entity" }
}

function ExampleScript:Init()
self.properties.entityToInteractWith.onInteract:Listen(self, "HandleInteraction")
end

function ExampleScript:HandleInteraction(player, hitResult, entity)
print("The player that interacted was " .. player:GetName())
print("The interaction was at " .. hitResult:GetPosition)
print("The entity that was interacted with was " .. entity:GetName())
end
© 2024 CraytaKit