Community Forum

Finding objects in the map

Forum Overview >> Farming Simulator 2011

CategoryFarming Simulator 2011
Created06.01.2011 21:36


Jim Panousis (Jimi540) 06.01.2011 21:50
Hello,

1. I want to learn how i can find an object(the world translation) i added to a map. I want to do it from the script of my mod. The object, a building in my case, is not a trigger or something like that. Its only an object.

2. I also used the getworldtraslation to get the translation of a child of my mod. With this one i got some coordinates that were not the same as the pda shows. It has a difference of about 1024-1027. For example i got for x-axis -210 and the pda shows ~=814. So, i added plus 1024 to 1027 to get the right result. Please tell me if i did right with this, and how i can find the whole mod world translation add not only the childs.
With this test i realised that the world translation i found with the getworldtranslation function shows me that has a reference at the middle of the map, but the pda shows coordinates which has reference from the up left of the map. Am i right?

Hope i didnt mess up the explanation of the problem, and please tell me about the first one. Thanks.

Thats all,
Best Regards,
Jimi

Stefan Geiger - GIANTS Software 07.01.2011 12:31
1. You need to find the node id of the object. And then you can call getWorldTranslation(nodeId) to get the world position of it.

To find the node id, there are multiple ways to do this.
One possibility is to use the onCreate method, as used or triggers and other things, e.g the night-lights of the buildings.
You can find a description how to write the script code to use the onCreate functions with your mod map:
http://gdn.giants-software.com/thread.php?categoryId=16&threadId=556

A second method would be to loop over the whole scene and check for a specific name of an object. E.g. "JimBuilding1".
Here is some code to do this:

function findNodeByName(nodeId, name)
if getName(nodeId) == name then
return nodeId;
end;
for i=0, getNumOfChildren(nodeId)-1 do
local tmp = findNodeByName(getChildAt(nodeId, i), name);
if tmp ~= nil then
return tmp;
end;
end;
return nil;
end;

local jimBuilding1NodeId = findNodeByName(getRootNode(), "JimBuilding1");

if jimBuilding1NodeId ~= nil then
local x,y,z = getWorldTranslation(jimBuilding1NodeId);
end;

The problem is, that the name of the building you are looking for needs to be unique in the whole sccene, which makes the code quite inflexible.
So the preferred method is to use the onCreate method.


2. The zero point of the coordinate system is in the center of the terrain, somewhere below the terrain. If you select the terrain in the GIANTS Editor, the pivot is at the world zero point.

The position returned by getWorldTranslation is relative to this point.
The positions printed by the PDA however are relative to the upper left corner of the terrain. Thus you can convert to PDA coordinates by adding 1024 to the x and z coordinates (1024 is half the size of the terrain), as you already found out. This is the correct way to do this.



Jim Panousis (Jimi540) 07.01.2011 21:03
Stefan thank you very much for the detail instructions.
I agree that the first way is not flexible. I had read that at another post and i can understand it.
The second method is the must have because propably in the future i want to make triggers. I'm new to triggers and i cant hide that this is my first time i tried something like this.
I had read the last weekend all the posts about the onCreate function from LS2009 thread to LS2011 and finally i found the thread you gave me above.

So, i must tell you what i did. I made a building and i import it into the map. Collision, trigger etc.
Then, i made a scriptcallback attribute with name:onCreate and a value:example.onCreate
Then as i said above i tried to write the script to find out the nodeid of the object that have this attribute, so to find later the worldtraslation of it.
And this is the point i stack :)

I wrote the below function in my script file of my vehicle mod. not at any extrafile. Just in my specialization script file of my vehicle mod.

function myspecializationname:exampleonCreate(id)
self.myobjectid = id;
end;
function myspecializationname:draw()
renderText(0.5, 0.9, 0.02, "My object id= " ..self.myobjectid);
end;

Of course i didnt saw any result at my screen from the rendertext function i used.
I read from the tutorial that the onCreate function runs only once and when the object i3d loads during the map loading.
So, i dont know how to use it. I dont know where i must place the script and how i must use it, or what else i need to use it. I dont know basic thinks about this and the post you gave me confuse me a bit. Its clear but i dont know how to use all these.

I also have seen some objects from other modders like the ballenshredder which has a trigger to open its doors. It has a Barrier.onCreate attribute and it works fine with all the vehicles. But i couldnt see anywhere some script to see how they use the script callback attribute. I cant understand from where and how it works.
Can you guide me please?

Stefan Geiger - GIANTS Software 10.01.2011 17:14
The BarrierTrigger is part of the game. So you can just use it with out specifying it.

Using custom onCreate functions with mods is a bit different, since each mod is in its own namespace. So you can't just have function called myOnCreateFunction and write this in the i3d. This is different for the game code, there this works.

What you need to do is have the following code:
function myOnCreateFunction(id)
-- do some stuff
end;
g_onCreateUtil.addOnCreateFunction("myOnCreateFunction", myOnCreateFunction);

And in the i3d you would use the onCreate value: "modOnCreate.myOnCreateFunction".

Jim Panousis (Jimi540) 10.01.2011 17:42
Thanks Stefan, i'll try it but i dont know if i'll do it :)
My best regards!


Note: Log in to post. Create a new account here.