This code will work better
#include "GameObjManager.h"
#include "SoldierGameObj.h"
GameObject *Get_Nearest_Solider(GameObject *obj) // Get the nearest solider next to a object
{
float closestdist = FLT_MAX;
Vector3 obj_pos = Commands->Get_Position(obj);
SmartGameObj *object = 0;
for (SLNode<SmartGameObj>* node = GameObjManager::SmartGameObjList.Head(); node; node = node->Next())
{
SmartGameObj* object = node->Data();
if (o && o->As_SoldierGameObj())
{
Vector3 player_pos = Commands->Get_Position(o);
float dist = Commands->Get_Distance(player_pos,obj_pos);
if (dist < closestdist)
{
closestdist = dist;
object = o;
}
}
}
return object;
}
That code will find the closest soldier to the passed in object.
The SmartGameObjList is for soldiers and vehicles and should be used in this case as you will have less objects to check.