Detect if Actor is a class but NOT a child of that class

Discussions about Coding and Scripting
Post Reply
1337GameDev
Skilled
Posts: 198
Joined: Thu Apr 16, 2020 3:23 pm
Personal rank: GameDev

Detect if Actor is a class but NOT a child of that class

Post by 1337GameDev »

I am curious if there's a way to detect if a class is EXACTLY a specific class, but not a subclass.

An example could be if I subclass a particular Botpack weapon, but want to detect the original.

I thought about doing these:

Code: Select all

function bool isSpecificEnforcer(Inventory inv) {
    local Enforcer enf;
    enf = Enforcer(inv);

    if(enf != None) {
        //inv is an enforcer, but could be a subclass?
        return true;
    }

    if(inv.IsA('Enforcer') ){
        //inv is an enforcer, but could be a subclass?
        return true;
    }

    if(inv.Class == class'Enforcer') {
        //inv class is EXACTLY an enforcer? 
        return true;
    }
    
    return false;
}
What is the best approach?
User avatar
sektor2111
Godlike
Posts: 6410
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: Detect if Actor is a class but NOT a child of that class

Post by sektor2111 »

:???:
I don't get why you don't use "ClassIsChildOf" for figuring if said enforcer is not another enforcer resident elsewhere than Botpack ?
Also Actor set as variable will be listed as "Package.Class" where you can parse string for figuring if "BotPack" or another stock word it's part of the whole variable definition.
Buggie
Godlike
Posts: 2732
Joined: Sat Mar 21, 2020 5:32 am

Re: Detect if Actor is a class but NOT a child of that class

Post by Buggie »

> if Actor is a class but NOT a child of that class
Actor != None && Actor.Class == DesiredClass

> if Actor is a class but ONLY a child of that class
DesiredClass(Actor) != None && Actor.Class != DesiredClass
User avatar
Barbie
Godlike
Posts: 2802
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Detect if Actor is a class but NOT a child of that class

Post by Barbie »

A remark to type conversion vs. "IsA()":

Code: Select all

enf = Exampleclass(inv);
if(enf != None)
vs.

Code: Select all

if (inv.IsA('Exampleclass'))
Both give always the same result: inv is an Exampleclass or a sub class. I use the former when Exampleclass is known to the compiler. The latter, when Exampleclass is not known. But that one has the risk of undetected spelling errors.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
Buggie
Godlike
Posts: 2732
Joined: Sat Mar 21, 2020 5:32 am

Re: Detect if Actor is a class but NOT a child of that class

Post by Buggie »

Not exactly.

First check inv against well-know class Exampleclass. It is mean used exactly this class, and we travel on parent until find it, or until reach root.

Second check do almost same, except we check against name, not class. So if exists few classes with same name Exampleclass - then will be match against any.

UnrealShare.TriggerLight and Engine.TriggerLight is stock example about that.

Actor.isA('TriggerLight') - match both and require Actor != None

TriggerLight(Actor) != None - match only specific TriggerLight (Which compiler see first, and fine work when Actor == None.
1337GameDev
Skilled
Posts: 198
Joined: Thu Apr 16, 2020 3:23 pm
Personal rank: GameDev

Re: Detect if Actor is a class but NOT a child of that class

Post by 1337GameDev »

All great examples and confirms my suspicions :) Thanks for the collaborative help on this. Much appreciated :)
Post Reply