i literally just dont know how to call a function from another class :/

i thought i know c++ but i guess no. so what I want to do is call a function that I have created in an actor and so I should create an instance of that class and cast it to something and that’s the part I don’t know. what should i cast it to?

Assume you’re talking about a C++ function. You need to cast the actor to the actor class you’ve defined the function in. The cast will return null if the types are incompatible (if it’s not your actor class) so you can do something like this:


if (AMyActorClass* MyActor = Cast<AMyActorClass>(Actor))
{
  // Do whatever your custom actor class lets you do
  MyActor->MyAwesomeFunction();
}
else
{
  // Actor is not an instance of MyActorClass
}

where is that Actor keyword in the parentheses coming from?

this is basically what I want do:

AItems* Items = Cast<AItems>();

if (Items)
{
Items->DoStuff();
UE_LOG(LogTemp, Warning, TEXT(“Cast was success”));

}

i don’t know what to put inside parentheses

First, you must spawn the actor in the world with the SpawnActor() function, or get a reference to it somewhere if you spawned it somewhere else.



AItem* Item = SpawnActor<AItem>(SpawnLocation, SpawnRotation);


if(Item) {
     Item->DoStuff();
}


There is no need to cast since SpawnActor() is a template function. The class you specify in the angle brackets will be the return type of the function.

Casting is only necessary if you already have an object and wish to convert it to another related class type, such as casting an object from an AActor to an ACharacter.

You should create a new first person shooter project and select C++ project. It will make a copy of all the normal blueprint classes in C++ and you can see how they do all this stuff.

the actor already exists in the world. how do I get a reference without having to respawn it?

Finding the Actor in the world is the tricky part. This is where system design comes in. You want to think of a way to spawn/place your item and be able to find it quickly, there is no “right way” to do this - it all depends on your game, how your world is setup, how the player interacts with that world, etc.

Some examples of approaches:

1.) You just use a TActorIterator<MyActor>, which will iterate over every Actor of that type in the world. This is pretty brute force and potentially slow, but it would work. You could also cache the results and do it just at start up to hide the performance hit and make it one-time only.
2.) You attach a volume to your player that does a query every half a second or so, looking for items.
3.) Your Item has a volume that looks for Players and tells the Player “Hey, you’re looking at me” when they come in range.
4.) You have some system that spawns that Actor and then keeps a reference to it, e.g., MyItemManager which spawns all items in the world in random locations and keeps a list of all the Items it’s spawned.
5.) You have some Actor (e.g., MyDungeon) which is responsible for spawning the loot contained with it, and you just ask the Dungeon “Hey, give me a piece of loot please.”
6.) You have some other Item in the world and you manually setup a reference in the Level Editor (e.g., You have AKey which contains a pointer to ADoor which it unlocks).

If you do the “Battery Collector” tutorial, they go over things like this (and use approach #3).

guys this is big brain time :d

if (bHitSomething)
{
bool HitResult = Hit.GetActor()->ActorHasTag(TEXT(“InteractableItems”));

if (HitResult)
{

AActor* OtherActor = Hit.GetActor();

AItems* Items = Cast<AItems>(OtherActor);

if (Items)
{
Items->DoStuff();
}
}
}
}

the solution was in front of my ■■■■■■■ eyes omfg.