So I have this tower mesh I’m trying to continuously spawn in until I left click, kind of like a preview of where the player is going to place his tower. When the player left clicks, he is telling the game that where he clicked is where he wants to spawn his tower.
How do I spawn a mesh in c++? I’ve looked it up and I can’t really find anything on it.
I tried doing it with the actor itself, but this created a ton of copies of my actor and crashed my game.
Althought it succeeded in deleting the tower after every tick, the amount of deleted archers crashed my editor, so I figured this wasn’t an optimal way of doing it.
You want to go with the actor spawning route and store the reference to it. After you spawned the tower, just move it until you click again.
header file:
//in your header file
UPROPERTY(BlueprintReadOnly, Category ="your category")
class YourTowerParentClass* CurrentTower;
.cpp:
//.cpp file
if(!CurrentTower)//If we don't have a tower refrence yet,
{
if(TowerClass)//if the tower class is set
{
//Spawn the Tower and store a reference to it
CurrentTower = SpawnActor<ATower_Archer>(TowerClass,
currentPosition, startRotation);
}
}
else
{
//IN HERE ONLY MOVE THE TOWER TO THE DESIRED LOCATION
}
if(LeftMouseIsClicked)
{
//Stop moving the tower
CurrentTower = nullptr;
}
YourTowerParentClass is the parent class for all your towers, so the code is easier to write. TowerClass is the specific class of the tower you are trying to spawn at the moment.
Ok, this code spawns 1 tower and then moves the tower when you already spawned one. You might want to spawn something else than the real tower though. I mean a preview version of it, but that is just a suggetion.