LordGolias/sqf

_thisEventHandler and _thisScript

Closed this issue · 5 comments

_thisEventHandler and _thisScript are variables privatised by the engine just like the _this variable.

_thisEventHandler is passed to all event handler code.
https://community.bistudio.com/wiki/addEventHandler

_thisScript is passed to code executed using spawn of execVM.
https://community.bistudio.com/wiki/spawn
https://community.bistudio.com/wiki/execVM

As such they need to be recognised by the analyser as already privatised in the current scope (just like _this).

To check that I understood, the code for a test case for this would be

    def test_thisScript(self):
        code = '[] spawn {x = _thisScript}'
        analyser = analyze(parse(code))
        self.assertEqual(len(analyser.exceptions), 0)

which tests that _thisScript is defined within a spawn code.

Can you suggest an example for the other case?

I belive this is semi-related:

We sometimes use code that expects similar behavior.
example: https://github.com/CBATeam/CBA_A3/blob/master/addons/hashes/fnc_hashEachPair.sqf#L47
Code isn't passed anything, the upper scope values _key and _value are just expected to be used.

In ace we had a very basic private var detection script that would use this

//IGNORE_PRIVATE_WARNING ["_unit", "_weapon",.....

to skip warning about those privates.

I have not looked into how difficult something like that would be to implement.

@PabstMirror yep, that makes sense. If CBA_A3 is already using that convention, then maybe we can adopt it. We could also just add an option to choose the token to list locals to ignore.

@PabstMirror, this was implemented in b181633.

Essentially, codes below gives no errors:

//IGNORE_PRIVATE_WARNING ["_unit"];
_unit = 2;
//USES_VARIABLES ["_unit"];
_unit = 2;

I called this fixed in 2078153 since it solves the _thisScript. The event handler is a bit trickier, so I will create a separate issue for it.