Fandom

CraftStudio Wiki

Scripting Reference/GameObject

< Scripting Reference

56pages on
this wiki
Add New Page
Comments3 Share

GameObject.transform & coEdit

GameObject.transform
GameObject.modelRenderer
GameObject.mapRenderer
GameObject.camera
GameObject.physics
GameObject.textRenderer

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 positionEdit

function Behavior:Update()
    local pos = self.gameObject.transform:GetPosition()
    print( pos )
end

GameObject:GetComponentEdit

[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:CreateComponentEdit

[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:GetScriptedBehaviorEdit

[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:CreateScriptedBehaviorEdit

[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 objectEdit

​local obj = CraftStudio.FindGameObject( "Some object" )
local scriptToUse = CraftStudio.FindAsset( "Some script" )

obj:CreateScriptedBehavior( scriptToUse, { Health=10, Ammo=3 } )

GameObject:GetName, GameObject:SetNameEdit

[string] GameObject:GetName()
GameObject:SetName( [string] new game object's name )

Gets or sets the game object's name.

GameObject:GetParent, GameObject:SetParentEdit

[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:FindChildEdit

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:GetChildrenEdit

[table] GameObject:GetChildren()

Returns the list of all direct children for the game object.

Example: Count direct childrenEdit

 local children = self.gameObject:GetChildren()
 print( self.gameObject:GetName() .. " has " .. #children .. " children" )

GameObject:SendMessageEdit

​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 messageEdit

Receiver script:

 function Behavior:SayHi( data )
   print( "Hi " .. data.name .. "!" )
 end

Sender script:

 CraftStudio.FindGameObject( "some object" ):SendMessage( "SayHi", { name="CraftStudio" } )

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.