Page 1 of 1

Using ternary operator to retrieve PlayerInfo gives error

Posted: Sat Jun 24, 2017 6:10 am
by The Zombie Killer
Whilst working on adding custom cheats via ZScript, I encountered a rather puzzling issue:

Code: Select all

let player = pn != -1 ? players[pn] : players[consoleplayer];
let mo     = player.mo;

// the below works, the above gives a script error
//let mo     = pn != -1 ? players[pn].mo : players[consoleplayer].mo;
//let player = mo.player;  
See zscript.zsc, line 17

Re: Using ternary operator to retrieve PlayerInfo gives erro

Posted: Sat Jun 24, 2017 6:19 am
by Graf Zahl
The 'issue' is that 'players' is an array of structs and the ternary operator cannot deal with non-scalars. In this particular case the internal conversion to a pointer type occurs too late. Sadly this is not something easily fixed with how the entire system works.

Better do this:

Code: Select all

let player = players[pn != -1? pn : consoleplayer];

Re: Using ternary operator to retrieve PlayerInfo gives erro

Posted: Sat Jun 24, 2017 6:21 am
by The Zombie Killer
Doing that gives me another error: "Cannot initialize non-scalar variable player here"

Re: Using ternary operator to retrieve PlayerInfo gives erro

Posted: Sat Jun 24, 2017 6:30 am
by Graf Zahl
Damn. Then you have to give it an explicit type (i.e. replace 'let' with 'PlayerInfo'. There's a few issues in the entire system with global instance variables of native struct types because to the compiler their type is ambiguous.
I wish this had gone differently but when discussing these issues with Randi nothing came out of it. The mere idea of types that can both be used by value and by reference never was part of ZScript's design, although a necessity, so it led to some not so nice problems.

Re: Using ternary operator to retrieve PlayerInfo gives erro

Posted: Sat Jun 24, 2017 6:34 am
by The Zombie Killer
That fixed it. I guess you can consider this bug closed then.