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.
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:
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.
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.