[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.

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
Ac!d
Posts: 345
Joined: Tue Apr 02, 2019 5:13 am
Location: France

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

Post by Ac!d »

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

Code: Select all

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 all

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.
Jarewill
 
 
Posts: 1766
Joined: Sun Jul 21, 2019 8:54 am

Re: [ZScript] Override bool TryPickup and CVars

Post by Jarewill »

Is it a server CVar or a user CVar?
If it's a server CVar, something like this should work:

Code: Select all

    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.
User avatar
MFG38
Posts: 414
Joined: Sun Apr 14, 2019 8:26 am
Graphics Processor: nVidia (Modern GZDoom)
Location: Finland
Contact:

Re: [ZScript] Override bool TryPickup and CVars

Post by MFG38 »

Jarewill wrote:Is it a server CVar or a user CVar?
If it's a server CVar, something like this should work:

Code: Select all

    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 all

if(CVar.FindCVar('New_SSG').GetInt() == 2)
Blue Shadow
Posts: 4949
Joined: Sun Nov 14, 2010 12:59 am

Re: [ZScript] Override bool TryPickup and CVars

Post by Blue Shadow »

MFG38 wrote:That's not proper ZScript syntax.
What's used there is this: https://zdoom.org/wiki/GetCVar_(DECORATE)
User avatar
Ac!d
Posts: 345
Joined: Tue Apr 02, 2019 5:13 am
Location: France

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

Post by Ac!d »

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

Code: Select all

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
Posts: 345
Joined: Tue Apr 02, 2019 5:13 am
Location: France

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

Post by Ac!d »

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
Posts: 345
Joined: Tue Apr 02, 2019 5:13 am
Location: France

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

Post by Ac!d »

This is the code for explain my previous post :

Code: Select all

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";
    }
 
Post Reply

Return to “Scripting”