How to make a TMap of UClass* and a method in that class?

I want to make a TMap of UClass pointers as the keys, and a function to call as the values. This would be extremely useful in pretty much any game, for example having generalized enemy types with damage health functions, so I can’t imagine this has never been done but I haven’t found any forum posts on it. What I have right now is this:

TMap<UClass*, void (TSubclassOf<AActor*>::*)(float)> badGuyTypes;

TTuple<UClass*, void (TSubclassOf<AActor*>::*)(float)> type1 = TTuple<UClass*, void (TSubclassOf<AActor*>::*)(float)>(AbadGuyActor::StaticClass(), &AbadGuyActor::damageHealth);

badGuyTypes.Add(type1);

I get an error saying that there is no constructor to make this because
void (TSubclassOf<AActor*>::*)(float) does not equal &AbadGuyActor::damageHealth. How do I just get a function pointer to a function in an unknown subclass of AActor?

bumping this

Hi Aejjee,

If I’m understanding you correctly - I think you would be better off having a “BadGuy” base class and SubClass off that for each of your other types, giving each of those it’s own derived “damgeHealth” function.

Then you could just call those functions from a list of all actors subclassed from “BadGuy” - it calls the correct function even when you’re just storing it as a BadGuy - as long as you constructed it as the specific type of SubClassed BadGuy.

What’s the difference between having the bad guys as subclasses of AActor? Wouldn’t it just be the same sitiuation where I’d have to say TSubclassOf<BadGuy*>::* but then each individual function pointer would just be like &badGuy1::damageHealth?

That seems like the same situation where it won’t like that badGuy1 isn’t directly equivalent to TSubclassOf<BadGuy*>. I likely misunderstood your solution could you explain it more?

I was meaning instead of doing any of that.

I’m picturing you currently doing something like badGuyTypes[mybadGuyActor::GetStaticClass()].damageHealth

If you have BadGuy SubClassed from Actor with a function called “GetDamageHealth” - then MyBadGuy1 SubClassed from BadGuy, override that “GetDamageHealth” and return what you want there.

Then you can simply do:

mybadGuyActor->GetDamageHealth()

But I may not be grasping exactly what you’re after…

I think I’m following what you’re saying and that would work but I’m not quite sure how to do that. I created a new class derived from AActor called badGuyBase, and in badGuyBase.h I defined:

UFUNCTION()
	void DamageHealth(float value);

I also wrote an empty definition of it in badGuyBase.cpp.

Then in badGuyActor.h I switched it to be derived from badGuyBase (I think) by doing:

class YOCRAZYSTORYHERE_API AbadGuyActor : public AbadGuyBase

Then I tried overriding DamageHealth() in AbadGuyActor.h by doing:

virtual void DamageHealth(float value) override;

but it gave me the error:

member function declared with ‘override’ does not override a base class member

How exactly do I make badGuyActor derived from badGuyBase and then override DamageHealth()?

That’s almost it, in the baseclass header, declare it as virtual:

UFUNCTION()
virtual void DamageHealth(float value);

but wouldn’t it be:

UFUNCTION()
virtual float DamageHealth();

to return the amount of damage?

Sweet that worked! No its meant to be like you call the damage health function on a bad guy for the amount of health the bad guy is damaged. Thank you for your help this was a much more elegant solution!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.