[zScript]overriding parent class function

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
Cherno
Posts: 1337
Joined: Tue Dec 06, 2016 11:25 am

[zScript]overriding parent class function

Post by Cherno »

Is it possible to override a custom function of a parent class in a sub-class?

Apparently, the virtual keyword doesn't do anything for custom functions.

Code: Select all

class BaseClass : Actor
{
     virtual void BaseFunction()//virtual keyword doesn't do anything apparently?
     {

     }
}

class SubClass : BaseClass 
{
     override void BaseFunction()//gives error that no virtual function of this name exists
     {

     }
}
Last edited by Cherno on Thu Mar 21, 2019 2:35 pm, edited 3 times in total.
User avatar
Mikk-
Posts: 2274
Joined: Tue Jun 30, 2009 1:31 pm

Re: [zScript]overriding parent class function

Post by Mikk- »

Well for one you're not inheriting from BaseClass, so of course Actor won't have the function BaseFunction to override ;)
User avatar
Cherno
Posts: 1337
Joined: Tue Dec 06, 2016 11:25 am

Re: [zScript]overriding parent class function

Post by Cherno »

Mikk- wrote:Well for one you're not inheriting from BaseClass, so of course Actor won't have the function BaseFunction to override ;)
Of course, my mistake in providing a false example. The actual code is correct in this regard, of course. I edited the code example. Thanks.
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: [zScript]overriding parent class function

Post by Blue Shadow »

Post the actual code that you're having trouble with.
User avatar
Cherno
Posts: 1337
Joined: Tue Dec 06, 2016 11:25 am

Re: [zScript]overriding parent class function

Post by Cherno »

It seems to have been a logic error on my part. Sorry for wasting everyone's time :oops:
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: [zScript]overriding parent class function

Post by Matt »

Did you put the #include for subclass before the one for baseclass?
User avatar
Cherno
Posts: 1337
Joined: Tue Dec 06, 2016 11:25 am

Re: [zScript]overriding parent class function

Post by Cherno »

Matt wrote:Did you put the #include for subclass before the one for baseclass?
I have never given the order much thought but I commonly #include base class first out of habit. However, as I said, it was a logic error and works as intended now.
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: [zScript]overriding parent class function

Post by Blue Shadow »

For the benefit of anyone who might run into the same issue as you did and are looking for an answer: what was the error, and how did you solve it?
User avatar
Cherno
Posts: 1337
Joined: Tue Dec 06, 2016 11:25 am

Re: [zScript]overriding parent class function

Post by Cherno »

I can't say. It's a very complex project I'm working on and I don't know exactly why it works now. However, it's safe to say that the code example above works if nothing else is preventing any functions from being called.
User avatar
RSSwizard
Posts: 237
Joined: Mon Jan 11, 2016 3:55 pm

Re: [zScript]overriding parent class function

Post by RSSwizard »

Hoping to avoid posting another thread but ive got a similar question:
How do you override existing functions like for example the action for the shotgun sergeant firing his shotgun?
I want to overwrite these functions so that I can graciously, and nicely, update monster attacks to reflect weapon set replacements. Without replacing the monsters.
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: [zScript]overriding parent class function

Post by Matt »

No, by design you can't go "backwards" and change actors that already exist, you'll need a new shotgunguy actor that calls your custom action.

I do understand it's an annoyance to have to redefine all those states though...
Post Reply

Return to “Scripting”