"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
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: "How do I ZScript?"

Post by Gutawer »

Sure can:

Code: Select all

Struct GlobalStuff {
    // not really practical but it works as a demonstration
    static int ReturnHealth(Actor mo) {
        return mo.health;
    }
}
Then, to call from an actor,

Code: Select all

GlobalStuff.ReturnHealth(self);
D2JK
Posts: 543
Joined: Sat Aug 30, 2014 8:21 am

Re: "How do I ZScript?"

Post by D2JK »

Thanks!
User avatar
kodi
 
 
Posts: 1355
Joined: Mon May 06, 2013 8:02 am

Re: "How do I ZScript?"

Post by kodi »

Is there a way to use the "let" special word with two or more actors that contain identically named variables but that do not inherit from each other or a common ancestor?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: "How do I ZScript?"

Post by Graf Zahl »

No. All 'let' does is to detect the type of the variable by examining the expression on the right hand side of the '='.
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 »

Is it possible to make a 2-dimensional Dynamic Array?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: "How do I ZScript?"

Post by Graf Zahl »

No. Due to the need of every array being backed by a native class, they can only be used with integral data types.
User avatar
AFADoomer
Posts: 1325
Joined: Tue Jul 15, 2003 4:18 pm
Contact:

Re: "How do I ZScript?"

Post by AFADoomer »

I'm trying to make a Status Bar that automatically displays information about in-range actors. I have it working, except that there doesn't appear to be a way to check line of sight in ui context... Is that intentional, or are there a bunch of functions thay just don't have clearscope? I can't think of how this would affect the play environment.

Or is there a different way I should be doing this than as part of a Status Bar?
User avatar
Jaxxoon R
Posts: 772
Joined: Sun May 04, 2014 7:22 pm

Re: "How do I ZScript?"

Post by Jaxxoon R »

Is there a way to check if two non-solid actors are "touching" (as in, the bounds of their height/radius overlap)?
User avatar
Major Cooke
Posts: 8175
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: "How do I ZScript?"

Post by Major Cooke »

Gutawer wrote:Sure can:

Code: Select all

Struct GlobalStuff {
    // not really practical but it works as a demonstration
    static int ReturnHealth(Actor mo) {
        return mo.health;
    }
}
Then, to call from an actor,

Code: Select all

GlobalStuff.ReturnHealth(self);
Even in code snippets, you should include a check for the actor actually existing first. That function right there will abort the game of mo is null.

So do this instead:

Code: Select all

return mo ? mo.health : 0;
User avatar
AFADoomer
Posts: 1325
Joined: Tue Jul 15, 2003 4:18 pm
Contact:

Re: "How do I ZScript?"

Post by AFADoomer »

Jaxxoon R wrote:Is there a way to check if two non-solid actors are "touching" (as in, the bounds of their height/radius overlap)?
Something like this for the radius check (warning, untested code):

Code: Select all

if (actor1.Distance2d(actor2) <= actor1.radius + actor2.radius) {} // Not 100% accurate because internally actors are square...  
- or -

Code: Select all

if (actor1.Distance2d(actor2) <= (actor1.radius + actor2.radius) * 1.414) {} // Not 100% accurate for the same reason - essentially calculates the radius at the diagonal of the actor (radius * sqrt(2)) and uses that instead  
- or -

Code: Select all

if(abs(actor1.pos.x - actor2.pos.x) <= actor1.radius + actor2.radius || abs(actor1.pos.y - actor2.pos.y) <= actor1.radius + actor2.radius) {} // 100% accurate for square actors  
and this for the height/z check:

Code: Select all

if (actor1.pos.z >= actor2.pos.z && actor1.pos.z <= actor2.pos.z + actor2.height) {}
User avatar
Major Cooke
Posts: 8175
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: "How do I ZScript?"

Post by Major Cooke »

This is how it's done in the engine. AFADoomer's does not account for portals.

Code: Select all

double blockdist = radius + mo.radius;
if (abs(mo.pos.x - it.Position.X) >= blockdist || abs(mo.pos.y - it.Position.Y) >= blockdist)
    continue;

// Q: Make this z-aware for everything? It never was before.
if (mo.pos.z + mo.height < pos.z || mo.pos.z > pos.z + height)
{
    if (CurSector.PortalGroup != mo.CurSector.PortalGroup)
        continue;
}
User avatar
Major Cooke
Posts: 8175
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: "How do I ZScript?"

Post by Major Cooke »

Is there a way to skip one super call? I.e. Super.Super.FunctionName();
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: "How do I ZScript?"

Post by Graf Zahl »

You have to use the actual class name instead of 'Super'.
User avatar
zrrion the insect
Posts: 2411
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 »

That helps me with an issue I was having actually! Thanks graf.
Lavender Moon
Posts: 30
Joined: Thu May 21, 2015 5:33 am

Re: "How do I ZScript?"

Post by Lavender Moon »

Is it possible to convert a string into a StateLabel at runtime? Since you can't have an array of StateLabels in ZScript, I have an array of strings. But unlike the StateLabel variables that I use ResolveState on to turn them into States, ResolveState rejects strings, and there doesn't seem to be any similar function that will take a string and convert it into a StateLabel. I'm currently having to use a lookup table to resolve this, which is annoying.

This is my current code ():

Code: Select all

Array<string> LabelFlash;

action StateLabel B_GetFlash()
{
	string temp = invoker.LabelFlash[invoker.LabelFlashNext];
	invoker.LabelFlashNext += 1;
	
	if(invoker.LabelFlashNext >= invoker.LabelFlash.Size())
	{
		invoker.LabelFlashNext = 0;
	}
	
	//return temp;
	
	if(temp == 'Pistol_Flash')			{ return 'Pistol_Flash'; }
	if(temp == 'Shotgun_Flash')			{ return 'Shotgun_Flash'; }
	if(temp == 'SuperShotgun_Flash')	{ return 'SuperShotgun_Flash'; }
	if(temp == 'Chaingun_Flash')		{ return 'Chaingun_Flash'; }
	if(temp == 'Chaingun_Flash2')		{ return 'Chaingun_Flash2'; }
	if(temp == 'RocketLauncher_Flash')	{ return 'RocketLauncher_Flash'; }
	if(temp == 'PlasmaRifle_Flash')		{ return 'PlasmaRifle_Flash'; }
	if(temp == 'PlasmaRifle_Flash2')		{ return 'PlasmaRifle_Flash2'; }
	if(temp == 'BFG9000_Flash')		{ return 'BFG9000_Flash'; }
	
	return null;
}
Locked

Return to “Scripting”