GetDefaultByType with custom properties?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
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
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

GetDefaultByType with custom properties?

Post by Matt »

Let's say I want to make an inventory class from which several other classes inherit that has a custom string and property, "NiceName" that will show up in place of the real actor name when certain scripts are executed, e.g., it'll say "Fresh Baked Chocolate Chip Cookies" instead of "CookieCChipSpawnD0" or at best "FreshBakedChocolateChipCookies" with no spaces.

I've got one script that needs to get the default NiceNames of these classes, without referring to any particular instance of any of them.

Here is what I have tried:

Code: Select all

// Expected output: type "summon nna" in console, get "nice name" when it appears
// Actual: fail to compile: "Unknown identifier 'nicename'"
class nicenameactor:inventory
{
    string nicename;
    property nicename:nicename;
    default
    {
        nicenameactor.nicename "nice name";
    }
}
class nna:actor
{
    states
    {
    spawn:
        TNT1 A 1 nodelay
        {
            for(int i=0;i<allactorclasses.size();i++)
            {
                if(allactorclasses[i] is "nicenameactor")
                {
                    string nn=getdefaultbytype(allactorclasses[i]).nicename;
                    A_Log(nn);
                }
            }
        }
        stop;
    }
}
 
If I specifically write in "nicenameactor" in the getdefaultbytype, it works fine, but obviously that means I won't be able to check for any descendants which is the whole point of this script.

Is there any other way to get the default class properties where the class is reference by a variable?
gramps
Posts: 300
Joined: Thu Oct 18, 2018 2:16 pm

Re: GetDefaultByType with custom properties?

Post by gramps »

It looks like strings are automatically converted to class type references for functions that want class type references, but not the other way around. I think AllActorClasses[n] is getting you a type reference, but GetDefaultByType actually wants a class name in the form of a string.

Might be missing something, but it looks like GetDefaultByType really ought to take a type reference instead, if a string would get converted anyway. As it is, it's not really clear to me how to do what you want, but I'm not too familiar with the API yet.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: GetDefaultByType with custom properties?

Post by Graf Zahl »

The problem here is actually very simple:

GetDefaultByType("nicemnameactor") can do an implicit type conversion and give you the proper type, but when you pass in a class type variable you get a basic class<Object> as return because the compiler does not know about the queried object's type at compile time. And this base type simply does not know about the extended properties of subclasses.

You have to cast the returned default into the proper type yourself.
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

Re: GetDefaultByType with custom properties?

Post by Matt »

Thanks, good to know I was at least in the right direction and it was just a matter of which syntax to use.

"nicenameactor(getdefaultbytype(allactorclasses))" gave me "Cannot cast a readonly pointer" while "getdefaultbytype(nicenameactor(allactorclasses))" gave me "Cannot convert ClassPointer<Actor> to Pointer<Class<Object>>".

Turns out the correct version is

Code: Select all

string nn=getdefaultbytype((class<nicenameactor>)(allactorclasses[i])).nicename; 
(not sure if the outermost parentheses are necessary though) (they are)

Return to “Scripting”