Using ternary operator to retrieve PlayerInfo gives error

These bugs do plan to be resolved, when they can be.

Moderator: GZDoom Developers

Post Reply
User avatar
The Zombie Killer
Posts: 1528
Joined: Thu Jul 14, 2011 12:06 am
Location: Gold Coast, Queensland, Australia

Using ternary operator to retrieve PlayerInfo gives error

Post 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
Attachments
custom_cheats.pk3
(2.13 KiB) Downloaded 241 times
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Using ternary operator to retrieve PlayerInfo gives erro

Post 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];
User avatar
The Zombie Killer
Posts: 1528
Joined: Thu Jul 14, 2011 12:06 am
Location: Gold Coast, Queensland, Australia

Re: Using ternary operator to retrieve PlayerInfo gives erro

Post by The Zombie Killer »

Doing that gives me another error: "Cannot initialize non-scalar variable player here"
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Using ternary operator to retrieve PlayerInfo gives erro

Post 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.
User avatar
The Zombie Killer
Posts: 1528
Joined: Thu Jul 14, 2011 12:06 am
Location: Gold Coast, Queensland, Australia

Re: Using ternary operator to retrieve PlayerInfo gives erro

Post by The Zombie Killer »

That fixed it. I guess you can consider this bug closed then.
Post Reply

Return to “On Hold Bugs”