I don't see any responses to it, so it may have been overlooked.
The reason i found the problem then may very well be that ShieldCycler appears to be rather unique codingwise and uses 3 types of event handlers (equipment events, shipscript events & worldscript events) .
If you only use 1 of those, you may never encounter the problem.
a few questions :
- have other oxp makers encountered this problem ?
- are there better ways to solve this then my workaround describe below ?
- does current Oolite source-code (1.80 or trunk) still have this problem ?
If the problem still exists, should it be handled in Oolite sourcecode or in OXP code ?
LVV
Lone_Wolf wrote:The discussion about global namespace
meant that CCL was updated, and that in turns meant i had to update shieldcycler.
A comment (and a pm) from Commander McLane made me look back at older shieldcycler versions.
Before version 0.21.2 i used this.sc_foo for storing/changing values, but found they somehow didn't stick.
It looked like there were multiple instances of those values instead of just 1.
In 0.21.2 & 0.21.3 i tried to solve this by using global variables that were stored in global namespace.
This however didn't solve all problems, so i looked deeper.
Reading up on scope and JS calling context, i realised that when a function was called the caller determined whether a new context was created or an existing one was used.
Looking into oolite code, it appeared that there were different contexts created for :
equipment script events, ship script events and worldscript events.
Even though the handlers for these events were all in the same script, they didn't share the same context.
I found that calling a function as a method is the easiest way to ensure what the execution context will be at runtime.
In ShieldCycler 0.30.1 i created dummy event handlers like this in a separate script :This effectively means all event handlers shieldcycler uses are now worldscript events and share the same context, thus solving the problems.Code: Select all
this.activated = function() { worldScripts["Shield Cycler"].sc_activated(); };
The reason i encountered these problems is likely the mix of equipment, shipscript and worldscript events shieldcycler needs to do its job.
I posted this here hoping my findings will be useful for other coders.