[ZScript] Polymorphism of child actors ?

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
vsonnier
Posts: 80
Joined: Wed Apr 10, 2019 11:22 pm
Graphics Processor: nVidia with Vulkan support
Contact:

[ZScript] Polymorphism of child actors ?

Post by vsonnier »

Question:

I have a piece of code I've made that works, involving polymorphism behaviour and I'm actually suprised it works out of the box. So, I would like to know if it is normal behaviour or if I'm very lucky here.

I have a base actor :

Code: Select all

class Z_Headshot : Actor
{
...
} 
And a child class of Z_Headshot :

Code: Select all

class Z_Headshot_Green : Z_Headshot
{
	Default
	{
		BloodType "Green_Blood", "GreenSawBlood", "GreenSawBlood";
		BloodColor "DarkGreen";
	}
}
that only overrides the blood color and type.

Later, I instantiate one or the other depending of some condition:

Code: Select all

Z_Headshot hs;
if (...) {
	hs = Z_Headshot(Spawn("Z_Headshot_Green", Owner.Pos, NO_REPLACE));
} else {
	hs = Z_Headshot(Spawn("Z_Headshot", Owner.Pos, NO_REPLACE));
}
and store hs as Z_Headshot in some other class member:

Code: Select all

self.myHeadshot = hs;
The point is, while self.myHeadshot is of type Z_Headshot it really behave as Z_Headshot or Z_Headshot_Green depending on the stored object:
That polymorphism is just what I wanted but I wonder if it is intended behaviour or just luck.

Thanks !
User avatar
m8f
 
 
Posts: 1445
Joined: Fri Dec 29, 2017 4:15 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Manjaro Linux
Location: Siberia (UTC+7)
Contact:

Re: [ZScript] Polymorphism of child actors ?

Post by m8f »

Yes, this is intended. This behavior is everywhere. For example, inventory items are all stored as Inventory class, yet they have their own behavior, icons, etc.
User avatar
vsonnier
Posts: 80
Joined: Wed Apr 10, 2019 11:22 pm
Graphics Processor: nVidia with Vulkan support
Contact:

Re: [ZScript] Polymorphism of child actors ?

Post by vsonnier »

Great ! Thanks for the answer. I asked because although inheritance was described in the various tutorials and documentations out there,
I have not seen polymorphism explicitly mantioned with practical examples.
Post Reply

Return to “Scripting”