Hi.
I’m trying to create a food system in C++ and so far, it works as expected.
A little bit of background:
It’s somewhat complicated with all the inheritance that I did, but for the sake of simplicity I’ll just get to the point:
So, I have a class called AOclloCharacter that inherits from ACharacter
I also have a class called “Food” which inherits from AActor.
In this class I override the NotifyActorOnClicked function thusly:
Note: simplified version of my code. The actual code stretches across several classes and blueprints.
void Food::NotifyActorOnClicked(FKey ButtonPressed)
{
FVector ItemCoords = GetActorLocation();
FVector PlayerCoords = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)->GetActorLocation();
FVector PlayerDistance = ItemCoords - PlayerCoords;
AOclloCharacter * PlayerCharacter = Cast<AOclloCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
float Distance = PlayerDistance.Size();
if (Distance > UseDistance)
{
PlayerCharacter->AddToMessageQueue("This is too far away to interact with.");
}
else
{
AOclloCharacter * PlayerCharacter = Cast<AOclloCharacter> (UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
PlayerCharacter->Eat(this);
}
}
So, basically what it does is,when my food item is clicked upon, it gets the first player, and calls its “Eat()” function supplying a reference to itself (which then, the player class gets all the info from this reference as to how much hunger to reduce… etc.)
Now, it works perfectly well for a single player game. But what if I have an online game? How can I get the instance of the exact player that instigated the click function?
As you can see, I’m trying to get the player location so that I can calculate the distance to the item clicked, and also, as mentioned above, to call the player’s Eat(); function.