How to access other script component from a script component?

I just want to find a way for script components communication. I looked through the code of ScriptInstance but I don’t find any api for this except event.

It will be nice if something like the following is supported.

[lua]
local cmpt = self.node:GetComponent(“OtherScript”)
cmpt:callSomeMethod()

If it is not supported, how much work is needed to add this feature?

+1, I wanted such feature.
However, I don’t know good design for such functionality because GetComponent now searches C++ components.

After some research, it seems that Urho3d uses a shared lua state across the app. And the LuaScriptInstance creates an instance of the LuaScriptObject when the script is loaded. If we can created a global variable holding the LuaScriptObject instance, then we can access it from other script components. Variable name collision may be a problem. Some naming convention is needed. Finally, add some GetComponent methods to the LuaScriptObject class to find the right object quickly.

I’m new to Lua and Urho3d. Any suggestions?

local thingamajig=node:GetScriptObject(“WhichScriptObject”)
thingamajig:doohickey()

2 Likes

It works. Thank you.

How to reload the whole lua state in editor? I have to restart the editor every time I changed the script.

Great! This is already implemented!
I use to miss some nice Urho features…

I think that re-loading a script in Lua is still problematic. The AngelScript module has no issues with destroying/rebuilding a component on script reload, but I’m not sure they ever did really concoct a way to do the same elegantly in Lua. You can re-load the file, but I don’t believe that will affect any script objects already in existence.