Accessing components and scripts
How to get components
As you know, the components are attached to the game object, therefore you need to have access to the GameObject which your intended component is attached to. This can be accomplished in multiple ways, the easiest being using the Serialized Fields, or maybe accessing it directly in game from collision, etc.
To access the component from a game object, you can use the following methods which are in GameObject API:
GameObject
.GetComponent(type)
GameObject
.GetComponentInChildren(type)
GameObject
.GetComponentInParent(type)
GameObject
.GetComponents(type)
GameObject
.GetComponentsInChildren(type)
GameObject
.GetComponentsInParent(type)
GameObject
.TryGetComponent(type)
The components themselves also have Similar methods.
For Example, to access the Rigidbody component of an object objA
you can use the following method:
local rigidBody = objA.GetComponent(Rigidbody);
Lua Scripts As Components
Every Lua script also behaves like a component, therefore allowing scripts to add or access scripts to and from any game object (self or other).
It means that the following methods also can be used to add or access other scripts.
GameObject
.AddComponent
GameObject
.GetComponent
GameObject
.GetComponentInChildren
GameObject
.GetComponentInParent
GameObject
.TryGetComponent
Component
.GetComponent
Component
.GetComponentInChildren
Component
.GetComponentInParent
Component
.TryGetComponent
Also, both gameobejct and component have the following method:
GetAllLuaScripts()
This method returns an array of all Lua scripts that attached to the game object.
Example
Lets say that we have created a Lua Script called ("Door.Lua") and it is somewhere in our project. We also added this script to a game object A
.
do
local door = LUA.script;
local door.DoorState = "open"; -- adding a field to the door
function door.SayHello()
Debug.Log("Hello");
end
function door.Start()
end
function door.Update()
end
end
Now let say we want to access this script from Object B
and we use a Serialized Field to get object A
do
local script = LUA.script;
local objA = SerializedField("A", GameObject);
function door.Start()
-- access the script Door in object A
local doorLuaScript = objA.GetComponent(Door);
-- Now you can use the doorScript like script.
Debug.Log(doorLuaScript.DoorState) -- should print "open";
-- call the function
doorLuaScript.SayHello(); -- will print "Hello"
end
end
Note: When using any get/add component method, for the type parameter write the name of the Lua File without extension.
Not a string! just the a name of file
So for a Lua File "Test.lua", we use following:
GetComponent(Test);
or AddComponent(Test);
, etc.
Important facts
Getting the script table of another script means:
- Not only you can get and set the existing value or use functions, you can also add more fields and functions to it.
- In order to a field to be accessible to other scripts, it needs to be attached to the script object. Same discussion here.
The limitation this imposes
Similar to class names in C#, This imposes a mandate that every Lua script file name in a project must be unique (doesn't matter if in separate directories) . Currently the Editor SDK allows you to have multiple script names, and they will perform as usual, but when using the GetComponent there will be ambiguity.
You can extend the name of the Lua files to imitate a C# style namespace, Like
MyGame_SceneObjects_DoorController.lua
;
and you access it like
GetComponent(MyGame_SceneObjects_DoorController);