Passing player variables to weapon

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Passing player variables to weapon

Postby Se7eNytes » Thu Nov 07, 2019 10:33 pm

I am casting a player class to allow its variables to be accessed by a weapon. Is there a way I can increase the scope of the Actor pointer so I don't have to redefine it in every anonymous function?
Spoiler: Weapon class with pointer to player


What doesn't seem to work is defining the Actor pointer outside an anonymous function, which causes "unknown identifier" errors:
Spoiler: Defining "player" outside an anonymous function
User avatar
Se7eNytes
 
Joined: 02 Mar 2010
Location: VIOLET SYSTEMS
Discord: Moon#5126

Re: Passing player variables to weapon

Postby Boondorl » Fri Nov 08, 2019 1:13 am

If walking is a custom variable then your player variable in the second code snippet won't see it because you're storing it as a regular Actor pointer. You're also calling the wrong pointer in your DoEffect() function.
Spoiler:

The reasoning is a bit complex, but essentially when you call any function from a weapon's states it's actually being called from the player, hence why there's a difference between the self pointer and invoker pointer. When you call a weapon's custom or virtual function that does not have the action keyword, however, the self pointer will refer to the weapon itself. Owner is the pointer you'll be looking for in these cases. Another quirk of this is that in order to access a weapon's variable in one of its states, you'll have to use the invoker pointer e.g. invoker.player would access the weapon's player variable.
Spoiler:

Ultimately, you can just call AstralPlayer(self).walking from a weapon's state to quickly access the variable.
Spoiler:
User avatar
Boondorl
 
Joined: 11 Jul 2018

Re: Passing player variables to weapon

Postby Se7eNytes » Fri Nov 08, 2019 3:29 pm

Hm, the first method seems to cause a VM abort (read from address zero) depending on where it's called:
Spoiler:


However, the second method works in either place, thank you:
Spoiler:
User avatar
Se7eNytes
 
Joined: 02 Mar 2010
Location: VIOLET SYSTEMS
Discord: Moon#5126


Return to Scripting

Who is online

Users browsing this forum: No registered users and 1 guest