ZScript Discussion

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!)
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49130
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: ZScript Discussion

Post by Graf Zahl »

You cannot override a function in its own class - only in a subclass.
User avatar
Ed the Bat
Posts: 3060
Joined: Thu May 03, 2012 1:18 pm
Graphics Processor: nVidia with Vulkan support
Location: Maryland, US

Re: ZScript Discussion

Post by Ed the Bat »

Major Cooke wrote:Can something like this be done?

Code: Select all

// "MyActor" has an int variable PL.
Actor t = Spawn("MyActor",pos);

let s = t;

if (s)
{
    s.PL = 1;
} 
Because this doesn't work. PL isn't recognized.

I ask because I have an array of Class<Actor>s to determine randomly which actor to spawn, and I haven't found a working solution to auto-resolve the class fully without requiring yet another cast to be around it.

Something tells me I need the list of actors to inherit from someone with the variable...
Instead of let, try this:

Code: Select all

MyActor s = MyActor(t);
FelipeO_O
Posts: 7
Joined: Sat Dec 10, 2016 9:31 am

Re: ZScript Discussion

Post by FelipeO_O »

What the zscript keyword "let" does? I cant remember very well.
User avatar
Ed the Bat
Posts: 3060
Joined: Thu May 03, 2012 1:18 pm
Graphics Processor: nVidia with Vulkan support
Location: Maryland, US

Re: ZScript Discussion

Post by Ed the Bat »

Attempts to automatically choose a type for the variable being defined, I believe.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49130
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: ZScript Discussion

Post by Graf Zahl »

'let assigns the type of the right-side expression of the '=' operator to the variable, it's meant as a shorthand for stuff where otherwise some repetition would be needed or where exact knowledge of the type is not needed and it' just supposed to match a variable.
'let a = Inventory(b)' is the same as 'Inventory a = Inventory(b)'.

Other example:

let v = thing.vel; thing.VelFromAngle(); thing.vel += v;

There's no need to know what precise type this is, as long as it's compatible with the following operation.

Essentially it's equivalent to C++'s auto keyword.
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: ZScript Discussion

Post by ZZYZX »

[11:40 AM] ZZYZX: I wrote this
[11:40 AM] ZZYZX: http://pastebin.com/4W9MXAnt
[11:40 AM] ZZYZX: problem is
[11:40 AM] ZZYZX: it says no function GetClassName
[11:40 AM] ZZYZX: While I'm currently looking at base.txt and actor.txt
[11:40 AM] ZZYZX: from gzdoom.pk3
[11:41 AM] ZZYZX: And see that Actor : Thinker, and Thinker : Object, and Object has native Name GetClassName();

What's wrong here?
User avatar
Fishytza
Posts: 786
Joined: Wed Feb 23, 2011 11:04 am
Preferred Pronouns: No Preference

Re: ZScript Discussion

Post by Fishytza »

What's the difference between this:

Code: Select all

if(target is "DoomPlayer")
and this:

Code: Select all

if(DoomPlayer(target))
?
User avatar
Rachael
Posts: 13720
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her

Re: ZScript Discussion

Post by Rachael »

The last typecasts "target" into DoomPlayer. If the cast fails (i.e. target cannot be casted into DoomPlayer) it returns "nullptr" or false.

I am not sure exactly what conditions trigger a successful casting condition, but I would imagine any class that inherits from DoomPlayer would successfully trigger the condition specified in the if statement.
User avatar
Fishytza
Posts: 786
Joined: Wed Feb 23, 2011 11:04 am
Preferred Pronouns: No Preference

Re: ZScript Discussion

Post by Fishytza »

@ZZYZX

At the time of making this post, GetClassName isn't present in the latest dev build. (GZDoom g2.3pre-1072-ga37db1c)
That's probably why you're getting the error.

EDIT: ActorIterator won't do anything if the search TID is 0. What you want to do instead is use ThinkerIterator.
IE replace this

Code: Select all

ActorIterator ai = ActorIterator.Create(0);
with this

Code: Select all

ThinkerIterator ai = ThinkerIterator.Create();
Also make sure to typecast ai.Next() into Actor.
Last edited by Fishytza on Wed Dec 28, 2016 5:05 am, edited 1 time in total.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49130
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: ZScript Discussion

Post by Graf Zahl »

FishyClockwork wrote:What's the difference between this:

Code: Select all

if(target is "DoomPlayer")
and this:

Code: Select all

if(DoomPlayer(target))
?

None. The main difference is that the first expression is a boolean result and the second one a pointer to the target type, but that only matters if you need to work with the pointer - inside an 'if' it doesn't matter.

Code: Select all

(target is "DoomPlayer")
is just shorthand for

Code: Select all

(DoomPlayer(target) != null)
The main difference is that the 'is' operator does not need a type constant to check. You can also do

Code: Select all

if (target is tracer.GetClass())
for example.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49130
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: ZScript Discussion

Post by Graf Zahl »

Eruanna wrote:The last typecasts "target" into DoomPlayer. If the cast fails (i.e. target cannot be casted into DoomPlayer) it returns "nullptr" or false.

I am not sure exactly what conditions trigger a successful casting condition, but I would imagine any class that inherits from DoomPlayer would successfully trigger the condition specified in the if statement.
It's just like in C++. You can cast a pointer to a type if the referenced object is a subclass of that type.
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: ZScript Discussion

Post by ZZYZX »

Is there any way to get the name of an object's class though? I need to somehow obtain at least RELATIVELY unique human readable data to see what actors are affected...
Also, GetClassName seems to be implemented for me here

Code: Select all

DEFINE_ACTION_FUNCTION(DObject, GetClassName)
{
	PARAM_SELF_PROLOGUE(DObject);
	ACTION_RETURN_INT(self->GetClass()->TypeName);
}
Might be that I'm looking at more recent code revision here than the one built at DRDTeam. Sorries in that case.
Last edited by ZZYZX on Wed Dec 28, 2016 5:01 am, edited 1 time in total.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49130
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: ZScript Discussion

Post by Graf Zahl »

Just implemented yesterday: GetClassName()
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: ZScript Discussion

Post by ZZYZX »

FishyClockwork wrote:EDIT: ActorIterator won't do anything if the search TID is 0. What you want to do instead is use ThinkerIterator.
IE replace this

Code: Select all

ActorIterator ai = ActorIterator.Create(0);
with this

Code: Select all

ThinkerIterator ai = ThinkerIterator.Create();
Also make sure to typecast ai.Next() into Actor.
ThinkerIterator.Create("Actor") works, thanks.
Complete code looks like this now:

Code: Select all

		ThinkerIterator ti = ThinkerIterator.Create("Actor");
		Actor mo;
		while (mo = Actor(ti.Next()))
		{
			A_Log(mo.GetClassName());
		}
User avatar
Ed the Bat
Posts: 3060
Joined: Thu May 03, 2012 1:18 pm
Graphics Processor: nVidia with Vulkan support
Location: Maryland, US

Re: ZScript Discussion

Post by Ed the Bat »

Graf Zahl wrote:Just implemented yesterday: GetClassName()
YES! This is an absolute lifesaver! This satisfies my queries from before about how to get the name of a player's class! Thank you so much, Graf! :D

Return to “Scripting”