Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help Determine player position
MyRedNeptune Offline
Senior Member

Posts: 553
Threads: 3
Joined: May 2012
Reputation: 33
#1
Determine player position

Is there a script to find out the player's (x;y;z) position, and then use those values to set the position of an entity?

Here's the deal: I want an entity with a billboard attached to it to constantly maintain a certain position in relation to the player.

If this can be done, that would be awesome-tastic. ^^

^(;,;)^
(This post was last modified: 10-21-2012, 01:44 PM by MyRedNeptune.)
10-21-2012, 01:23 PM
Find
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#2
RE: Determine player position

This is not an easy task, but it is possible. The approach you would need to take depends on a few details however:
  1. Does the entity need to be solid / interact with physics in any way?
  2. Do you require knowledge about which direction the player is facing?
  3. How large an area should this occur over?
  4. How long do you expect this to happen for - Seconds? Minutes? Forever?
  5. Is the entity close to the player or far away?
10-21-2012, 01:37 PM
Find
The chaser Offline
Posting Freak

Posts: 2,486
Threads: 76
Joined: Jun 2012
Reputation: 113
#3
RE: Determine player position

Well, I don't really think this works this way, but maybe it does.

AddAttachedPropToProp("Player", "Entity", "Entity.ent", 0,0,0, 0,0,0);

THE OTHERWORLD (WIP)
[Image: k6vbdhu]

Aculy iz dolan.
10-21-2012, 01:43 PM
Find
MyRedNeptune Offline
Senior Member

Posts: 553
Threads: 3
Joined: May 2012
Reputation: 33
#4
RE: Determine player position

1. No, the only purpose of the entity is to move the billboard. The entity itself would not even be visible.
2. No.
3. Do you mean the size of the effect? Quite small - just a single billboard.
4. For the duration of a level.
5. I have not quite decided on this yet. Does it make a large difference?

^(;,;)^
(This post was last modified: 10-21-2012, 01:47 PM by MyRedNeptune.)
10-21-2012, 01:43 PM
Find
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#5
RE: Determine player position

Ok, there are two approaches, and these approached depend on how frequently you wish to move the object and how vital precision is. The first approach is discussed on the wiki here.

This approach is a reasonable approach, except it will be slow for any decent level of precision (remember that this system will need to start off with an initial cube size large enough to cover your map, then subdivide down to the player position - whilst this grows at a rate of log(n), each step still adds hard disk read for loading in the subdivision entity of that size). The bad new is this approach would require altering to get it to work with the "Player" entity - and i don't know how much work that would be. If you wish to use this approach with the player and cannot alter the code yourself i would probably be able to write up a version which supports "Player", but i'm probably going to be busy so i might not get around to it for some time.

The other approach i have worked on today, and acts a little differently. You can get the sample map here .
This map sample contains the map and script demo, as well as a python script for generating expObjs (which can be loaded into the level editor using file->Import objects) and a generated expobj.

The actual finding a position has very little performance overhead, and will be able to track the position of a huge amount of objects to an arbitrary level of precision. The downside is that this will bloat your map file size significantly - but this is still a fairly favorable trade-off.

Firstly, you will need to download and install python 2.7 and run this script (This script is also included in the sample map folder). I used the following parameters:
scale factor (x/z):1
scale factor (y):5
x-length:20
y-length:10
z-length:20
filename:t.expobj
This generates an expobj file containing a grid of script areas covering the specified x/y/z range with a step-size equal to the scale factor. The python script can be extended to include lookat callbacks along with other things pretty trivially. You then import the object into your map file and move the grid to the appropriate location.

You can then copy & paste this script into your script file. This script will then let you use the following functions:
Grid_Initiate(float originX, float originY, float originZ, float scaleFactorX, float scaleFactorY, float scaleFactorZ)
Grid_StartTracking(string &in asEntity)
float[] Grid_GetTrackedObjectPosition(string &in asEntity)
string Grid_WorldCoordToGrid(float x, float y, float z)
There is also a callback called "_cb_Grid_OnMove" which determines if an object has moved, you can tweak this to act as a trigger for if the player has moved if you so wish.

To use this functions, firstly call "Grid_Initiate" in the OnStart() event - the Origin parameters are the X/Y/Z coordinates of the script area called "SAGrid_(0.000,0.000,0.000)", and the scale values are the x/y/z scale of this script area. After this, at any point you can call "Grid_StartTracking" to begin tracking an entity (E.g. "Player"). To get the coordinates of this entity call "Grid_GetTrackedObjectPosition" and specify the entity, a float array of 3 elements will be returned. E.g.
void OnStart()
{
Grid_Initiate(-8.75f,2.5f,-10.75f,1,5,1);
Grid_StartTracking("Player");
}
void SomeEvent()
{
float[] position = Grid_GetTrackedObjectPosition("Player");
AddDebugMessage("Player's X-Position is: " + position[0],false);
AddDebugMessage("Player's Y-Position is: " + position[1],false);
AddDebugMessage("Player's Z-Position is: " + position[2],false);
}

Like with the previous approach, you can just use AddAttachedPropToProp to attach the entity you wish to make appear. However, if you want a "billboard" effect you could also just create the billboard as a particle system.

If you want to use the particle system approach (or you want your entity to collide and be physically active) then you can use the last function: Grid_WorldCoordToGrid - where the x/y/z represent where in the world you want to place your object. You can then use CreateEntityAtArea (or the equivalent PS function) with the returned value. E.g.
void SomeEvent()
{
//Get player position
float[] position = Grid_GetTrackedObjectPosition("Player");
//Create a barrel above the player's head & drop it on him
string gridObject = Grid_WorldCoordToGrid(position[0],position[1]+5,position[2]);
CreateEntityAtArea("Barrel_Drop","barrel01.ent",gridObject,false);
}
(This post was last modified: 10-21-2012, 06:01 PM by Apjjm.)
10-21-2012, 06:00 PM
Find




Users browsing this thread: 1 Guest(s)