[SOLVED] [ZScript] Get currently selected weapon?

Tue Feb 08, 2022 10:07 am

So, how would I go about getting the player's currently selected weapon in ZScript? GetWeapon() apparently isn't a thing, neither is SelectedWeapon, and player.ReadyWeapon and player.mo.ReadyWeapon both result in errors that abort GZDoom's bootup process. Those are what I've thought to try so far.

I should mention that I aim to use it in a PlayerPawn-inheriting class's Tick() function to check for a weapon not being selected and removing an inventory item every few tics if so. Been trying to search for answers in a number of places but I'm not coming across any solutions.

Relevant bit of code from the player's ZScript code:

Code:
override void Tick()
{
   if (!player || !player.mo || player.mo != self)
   {
      Super.Tick();
      return;
   }
   
   int timer;
   int cooldown;
   
   if(CheckInventory("ArgentRifle_Cooldown",1)) { cooldown = 18; }
   else { cooldown = 24; }
   
   if(CountInv("ArgentRifleHeat") > 0 &&
   (timer % cooldown) == 0 &&
   player.mo.ReadyWeapon != "SGDArgentRifle") // <<< Latest attempt, resulted in error.
   {
      TakeInventory("ArgentRifleHeat",1);
   }

   timer++;
}
Last edited by MFG38 on Wed Feb 09, 2022 9:06 am, edited 1 time in total.

Re: [ZScript] Get currently selected weapon?

Tue Feb 08, 2022 10:20 am

I think players[consoleplayer].ReadyWeapon is how you do it. (consoleplayer is a global variable)

Re: [ZScript] Get currently selected weapon?

Tue Feb 08, 2022 10:46 am

7Soul wrote:I think players[consoleplayer].ReadyWeapon is how you do it. (consoleplayer is a global variable)


No luck. Bootup aborts with an "incompatible operands for != comparison" error.

Re: [ZScript] Get currently selected weapon?

Tue Feb 08, 2022 11:27 am

ReadyWeapon is not a property of PlayerPawn. It is the property of PlayerInfo.
To access it simply use
Code:
self.Player.ReadyWeapon;

self probably could be omitted, so in your "if" case:
Code:
   if(CountInv("ArgentRifleHeat") > 0 &&
   (timer % cooldown) == 0 &&
   player.ReadyWeapon != "SGDArgentRifle")


EDIT: Just a small reminder, you need to create a failsafe, to check if the "player" field is even set - Morphing and voo doo dolls might cause a VM abort in that case (not exactly sure, but you need to make failsafe's always)

Re: [ZScript] Get currently selected weapon?

Tue Feb 08, 2022 11:34 am

Virathas wrote:ReadyWeapon is not a property of PlayerPawn. It is the property of PlayerInfo.
To access it simply use
Code:
self.Player.ReadyWeapon;

self probably could be omitted, so in your "if" case:
Code:
   if(CountInv("ArgentRifleHeat") > 0 &&
   (timer % cooldown) == 0 &&
   player.ReadyWeapon != "SGDArgentRifle")


I already tried that and the result was similar. Error and execution aborted.

If no one comes up with an actually workable solution, I'll just remove that check. That'll make it work if nothing else does.

Re: [ZScript] Get currently selected weapon?

Tue Feb 08, 2022 11:42 am

Try

Code:
self.Player.ReadyWeapon.getClassName() != "SGDArgentRifle"

Re: [ZScript] Get currently selected weapon?

Tue Feb 08, 2022 11:53 am

7Soul wrote:Try

Code:
self.Player.ReadyWeapon.getClassName() != "SGDArgentRifle"


Well, that solved the execution halting due to errors, but now the problem is that the cooldown is too fast when the specified weapon is not selected. Possibly something wrong with the "timer vs. cooldown rate" check, as it seems to get ignored entirely - it's removing an ArgentRifleHeat from the inventory every single tic instead of every 18 or 24. Gonna have to look into that.

Re: [ZScript] Get currently selected weapon?

Tue Feb 08, 2022 1:33 pm

(timer % cooldown) == 0 will be true right at the start (0 % 24 = 0). Move timer++; above this check

Re: [ZScript] Get currently selected weapon?

Tue Feb 08, 2022 2:44 pm

7Soul wrote:Try

Code:
self.Player.ReadyWeapon.getClassName() != "SGDArgentRifle"


Ah, yes, forgot that in my test i used "is" keyword instead of directly checking for classname.

I need to ask however, why do you need this interaction inside "playerpawn" instead of the weapon itself?

Re: [ZScript] Get currently selected weapon?

Wed Feb 09, 2022 8:44 am

Virathas wrote:I need to ask however, why do you need this interaction inside "playerpawn" instead of the weapon itself?


Wouldn't it then only work as long as the weapon was selected if I did it there? I specifically needed a way to do the cooldown when the weapon was not selected, and doing it in PlayerPawn seemed like the obvious choice. Besides, the weapon's Ready state already has the relevant code for the cooldown system.

Anyway, I managed to get the cooldown to work as intended by doing this:

Code:
override void Tick()
{
   if (!player || !player.mo || player.mo != self)
   {
      Super.Tick();
      return;
   }
      
   super.Tick();
   
   //int timer;
   int cooldown;
   
   if(CheckInventory("ArgentRifle_Cooldown",1)) { cooldown = 18; }
   else { cooldown = 24; }

   if(CountInv("ArgentRifleHeat") > 0 &&
   level.maptime % cooldown == 0 &&
   self.player.ReadyWeapon.GetClassName() != "SGDArgentRifle")
   {
      TakeInventory("ArgentRifleHeat",1);
   }
}