GetDefaultObject

I have an Actor in my game that has some functions to manipulate other actors. Here is just an example:

void MyActor::ConfuseAttack(AActor* Confused)
{
 // manipulate other actor (does not change state of this actor)
}

It makes sense to put the code there because the functionality is tied to MyActor but I don’t want to have to spawn a new MyActor every time I need to use that functionality.

I only have access to a TSubclassOf<> variable which is set to a Blueprint class which inherits from the MyActor class above.

Can GetDefaultObject() be used for situations like this? Is this a common way to use it?

Thats what static functions are for, in .h class decleration:

static void ConfuseAttack(AActor* Confused);

.cpp stays the same and you can call this function from anywhere like this MyActor::ConfuseAttack(ActorGoesHere) without need of a object. I bet you already used static functions like that as engine have a lot of them too, if oyu look on API reference some functions have S icon, that means it’s a static function and you can call them without object, there even entire classes that only have static function, just because they didn’t fit elsewhere and those classes practically only work as namespaces:

You can also make blueprint node with static function, they will apper without Target pin. GameplayStatics for example is blueprint library, all those functions are in blueprints.

Just remember that by doing so you don’t have access to this and other non-static functions (ofcorse you can still call them but you need object to call on them from elsewhere) as you calling function without object, you only have arguments of function call. But assuming you been ready to do this kind of juggling with default object just to call a function i assume that not a issue

Extra material:

http://www.learncpp.com/cpp-tutorial/812-static-member-functions/

http://en.cppreference.com/w/cpp/language/static

I forgot to mention that it’s a virtual function which is overridden in subclasses, not just one static function with a single behavior. But with the TSubclassOf<> variable I should have access to the subclass type already. Only problem is I don’t have an instance of the class to call it on.

So no, this is not common way it’s first time i hear this kind of idea. But i can tell you that as long as function don’t modify object that is called on (so it does not modify default object) it should be ok, otherwise it might mess up things.

What does this function actually do? Since i assume you only interact with Confused and by name it seems like something attacking the Confused actor and it should like you need actor in the world to do so.

But not every software platform gonna give you default object what you would do then? If you want some confuse attack after actor dies which is only situation i can think of why you want to use function in such way, it is common for games to have status system like in WoW which is having status objects applied to actor that effects actor in some way on some events or overtime.

Also side note TSubclassOf<> is UClass* :stuck_out_tongue: TSubclassOf is only class choose limiting template

Yeah, what I was trying to do was basically what you described. I want to have a status object which effects the actor instantly or over time. For example, a poisoned piece of food. The food needs to have a mesh and exist in the world so it makes sense for it to be an actor. But once the player picks it up he might not choose to eat it right away. Then when he chooses to eat it, it lowers his health. So I have everything about the food in one Actor class (call it FoodActor) which has the mesh, the behavior when eaten, and stats about the food. I was destroying the FoodActor when picked up, but adding a struct to the player which has info about the item he picked up (including the UClass*). So the player will have the UClass* of the food item he picked up and know he has food, but the actual FoodActor was destroyed so he can’t call MyFood->Eat() when he’s ready to eat the food. Each type of food might do something different which is why Eat() would be virtual. Does that make sense?

So I guess one option would be just to make the FoodActor invisible when picked up instead of destroying it? But then every object that the player has will have to be spawned in the world somewhere and invisible. Maybe it would make more sense to decouple the mesh from the behavior into 2 separate classes?