Script | CraytaKit
Skip to main content

Script

Functions

Function NameReturn TypeDescriptionTags
GetProperties()PropertiesAlternative to self.properties, this gets the bag of values produced from the property editor for this script on this Entity. The properties are defined by the static Properties table on the table returned by the Lua scriptNone
RevertClientProperty(string propertyName)NoneRevert a property that's been changed on the client back to the server's value for itClient Only
GetEntity()EntityReturns the Entity that the script is attached toNone
GetScriptAsset()ScriptAssetReturns the ScriptAsset this is an instance ofNone
ListenForEvent(string eventName, Script listenerScriptComponent)NoneTell this script that listenerScriptComponent wants to be informed when it sounds eventName using SendEventToListeners. eventName will be called on the listenerScriptComponent scriptNone
ListenForEvent(string eventName, Script listenerScriptComponent, string functionName)Tell this script that listenerScriptComponent wants to be informed when it sounds eventName using SendEventToListeners. functionName will be called on the listenerScriptComponent scriptNone
SendEventToListeners(string eventName, ...)NoneCall eventName on any scripts that have registered for it using ListenForEvent with the given args. If called on the server do it only on the server, if called on a client do it only on that clientNone
Schedule(function callback)ScheduleHandlePass this a function to do that function in a thread. Can use globals like Wait to control flow as the function will be re-entrantNone
Cancel(ScheduleHandle handle)NoneCancel a scheduled task if its runningNone
SetSaveData(table saveData)NoneSet the save data for this script to the table supplied. The script must be owned by a User or PlayerServer Only
GetSaveData(function callback)NoneGet the save data previously written out with SetSaveData on this script. This function is asynchronous and will call the callback function when its finished with the save data as an argumentDeprecated, Server Only
GetSaveData()tableGet the save data previously written out with SetSaveData on this script. This function returns the save data immediatelyServer Only
SendToScript(string eventName, ...)NoneCall eventName on this script if it exists, with the given argsNone
SendToAllClients(string eventName, ...)NoneCall eventName on this script on all clients currently connected to the server with the given args. Note, this function call can not guarantee that entities are all in a ready state on the client at the time of call, and might therefore miss events during constructionServer Only
SendToServer(string eventName, ...)NoneCall eventName on this script on the server. Implicitly passes the local user as the last parameter.Local Only
SendToLocal(string eventName, ...)NoneCall eventName on this script on the client that owns the Player or User this script is attached toServer Only

Entry Points

These methods are called at specific points in a game's lifecycle, if present in a script.

-- This method is called when the script is initialized on the server
function ExampleScript:Init()

end
Entry Point NameReturn TypeDescriptionTags
OnInteract(Character player, HitResult hitResult)NoneCalled when a player interacts with an entity on all scripts of the entityServer Only
OnCollision(Entity entity)NoneCalled when a player collides with an entity on all scripts of the entity. Also calls this function on the player's scripts with the entity as the argumentServer Only
OnDamage(number amount, Entity causer, HitResult hitResult)NoneCalled when an entity is damaged on all scripts of the entityServer Only
Init()NoneCalled to initialize a script on the serverServer Only
ClientInit()NoneCalled to initialize a script on the clientClient Only
LocalInit()NoneCalled to initialize a script on the client that owns this entityLocal Only
OnTick(number deltaTime)NoneCalled each frame on the server, passed the time in seconds since the last callServer Only
ClientOnTick(number deltaTime)NoneCalled each frame on the client, passed the time in seconds since the last callClient Only
LocalOnTick(number deltaTime)NoneCalled each frame on the client that owns this script, passed the time in seconds since the last callLocal Only
OnUserLogin(User user)NoneCalled when a new user joins the gameServer Only
OnUserLogout(User user)NoneCalled when a user leaves the gameServer Only
OnDeathPlaneTrigger()NoneCalled on the player when the player is below the death planeServer Only
OnTriggerEnter(Entity entity)NoneCalled by a trigger component when an entity enters the trigger volumeServer Only
OnTriggerExit(Entity entity)NoneCalled by a trigger component when an entity leaves the trigger volumeServer Only
OnButtonPressed(string buttonName)NoneCalled on player and user scripts when a particular button is pressed, giving the string name of the buttonServer Only
OnButtonReleased(string buttonName)NoneCalled on player and user scripts when a particular button is released, giving the string name of the buttonServer Only
OnIronSightStart()NoneCalled when the character goes in to Iron Sight modeServer Only
OnIronSightStop()NoneCalled when the character stops Iron Sight modeServer Only
OnSprintStart()NoneCalled when the character starts sprintingServer Only
OnSprintStop()NoneCalled when the character stops sprintingServer Only
OnCrouch()NoneCalled when the character crouchesServer Only
OnStand()NoneCalled when the character standsServer Only
OnJump()NoneCalled when the character jumpsServer Only
LocalOnButtonPressed(string buttonName)NoneCalled locally on player and user scripts when a particular button is pressed, giving the string name of the buttonLocal Only
LocalOnButtonReleased(string buttonName)NoneCalled locally on player and user scripts when a particular button is released, giving the string name of the buttonLocal Only
LocalOnIronSightStart()NoneCalled when the character goes in to Iron Sight modeLocal Only
LocalOnIronSightStop()NoneCalled when the character stops Iron Sight modeLocal Only
LocalOnSprintStart()NoneCalled when the character starts sprintingLocal Only
LocalOnSprintStop()NoneCalled when the character stops sprintingLocal Only
LocalOnCrouch()NoneCalled when the character crouchesLocal Only
LocalOnStand()NoneCalled when the character stands from crouchLocal Only
LocalOnJump()NoneCalled on the player when a jump action happenedLocal Only
OnHotbarChanged(number hotbarIndex)NoneCalled on server when the user's hotbar index changes, either by using the previous and next buttons or using the hotbar keys on a keyboard. Calls function on all user and player scriptsServer Only
OnChatMessage(User user, Text message)NoneCalled when a quick chat message is triggered by a userClient Only
OnDestroy()NoneCalled on the server when an entity is destroyedServer Only
LocalOnMantleStart()NoneCalled when the character starts to mantle up to a platformLocal Only
OnMantleStart()NoneCalled when the character starts to mantle up to a platformServer Only
LocalOnMantleStop()NoneCalled when the character stops to mantling up to a platformLocal Only
OnMantleStop()NoneCalled when the character stops to mantling up to a platformServer Only
LocalOnSlideStart()NoneCalled when the character starts to slideLocal Only
OnSlideStart()NoneCalled when the character starts to slideServer Only
LocalOnSlideStop()NoneCalled when the character stops slidingLocal Only
OnSlideStop()NoneCalled when the character stops slidingServer Only
LocalOnRollStart()NoneCalled when the character starts to rollLocal Only
OnRollStart()NoneCalled when the character starts to rollServer Only
LocalOnRollStop()NoneCalled when the character stops rollingLocal Only
OnRollStop()NoneCalled when the character stops rollingServer Only
LocalOnSpawnEffectEnded()NoneCalled when the character spawn effect has endedLocal Only
OnSpawnEffectEnded()NoneCalled when the character spawn effect has endedServer Only
OnActivityTriggered(string id, Text display, string category)NoneCalled when an activity is triggeredNone
OnFirstPersonStarted()NoneCalled when the local camera enters first person modeLocal Only
OnFirstPersonEnded()NoneCalled when the local camera exits first person modeLocal Only

Examples

OnUserLogin

When OnUserLogin is registered, it disables automatically spawning the player.


ExampleScript.Properties = {
{ name = "spawnLocator", type = "entity" },
{ name = "playerTemplate", type "template" }
}

function ExampleScript:OnUserLogin(user)
user:SpawnPlayerWithEffect(self.properties.playerTemplate, self.properties.spawnLocator)
end

Schedule

Spawn an enemy every 10 seconds

ExampleScript.Properties = {
{ name = "enemySpawnFrequency", type = "number", default = 10 },
{ name = "enemyTemplate", type = "template" }
}

function ExampleScript:SpawnEnemies()
self.enemySpawnScheduleHandle = self:Schedule(function()
while true do
Wait(self.properties.enemySpawnFrequency)

local enemyObj = GetWorld():Spawn(self.properties.enemyTemplate, self:GetEntity():GetPosition(), self:GetEntity():GetRotation())
end
end)
end

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:SendToServer("Test")
end

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

return ExampleScript
© 2024 CraytaKit