zscript problem with custom function - defaults to first

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.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
ramon.dexter
Posts: 1562
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

zscript problem with custom function - defaults to first

Post by ramon.dexter »

Well, ho wcould I describe my problem?
I tried making a medical item use animation using a fake weapon that plays the animation, then returns back to previosly selected weapon. Basicaly the same approach as used in NTMAI with the keycard use. The point is, it doesnt return to my previously selected weapon, but to the first weapon (weapon in slotNumber 01). Zscript is not really my strength here, so I really don't know if I'm doing something wrong here.

Here is code for the mentioned item:

Code: Select all

class hyposprej_heal : Health {
	Default {
		inventory.amount 25;
	}
}
class hyposprej_item : CustomInventory {
	Default {
		//$Category "Health and Armor/WoS"
		//$Title "Hyposprej"
		-SOLID
		+SHOOTABLE
		+NODAMAGE
		+NOBLOOD
		+CANPASS
		+INVENTORY.INVBAR
		
		Tag "$T_HYPOSPREJ";
		height 16;
		radius 14;
		//scale 0.65;
		
		Inventory.MaxAmount 20;
		Inventory.InterHubAmount 20;
		Inventory.PickupMessage "$F_HYPOSPREJ";
		Inventory.Icon "I_MED1";
		//Inventory.useSound "sounds/med";
	}
	
	States {
		Spawn:
			MED1 V -1;
			Stop;
		Use:
			TNT1 A 0 A_GiveInventory("dx_applyHyposprej", 1);
			Stop;
	}
}
class dx_applyHyposprej : weapon {

	class<weapon> selectedWeapon;

	Default {
        +Inventory.Undroppable
    }

	States {
		Select:
			AMHS A 1 A_Raise(10);
			Loop;
		Ready:
			AMHS A 1 A_WeaponReady;
		Fire:
		ApplyMed:
            AMHS BCDEFGH 2;
			AMHS I 2 A_Playsound("sounds/med");
            AMHS J 2 A_GiveInventory("hyposprej_heal", 1); //do stuff
		Deselect:
			AMHS K 1 A_Lower(9);
			AMHS K 0 A_SelectPrevWeapon();
			Stop;
	}
    action void A_SelectPrevWeapon() {
        
		let owner = binderPlayer(invoker.owner);
		owner.takeInventory("dx_applyHyposprej", 1);
		A_SelectWeapon(invoker.selectedWeapon, SWF_SELECTPRIORITY);
    }	
}
Does anybody know what I'm doing wrong here and why it the A_SelectWeapon() returns to the first weapon?
Jarewill
 
 
Posts: 1853
Joined: Sun Jul 21, 2019 8:54 am

Re: zscript problem with custom function - defaults to first

Post by Jarewill »

For one, you don't set selectedWeapon to the previous weapon of the player.
For two, you take away the weapon instead of letting it deselect normally. So any switching doesn't work.

Also you only give the player the weapon without switching to it, so if the player plays with switch on pickup disabled, the item won't work.
And that Deselect state looks worrying.

How I would do this is to define selectedWeapon in the player class, instead of the weapon.
Then I would change the item's use state to this:

Code: Select all

      Use:
         TNT1 A 0
         {
                  let playe = binderPlayer(self);
                  playe.selectedWeapon = Weapon(playe.player.readyweapon);
                  A_GiveInventory("dx_applyHyposprej", 1);
                  A_SelectWeapon("dx_applyHyposprej");
         }
         Stop;
And in the A_SelectPrevWeapon() function:

Code: Select all

    action void A_SelectPrevWeapon() {
       
      let playe = binderPlayer(self); //Self works like Invoker.owner when called in this context, also I don't know if using "let owner" works
      A_SelectWeapon(playe.selectedWeapon, SWF_SELECTPRIORITY);
    }
Also I don't think that Deselect state would even work. I'd change it to this:

Code: Select all

      ApplyMed:
            AMHS BCDEFGH 2;
         AMHS I 2 A_Playsound("sounds/med");
            AMHS J 2 A_GiveInventory("hyposprej_heal", 1); //do stuff
         AMHS K 0 A_SelectPrevWeapon();
         AMHS K 0 A_WeaponReady(WRF_NOFIRE);
      Deselect:
         AMHS K 1 A_Lower(9);
         Loop;
Now, you can't take away the item until the switch is done, so you will have to do it another way.
While I haven't tested this, most of the functions I took from my own mod, so it should work.
User avatar
ramon.dexter
Posts: 1562
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: zscript problem with custom function - defaults to first

Post by ramon.dexter »

Ok, I tried redefining the selectedWeapon into player class.

Now it gives me error:
Cannot convert Pointer<Class<Weapon>> to class type

on this line:

Code: Select all

class hyposprej_item : CustomInventory {
	Default {
		//$Category "Health and Armor/WoS"
		//$Title "Hyposprej"
		-SOLID
		+SHOOTABLE
		+NODAMAGE
		+NOBLOOD
		+CANPASS
		+INVENTORY.INVBAR
		
		Tag "$T_HYPOSPREJ";
		height 16;
		radius 14;
		//scale 0.65;
		
		Inventory.MaxAmount 20;
		Inventory.InterHubAmount 20;
		Inventory.PickupMessage "$F_HYPOSPREJ";
		Inventory.Icon "I_MED1";
		//Inventory.useSound "sounds/med";
	}
	
	States {
		Spawn:
			MED1 V -1;
			Stop;
		Use:
			TNT1 A 0 {
				let playe = binderPlayer(self);
				playe.selectedWeapon = Weapon(playe.player.readyWeapon); <<--error here
				A_GiveInventory("Hyposprej_apply", 1);
			}
			Stop;
	}
}
Jarewill
 
 
Posts: 1853
Joined: Sun Jul 21, 2019 8:54 am

Re: zscript problem with custom function - defaults to first

Post by Jarewill »

Oh right.
That's because the variable has to be defined as

Code: Select all

Weapon selectedWeapon;
And not

Code: Select all

class<weapon> selectedWeapon;
User avatar
ramon.dexter
Posts: 1562
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: zscript problem with custom function - defaults to first

Post by ramon.dexter »

I've modified it how you advised...

Code: Select all

in binderplayer:
Weapon selectedWeapon;

class hyposprej_heal : Health {
	Default {
		inventory.amount 25;
	}
}
class hyposprej_item : CustomInventory {
	Default {
		//$Category "Health and Armor/WoS"
		//$Title "Hyposprej"
		-SOLID
		+SHOOTABLE
		+NODAMAGE
		+NOBLOOD
		+CANPASS
		+INVENTORY.INVBAR
		
		Tag "$T_HYPOSPREJ";
		height 16;
		radius 14;
		//scale 0.65;
		
		Inventory.MaxAmount 20;
		Inventory.InterHubAmount 20;
		Inventory.PickupMessage "$F_HYPOSPREJ";
		Inventory.Icon "I_MED1";
		//Inventory.useSound "sounds/med";
	}
	
	States {
		Spawn:
			MED1 V -1;
			Stop;
		Use:
			TNT1 A 0 {
				let playe = binderPlayer(self);
				playe.selectedWeapon = Weapon(playe.player.readyWeapon);
				A_GiveInventory("Hyposprej_apply", 1);
			}
			Stop;
	}
}
class Hyposprej_apply : weapon {

	//class<weapon> selectedWeapon;

	Default {
        +Inventory.Undroppable
    }

	States {
		Select:
			AMHS L 1 A_Raise(10);
			Loop;
		Ready:
			AMHS L 1 A_WeaponReady;
		Fire:
		ApplyMed:
            AMHS ABCDEFGH 2;
			AMHS I 2 A_Playsound("sounds/med");
            AMHS J 2 A_GiveInventory("hyposprej_heal", 1); //do stuff
		Deselect:
			AMHS K 1 A_Lower(9);
			AMHS K 0 A_SelectPrevWeapon();
			Stop;
	}
    action void A_SelectPrevWeapon() {
        
		//let owner = binderPlayer(invoker.owner);
		//owner.takeInventory("dx_applyHyposprej", 1);
		//A_takeInventory("Hyposprej_apply", 1);
		//A_SelectWeapon(invoker.selectedWeapon, true);
		let playe = binderPlayer(self);
		A_SelectWeapon(playe.selectedWeapon, SWF_SELECTPRIORITY); <<--error still points to this line: Cannot convert Pointer<Class<Weapon>> to class type
    }	
}
And... still the same error.

Code: Select all

Cannot convert Pointer<Class<Weapon>> to class type
Jarewill
 
 
Posts: 1853
Joined: Sun Jul 21, 2019 8:54 am

Re: zscript problem with custom function - defaults to first

Post by Jarewill »

Aaaaaand another error of mine, sorry about that.
A_SelectWeapon should have GetClassName() added:

Code: Select all

A_SelectWeapon(playe.selectedWeapon.GetClassName(), SWF_SELECTPRIORITY);} 
User avatar
ramon.dexter
Posts: 1562
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: zscript problem with custom function - defaults to first

Post by ramon.dexter »

Great, Many thanks, Jarewill, you've saved me a lot of headache! :D
Post Reply

Return to “Scripting”