Using ternary operator to retrieve PlayerInfo gives error

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

Moderator: GZDoom Developers

Using ternary operator to retrieve PlayerInfo gives error

Postby The Zombie Killer » Sat Jun 24, 2017 7:10 am

Whilst working on adding custom cheats via ZScript, I encountered a rather puzzling issue:

Code: Select allExpand view
let player = pn != -? 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 55 times
User avatar
The Zombie Killer
King of the Kangaroos
 
Joined: 14 Jul 2011
Location: Gold Coast, Queensland, Australia
Discord: Zombie#1795

Re: Using ternary operator to retrieve PlayerInfo gives erro

Postby Graf Zahl » Sat Jun 24, 2017 7:19 am

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 allExpand view
let player = players[pn != -1? pn : consoleplayer];
User avatar
Graf Zahl
Lead GZDoom Developer
Lead GZDoom Developer
 
Joined: 19 Jul 2003
Location: Germany

Re: Using ternary operator to retrieve PlayerInfo gives erro

Postby The Zombie Killer » Sat Jun 24, 2017 7:21 am

Doing that gives me another error: "Cannot initialize non-scalar variable player here"
User avatar
The Zombie Killer
King of the Kangaroos
 
Joined: 14 Jul 2011
Location: Gold Coast, Queensland, Australia
Discord: Zombie#1795

Re: Using ternary operator to retrieve PlayerInfo gives erro

Postby Graf Zahl » Sat Jun 24, 2017 7:30 am

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
Graf Zahl
Lead GZDoom Developer
Lead GZDoom Developer
 
Joined: 19 Jul 2003
Location: Germany

Re: Using ternary operator to retrieve PlayerInfo gives erro

Postby The Zombie Killer » Sat Jun 24, 2017 7:34 am

That fixed it. I guess you can consider this bug closed then.
User avatar
The Zombie Killer
King of the Kangaroos
 
Joined: 14 Jul 2011
Location: Gold Coast, Queensland, Australia
Discord: Zombie#1795


Return to On Hold Bugs

Who is online

Users browsing this forum: No registered users and 1 guest