[ZScript] Override bool TryPickup and CVars - Ammo limits? ?

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.

[ZScript] Override bool TryPickup and CVars - Ammo limits? ?

Postby Ac!d » Mon Aug 03, 2020 12:57 am

I'm working on a functionality where if a CVar is set for a specific weapon.

Code: Select allExpand view
Class SuperShotgunReplacement : Actor replaces SuperShotgun
{
    Default
    
{
        +WEAPONSPAWN
    
}
    States
    
{
    Spawn:
        TNT1 A 0 NoDelay A_JumpIf(GetCVar("New_SSG") == 0, "Random");
        TNT1 A 0 A_JumpIf(GetCVar("New_SSG") == 1, "SuperShotgun"); // SuperShotgun only
        TNT1 A 0 A_JumpIf(GetCVar("New_SSG") == 2, "AutoShotgun"); // AutoShotgun only
        Stop;

    Random:
        TNT1 A 0 A_Jump(256, "SuperShotgun", "AutoShotgun");
        Stop;

    SuperShotgun:
        TNT1 A 0 A_SpawnItem("NewSuperShotgun");
        Stop;
    AutoShotgun:
        TNT1 A 0 A_SpawnItem("NewShotgun");
        Stop;
    }
}

// Exemple of the weapon
Class NewSuperShotgun : Weapon
{
    Default
    
{
        Weapon.SelectionOrder 400;
        Weapon.AmmoUse 2;
        Weapon.AmmoGive2 8;
        Weapon.AmmoType "SuperShotgunAmmo";
        Weapon.AmmoType2 "Shell";
        +WEAPON.NOALERT
        Inventory
.PickupMessage "$GOTSHOTGUN2";
        Obituary "$OB_MPSSHOTGUN";
        Tag "$TAG_SUPERSHOTGUN";
    }
}


Exemple : The CVar "New_SSG" is set at 2 (AutoShotgun), the class "NewShotgun" will replace the "SuperShotgun", but if someone cheat with " IDKFA / Give All ", it will not give the "NewSuperShotgun".
In the case where we enter ' Summon NewSuperShotgun ' in the console, it will give the 8 shells of the weapon when we pick up this item.


Code: Select allExpand view
override bool TryPickup (in out Actor toucher) 
    
{
        if(toucher. (" ")).
        {
            A_Log("You unloaded the New Super Shotgun ");
            return false;
        }
        else
            return Super
.TryPickup(toucher);
    
Last edited by Ac!d on Tue Sep 15, 2020 1:49 pm, edited 2 times in total.
User avatar
Ac!d
Perfection is a way, not an end. - Korean Proverb
 
Joined: 02 Apr 2019
Location: France
Discord: Ac!d#2732

Re: [ZScript] Override bool TryPickup and CVars

Postby Jarewill » Mon Aug 03, 2020 1:56 am

Is it a server CVar or a user CVar?
If it's a server CVar, something like this should work:
Code: Select allExpand view
    override bool TryPickup (in out Actor toucher) 
    
{
        if(GetCVar("New_SSG")==2)
        {
            If(self.ammogive2>0) //It will execute every tic, filling the player with shells
            {
                A_Log("You unloaded the New Super Shotgun ");
                toucher.A_GiveInventory("Shell",8);
                self.ammogive2=0; //So this is a little thing to prevent that from happening
            }
            return false;
        }
        else
            return Super
.TryPickup(toucher);
    }

If you don't want your weapon to be cheatable with IDKFA / Give all, add this flag to your weapon: +WEAPON.CHEATNOTWEAPON
Even with that flag however, the weapon will still be cheatable with the "Give everything" command.
If you really don't want your weapon to be cheatable, you can make a void DoEffect() override that checks the CVar and takes away the item.
Jarewill
 
 
 
Joined: 21 Jul 2019

Re: [ZScript] Override bool TryPickup and CVars

Postby MFG38 » Mon Aug 03, 2020 2:38 pm

Jarewill wrote:Is it a server CVar or a user CVar?
If it's a server CVar, something like this should work:
Code: Select allExpand view
    override bool TryPickup (in out Actor toucher) 
    
{
        if(GetCVar("New_SSG")==2)
        {
            If(self.ammogive2>0) //It will execute every tic, filling the player with shells
            {
                A_Log("You unloaded the New Super Shotgun ");
                toucher.A_GiveInventory("Shell",8);
                self.ammogive2=0; //So this is a little thing to prevent that from happening
            }
            return false;
        }
        else
            return Super
.TryPickup(toucher);
    



That's not proper ZScript syntax. The cvar should be retrieved like so:

Code: Select allExpand view
if(CVar.FindCVar('New_SSG').GetInt() == 2)
User avatar
MFG38
 
Joined: 14 Apr 2019
Location: Finland
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

Re: [ZScript] Override bool TryPickup and CVars

Postby Blue Shadow » Mon Aug 03, 2020 2:57 pm

MFG38 wrote:That's not proper ZScript syntax.

What's used there is this: https://zdoom.org/wiki/GetCVar_(DECORATE)
User avatar
Blue Shadow
 
 
 
Joined: 14 Nov 2010
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: ATI/AMD (Modern GZDoom)

Re: [Solved][ZScript] Override bool TryPickup and CVars

Postby Ac!d » Tue Aug 04, 2020 2:27 am

Thanks for all of you, I've used your improvements to made this code :

Code: Select allExpand view
Class NewSuperShotgun : Weapon
{
    override bool TryPickup (in out Actor toucher) 
    
{
        if(CVar.FindCVar("New_SSG").GetInt() == 2)
        {
            If(self.ammogive2>0) //It will execute every tic, filling the player with shells
            {
                A_Log("$UNLOADSHOTGUN2");
                toucher.A_GiveInventory("Shell",8);
                A_StartSound("misc/w_pkup", CHAN_WEAPON);
                A_FadeOut(1);
                self.ammogive2=0; //So this is a little thing to prevent that from happening
            }
            return false;
        }
        else
            return Super
.TryPickup(toucher);
    }

    override void DoEffect ()
    {
        Super.DoEffect();

        if(CVar.FindCVar("New_SSG").GetInt() == 2)
        {
            A_TakeInventory("NewSuperShotgun");
        }
    }


I've implemented A_FadeOut() to make the spawn sprite of the weapon vanish when picked up.
User avatar
Ac!d
Perfection is a way, not an end. - Korean Proverb
 
Joined: 02 Apr 2019
Location: France
Discord: Ac!d#2732

Re: [ZScript] Override bool TryPickup and CVars - Ammo limit

Postby Ac!d » Tue Sep 15, 2020 2:07 pm

I have an other problem :

When my ammo capacity is at his maximum (with and without backpack), I can still pickup up a weapon.
Exemple : my mod Ac!d's Eriguns have an option to select between the gatling gun (always dropped by chaingunners) and the nailgun or both. When I have only the nailgun and maximum ammo, I can still pickup a gatling gun for his ammo. (both of theses guns use clip as ammo)

What should I wrote in the override bool TryPickup to stop this problem ?
User avatar
Ac!d
Perfection is a way, not an end. - Korean Proverb
 
Joined: 02 Apr 2019
Location: France
Discord: Ac!d#2732

Re: [ZScript] Override bool TryPickup and CVars - Ammo limit

Postby Ac!d » Tue Sep 29, 2020 10:23 am

This is the code for explain my previous post :

Code: Select allExpand view

Class EriGrenadeLauncher 
: EriWeapon
{
    override bool TryPickup (in out Actor toucher) 
    
{
        if(CVar.FindCVar("Eri_RL").GetInt() == 1 || CVar.FindCVar("Eri_RL").GetInt() == 3 || CVar.FindCVar("Eri_RL").GetInt() == 5)
        // Eri_RL : 1 => gives RocketLauncher / 3 => gives MissileLauncher / 5 => gives RocketLauncher or MissileLauncher 
        {
            If(self.ammogive1>0) // If the ammo capacity is at his maximum (RocketAmmo for example) with and without backpack, we can still pickup up a weapon.
            {
                A_Log("$UNLOADGRENADE");
                toucher.A_GiveInventory("RocketAmmo",4);
                A_StartSound("misc/w_pkup", CHAN_WEAPON);                
                A_FadeOut
(1);
                self.ammogive1=0;
            }
            return false;
        }
        else
            return Super
.TryPickup(toucher);
    }

    override void DoEffect ()
    {
        Super.DoEffect();

        if(CVar.FindCVar("Eri_RL").GetInt() == 1 || CVar.FindCVar("Eri_RL").GetInt() == 3 || CVar.FindCVar("Eri_RL").GetInt() == 5)
        // Eri_RL : 1 => gives RocketLauncher / 3 => gives MissileLauncher / 5 => gives RocketLauncher or MissileLauncher 
        {
            A_TakeInventory("EriGrenadeLauncher");
        }
    }

    Default
    
{
        Weapon.SelectionOrder 2500;
        Weapon.AmmoUse 1;
        Weapon.AmmoGive 4;
        Weapon.AmmoType "RocketAmmo";
        +WEAPON.NOALERT
        
+WEAPON.NOAUTOFIRE
        
+WEAPON.EXPLOSIVE
        Inventory
.PickupMessage "$GOTGRENADE";
        Tag "$TAG_GRENADE";
    }
 
User avatar
Ac!d
Perfection is a way, not an end. - Korean Proverb
 
Joined: 02 Apr 2019
Location: France
Discord: Ac!d#2732


Return to Scripting

Who is online

Users browsing this forum: No registered users and 1 guest