I assume Script Events are intended to be called across multiple scripts. I get a null callback error when I attempt calling a script event defined in one script from a different script. I know the script event is properly set up because it works fine when called from the script it’s defined in. I only get this error when calling it from an outside script. Any idea why this should be happening or is this the expected behavior?
I did some more testing of script events and I figured out the issue. I also found a number of bugs that exist for Lua Script Events.
For anyone who’s having the same issue, the problem was that I was declaring the script event subscriber as a local variable in OnActivate(). The table with function definitions that subscribes to the script event shouldn’t be declared using the “local” keyword, it should just be declared with a given name. After making this change, I was able to get global script events working in Lua.
However, after doing this, I still couldn’t get addressable script events working in Lua. Attempting to call an addressable script event from an outside script results in the engine crashing and closing out instantly. I also noticed that global broadcast script events with functions with more than one parameter failed to carry over any parameters aside from the first one. So my findings are as follows:
- The table that subscribes to script events cannot be declared using local keyword
- Attempting to call an addressable script event from a separate script results in the engine crashing
- Only non-addressable broadcast events can be called from an outside script
- Calling a non-addressable broadcast event with multiple parameters only carries over the first parameter
These gotcha bugs with this engine are really getting annoying.
Just found another bug. Creating a script event in Lua and setting a method return type as EntityId() also inexplicably crashes the engine.
Looking again at the documentation, I see it’s supposed to be typeid(EntityId()). After changing this the engine no longer crashes but it’s instead throwing a script error when attempting to use an addressable Script Event. Can anyone show me a working example of a cross-script implementation of an addressable Script Event in Lua?
Also, for anyone who’s attempting to use Lua Script Events,(which seems like I’m the only one) if you want to call methods from an entity script, in order for the subscribing table to call an entity method, it needs a reference to the table. This can be done using a global variable by not using the “local” keyword and assigning it to “self” somewhere in OnActivate(). The global variable can then be referenced from the subscribing table to call the entity script methods.