Scripting Reference/GameObject
Comments3this wiki
< Scripting Reference
GameObject.transform & co
Edit
GameObject.transform GameObject.modelRenderer GameObject.mapRenderer GameObject.camera GameObject.physics
Those aren't functions (so you don't need to call them with parentheses), they are properties that contain the object's first component for a particular type.
As such, self.gameObject.modelRenderer is a shorthand for self.gameObject:GetComponent( "ModelRenderer" )
Example: Printing an object's position
Edit
function Behavior:Update()
local pos = self.gameObject.transform:GetPosition()
print( pos )
end
GameObject:GetComponent
Edit
[table] GameObject:GetComponent( [string] component type )
Returns the game object's (first) component of the specified type (or nil if none were found).
Valid component type strings are: "Transform", "Camera", "ModelRenderer", "MapRenderer", "ScriptedBehavior" and "Physics".
GameObject:CreateComponent
Edit
[Component] GameObject:CreateComponent( [string] component type )
Creates a new component. The component type can be any of: "ModelRenderer", "MapRenderer", "Camera" or "Physics".
To create scripted behaviors, use GameObject:CreateScriptedBehavior
GameObject:GetScriptedBehavior
Edit
[ScriptedBehavior] GameObject:GetScriptedBehavior( [Script] script )
Returns the scripted behavior component (aka. script instance) with the specified script.
To find scripts, you can use CraftStudio.FindAsset.
Note: This function's parameters have changed with the scripting API version 4. See Scripting API Versioning for details.
GameObject:CreateScriptedBehavior
Edit
[Component] GameObject:CreateScriptedBehavior( [Script] script to use, [table] properties=nil )
Creates a new ScriptedBehavior component.
To find scripts, you can use CraftStudio.FindAsset.
Starting with CraftStudio Beta, you can optionally pass a table of properties to add to the newly created scripted behavior. The properties will be added to the scripted behavior before Awake is called on it so it's useful for customizing a scripted behavior's initialization.
Example: Create a new scripted behavior on an existing object
Edit
local obj = CraftStudio.FindGameObject( "Some object" )
local scriptToUse = CraftStudio.FindAsset( "Some script" )
obj:CreateScriptedBehavior( scriptToUse, { Health=10, Ammo=3 } )
GameObject:GetName, GameObject:SetName
Edit
[string] GameObject:GetName() GameObject:SetName( [string] new game object's name )
Gets or sets the game object's name.
GameObject:GetParent, GameObject:SetParent
Edit
[GameObject] GameObject:GetParent() GameObject:SetParent( [table] game object ) GameObject:SetParent( [table] game object, [boolean] keep local transform )
Gets or sets the game object's parent. GetParent returns nil if the game object has no parent.
SetParent can optionally carry over the game object's local transform instead of the global one.
GameObject:FindChild
Edit
GameObject:FindChild( [string] name ) GameObject:FindChild( [string] name, [boolean] recursive )
Look for a child game object with the specified name, optionally looking for all descendants instead of just immediate children.
GameObject:GetChildren
Edit
[table] GameObject:GetChildren()
Returns the list of all direct children for the game object.
Example: Count direct children
Edit
local children = self.gameObject:GetChildren() print( self.gameObject:GetName() .. " has " .. #children .. " children" )
GameObject:SendMessage
Edit
GameObject:SendMessage( [string] method name, [table] data )
Tries to call a method with the specified name on all the scripted behaviors attached to the game object. The data argument can be nil or a table you want the method to receive as its first (and only) argument.
If none of the scripted behaviors attached to the game object have a method matching the specified name, nothing happens.
Example: Sending a basic message
Edit
Receiver script:
function Behavior:SayHi( data ) print( "Hi " .. data.name .. "!" ) end
Sender script:
CraftStudio.FindGameObject( "some object" ):SendMessage( "SayHi", { name="CraftStudio" } )