Archive 17/01/2023.

How to access other script component from a script component?

jjy

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?

Eugene

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

jjy

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?

JTippetts1

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

jjy

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.

Eugene

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

JTippetts1

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.