"How do I ZScript?"

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!)
Locked
User avatar
Zergeant
Posts: 108
Joined: Tue Aug 31, 2010 7:19 am
Location: Sweden

Re: "How do I ZScript?"

Post by Zergeant »

Is there any function to scan the player's view for targets and then return valid targets in an array? Trying to create a weapon that targets several enemies at once. Or is there a better/smarter way to go about this than an array?
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: "How do I ZScript?"

Post by Matt »

So I ended up writing this:

Code: Select all

...
	void drawwepcounter(
		int input,
		int posx,int posy,
		string zero="",string one="",string two="",string three="",
		string four="",string five="",string six=""
	){
		string types[7]={ //let's pretend this works for brevity's sake
			zero,one,two,three,four,five,six
		};
		...
		if(result!="")drawimage(result,...
Is there a way to do this without keeping all those inputs twice, but also not requiring me to enter all 7 values each and every time?
Last edited by Matt on Sat Jul 08, 2017 1:40 am, edited 1 time in total.
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: "How do I ZScript?"

Post by Blue Shadow »

What are you trying to achieve from that?
User avatar
Major Cooke
Posts: 8212
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: GZBoomer Town
Contact:

Re: "How do I ZScript?"

Post by Major Cooke »

Vaecrius: Make a struct and have the struct pass in as the parameter of your function.

Example:

Code: Select all

Struct D4ManualLootParameters
{
    private bool initialized;
    Class<Actor> mo;
    int UpgradeDropChance, UpgradeDropAlways;
    int CrateDropChance, CrateDropAmount, CrateDropAlways;
    int CashChainsawPinata, CashAmount;

    void Init(Class<Actor> th = null, int upchance = -1, int updrop = 0, int cdchance = 0, int cdamt = 0, int cddrop = 0, int ccp = 0, int ca = 0)
    {
        mo = th;
        UpgradeDropChance = upchance;
        UpgradeDropAlways = updrop;
        CrateDropChance = cdchance;
        CrateDropAmount = cdamt;
        CrateDropAlways = cddrop;
        CashChainsawPinata = ccp;
        CashAmount = ca;
        initialized = true;
    }
    
    bool CheckInit()
    {
        return initialized;
    }
}

// Now have a function accept it somewhere on an actor.
// You only need 'in out' if you intend on modifying it directly with the function. 'in' is required for passing structs last I tried.
void Add(in out D4ManualLootParameters Loot)
{
//...
}
 
Or you could have it all be just 'in out' in your code but that might get a little messy.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: "How do I ZScript?"

Post by Matt »

Thanks, MC, though it looks like it'll necessarily be a few more steps than what I've got.

I'm trying to set up a function that reads an integer and displays a different sprite depending on the value. It's mostly for weapon fire settings so I almost never have more than 3 or 4 (including "not applicable" so practically only 3).
User avatar
zrrion the insect
Posts: 2432
Joined: Thu Jun 25, 2009 1:58 pm
Location: Time Station 1: Moon of Glendale

Re: "How do I ZScript?"

Post by zrrion the insect »

I am attempting to check the inventory of every player but I am getting the error "Return type mismatch." What could be causing that?

Code: Select all

for (int i = AAPTR_PLAYER1; i < AAPTR_PLAYER8; i << 1)
{
	if (CountInv("clip", i) >= 3)
	{
		//thing
		break;
	}
	else
	{
		//other thing
	}
}
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: "How do I ZScript?"

Post by Gutawer »

We'll need to see the entire function to determine that, since return type mismatch errors can only really be debugged when the return type is known.
User avatar
zrrion the insect
Posts: 2432
Joined: Thu Jun 25, 2009 1:58 pm
Location: Time Station 1: Moon of Glendale

Re: "How do I ZScript?"

Post by zrrion the insect »

It is returning names, I forgot to return a name outside of any for/if and now that I've put that there it is working correctly.

Code: Select all

override name myname()
{
	for (int i = AAPTR_PLAYER1; i < AAPTR_PLAYER8; i << 1)
	{
		if (CountInv("clip", i) >= 3)
		{
			return 'name';
			break;
		}
		else
		{
			return 'othername';
		}
	}
	return 'finalname';//I forgot to include this one, forgetting this breaks it
}
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: "How do I ZScript?"

Post by Matt »

How do you read a user cvar from inside a statusbar (or anything with a ui scope)?
User avatar
Nash
 
 
Posts: 17501
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: "How do I ZScript?"

Post by Nash »

Code: Select all

class MyHUD : BaseStatusBar
{
    override void NewGame()
    {
        if (CPlayer)
        {
            AttachToPlayer(CPlayer);
        }
    }
    
    void DrawSomething(void)
    {
        if (CPlayer && CPlayer.mo)
        {
            // check a CVar
            CVar myCVar = CVar.GetCVar("cl_mycvar", CPlayer);
            if (myCVar)
            {
                // do stuff
            }
    }
}
 
CPlayer is a PlayerInfo, in case you're not using a status bar.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: "How do I ZScript?"

Post by Matt »

Thanks, I'll give it a try.
EDIT: Doesn't work. Always returns true if found. Is there any way to get the actual value?


Is there a way to rotate an image in a status bar (ideally arbitrarily but 90-degrees would be helpful too)?
User avatar
Rip and Tear
Posts: 187
Joined: Tue May 02, 2017 3:54 pm

Re: "How do I ZScript?"

Post by Rip and Tear »

Is it possible to create a custom action function that is available to all actors?
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: "How do I ZScript?"

Post by Blue Shadow »

Mark the function as static and pass the actor to it as a parameter. Example:

Code: Select all

class Blah : Actor
{
    static int GetBlah (Actor mobj)
    {
        return mobj != null ? mobj.health + Random(1, 10) : 0;
    }
}

class DoomImp2 : DoomImp
{
    override void PostBeginPlay ()
    {
        Super.PostBeginPlay();
        int blah = Blah.GetBlah(self);
        Console.Printf("%d", blah);
    }
}
User avatar
Major Cooke
Posts: 8212
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: GZBoomer Town
Contact:

Re: "How do I ZScript?"

Post by Major Cooke »

Inheriting from actor isn't necessary, I don't think. You will need the play keyword though.

Alternatively a struct can be used.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: "How do I ZScript?"

Post by Matt »

1. How do I set up an inventory item that does not inherit from Weapon or Ammo (or a key) so that it would be given with ID(K)FA?


2. Is there a way to override or somehow get around an Ammo item's get-parent function so that you can have something like:

MyAmmo0:Ammo
MyAmmo1:MyAmmo0
MyAmmo2:MyAmmo0

in which MyAmmo1 and MyAmmo2 are distinct items?
Locked

Return to “Scripting”