Call Function From Another Class?

So I’ve had a look around and tried several different ways of doing this. I first tried this

but just adding the include file causes all sorts of errors never mind the rest. I also found this and tried it with minimal success,

I’m trying to call a function within an actor from my player character this is what I have so far.

FirstPersonCharacter.cpp

#include "Class1.h"

void AFirstPersonCharacter::TestFunction()
{
	AClass1* actorClass = Cast<AClass1>(this->GetClass());

	actorClass ->SpecialFunction();
}

Class1.cpp

void Class1::SpecialFunction()
{
	UE_LOG(LogTemp, Warning, TEXT("Boop Beep Bop"), );

	//bSpecialBoolean = true;
}

now all of this works somewhat the LOG works and responds when it should but as soon as I do anything else like change a variable or any other code apart from if statements then the whole engine crashes. Everything will compile like its all ok but just crashes when the function should be called.

Can you post your crash logs here?

sorry I should have uploaded it with the question

Those logs are nto really needed as i can see issue here

AClass1* actorClass = Cast<AClass1>(this->GetClass());

The cast will always return null (nothing) because GetClass() always return UClass* of object (which is class identfier) and it definitly not related to AClass1 so cast will fail and return null, this alone does not cause crash it self but actorClass ->SpecialFunction(); because cast return null actorClass will be null or trash memoery, when you call a function on null or invalid pointer you will have a crash or unpredictible behavior.

Classes are not objects, they are blueprint of a object that need to be created first in order to call function in it, UClass is just indefier of a class that UE4 creates so you can for example select it in property editor, but it’s not a object of that class, so you can’t cast it UClass to AClass1. You either need to spawn the actor first or search for it in world using TActorIterator if it’s already placed in the world So you need to do it like this:

 void AFirstPersonCharacter::TestFunction()
 {
     AClass1* actorClass = Cast<AClass1>(()->SpawnActor(AClass1::StaticClass())));
     // or shorter: AClass1* actorClass = ()->SpawnActor<AClass1>();
 
     actorClass ->SpecialFunction();
 }

SpawnActor return pointer to spawned actor (it will return null if it fails for some reason) and you can keep that pointer somewhere and control that actor, ofcorse when you use SpawnActor you will create new actor so call it once somewhere else.

1 Like

Thanks it worked after some more digging around I had to use an object iterator instead of an actor iterator. As far as I can tell Unreal keep changing the uses of it from version to version. Any way this is what I used to make it work as I didn’t need to spawn the actor.

void AFirstPersonCharacter::FunctionThatDoesStuff()
{
	for (TObjectIterator<AClass1> Itr; Itr; ++Itr)
	{
		if (Itr->IsA(AClass1::StaticClass()))
		{
			AClass1* actorClass = *Itr;
			actorClass->SpecialFunction();
		}
	}
}

if there is a cleaner way of achieving this then i would love to know.

thanks