Frictional Games Forum (read-only)
[SOLVED] How to get distance to enemy? - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: SOMA (https://www.frictionalgames.com/forum/forum-55.html)
+--- Forum: User created content (https://www.frictionalgames.com/forum/forum-79.html)
+---- Forum: Technical Support (https://www.frictionalgames.com/forum/forum-80.html)
+---- Thread: [SOLVED] How to get distance to enemy? (/thread-53523.html)



[SOLVED] How to get distance to enemy? - Mafiyia - 01-27-2017

I'm creating an add-on mod where I would like to get the distance to an enemy. After researching the documentation I have been trying to use the Player_GetDistanceToEntity(const tString &in asEntity) function where asEntity is the entity's name. But I'm having a hard time figuring out what I should use as the entity's name.

For example in Chapter 1 in Upsilon Awake there is an enemy according to the Wiki called 'Construct'. I've used Construct, construct, construct_theta, infected_robot, etc. as input into the function but I always get a entity does not exist in my debug messages.

I even tried loading up the map in the Level Editor to see if I could find it, but the map is so cluttered that it made this task pretty much impossible.

So does anyone know where I could find what the asEntity names should be or any better methods to find the distance to enemy?


RE: How to get distance to enemy? - Romulator - 01-27-2017

asEntity refers to the name of the Entity as it is defined in the Level Editor.

A slightly better way to do it, though I don't know the exact syntax, would be to get the name of the entity that the Player is currently looking at, then pass that value into Player_GetDistanceToEntity(), otherwise you need to add conditionals for every specific entity.

PHP Code:
tString entityName Player_GetFocusEntityName();      //Maybe?
float entityDist Player_GetDistanceToEntity(entityName);
cLux_AddDebugMessage(entityName " is " entityDist "f units away."false); 



RE: How to get distance to enemy? - Abion47 - 01-27-2017

There are three ways to do the distance problem, and they all require the name of the monster's entity. Like Romulator said, the "name" is just the value of the monster entity that is inside the "Name" box in the Level Editor. If, for example, that box looks like this:

[Image: UMfw1kl.png]

Then the name you would use in your script would be "super_scary_monster".

Now the first approach is what you are currently doing - use Player_GetDistanceToEntity:

Code:
float distance = Player_GetDistanceToEntity("super_scary_monster");

The second is getting the positions of both the player and the entity and doing some vector math:

Code:
cVector3f pPos = Player_GetPosition();
cVector3f ePos = Map_GetEntity("super_scary_monster").GetPosition();

float distance = (ePos - pPos).Length();

And the third is doing all the math yourself:

Code:
cVector3f pPos = Player_GetPosition();
cVector3f ePos = Map_GetEntity("super_scary_monster").GetPosition();

float distance = cMath_Sqrt((pPos.x - ePos.x) * (pPos.x - ePos.x)
                          + (pPos.y - ePos.y) * (pPos.y - ePos.y)
                          + (pPos.z - ePos.z) * (pPos.z - ePos.z));

The second one is basically what the first one does anyway, and the third one, though somewhat cool (depending on your point of view), is quite verbose, so yeah, you should go ahead and stick to the first one.


RE: [SOLVED] How to get distance to enemy? - Mafiyia - 01-30-2017

Thanks for the help!