Community Forum

How to load a trigger with the map

Forum Overview >> Farming Simulator 2009

CategoryFarming Simulator 2009
Created27.07.2009 13:40


B. Benjamin (unknown) 27.07.2009 13:49
Hello,

First, i'm sorry to ask this question and show you the following " script". It's just a try to understand how the trigger could work.
I'm trying to load a trigger which is on my map. It's a simple one, just to move a door by translation. I call this script with the " Extrafile" system. The script is loaded ( print is displayed ) but nothing worked...
On my map, I have a trigger ( static object - Collision - Trigger ) which has the attribute "onCreate|Porte.onCreate". This one contains the door as a static object. In the .i3d, the trigger has " type=scriptCallback" value="Porte.onCreate" ".

Here is the script "Porte.lua" which is called by the modDesc.xml file :

Porte = {};


function Porte:loadMap(name)
local self = {};
if customMt ~= nil then
setmetatable(self, customMt);
else
setmetatable(self, Porte_mt);
end;

self.triggerId = id;
addTrigger(id, "triggerCallback", self);
self.deleteListenerId = addDeleteListener(id, "delete", self);
self.count = 0;

self.porte = getChildAt(id, 0);

self.isEnabled = true;

return self;
end;

-- note: this is called as soon as the trigger entity is deleted
function Porte:delete()

removeTrigger(self.triggerId);
removeDeleteListener(self.triggerId, self.deleteListenerId);
end;

function Porte:update(dt)

if self.count > 0 then
setTranslation(self.porte, -2.21553, 1.60437, -4.4998);
else
setTranslation(self.porte, -2.21553, 1.60437, 0.13725);
end;
end;

function Porte:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay)

if onEnter and self.isEnabled then
self.count = self.count + 1;
elseif onLeave then
self.count = self.count - 1;
end;
end;

function Porte:onCreate(id)
table.insert(g_currentMission.barrieres, Porte:new(id));
print("Trigger pour stabule");
end;


I'm sure I made a complete mess with all that... I'm discovering trigger system and it's kinda difficult.
May I have some help to understand how I could make the trigger working ?

Thanks a lot for your help.
Best regards,
Bayn

Heady Planet-ls (Headshot XXL) 27.07.2009 21:13
"Trigger pour stabule" will be printed?
is the name of the zip Porte.zip?

or try this for oncreate function:

_G.PorteonCreate= function(id)
table.insert(g_currentMission.barrieres, Porte:new(id));
print("Trigger pour stabule");
end;

and the scriptcallback name of the trigger: PorteonCreate





(unknown) 09.08.2009 15:29
Hello Christoph,

I'm sorry for this huge delay, I had a lot of work these days.
I just tried your tips, unfortunately it doesn't work! :(

The " trigger pour stabule " is not printed and the name of the .zip file is Porte.zip. I changed the name of the trigger as "PorteonCreate" as you said.

Your advice sent me back this error :
Error running lua function: PorteonCreate
[string "C:/Documents and Settings/Bayn/My Documents..."]:50: attempt to call method 'new' (a nil value)

In this line : table.insert(g_currentMission.barrieres, Porte:new(id)); it seems there is no "id" sent back for the "new" method.

Any other advice ?
Thanks a lot for your help. ;)

Bayn

Heady Planet-ls (Headshot XXL) 10.08.2009 00:01
edit this function like this:

_G.PorteonCreate= function(id)
Porte:load(id)
print("Trigger pour stabule:", id);
end;

then add the function to the script:

function Porte:load(id)
end;

in this function can you write your self variables from the function "Porte:loadMap(name)" , without customMt.





B. Benjamin (unknown) 10.08.2009 14:49


Edit 10:02 PM :

I re-write the script to clean it and I have tried to copy the "setTranslation (self.porte,... ) " line in the Porte:triggerCallback function. Oddly, it worked! :/ ( But it's an instant movement. It's directly open OR close). I "played" with some "print" function, to see where the problem was and it seems that the script never read the "update" function. Is it possible that this part of the script is wrong ?

Here is the "new" script :

Porte = {};

_G.PorteonCreate= function(id)
Porte:load(id)
print("Trigger pour stabule:", id);
end;

function Porte:load(id)


self.triggerId = id;
addTrigger(id, "triggerCallback", self);
self.deleteListenerId = addDeleteListener(id, "delete", self);
self.translateDoor = false;
self.porte = getChildAt(id, 0);
print("PorteLOAD=OK");
end;

function Porte:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay)

if onEnter then
self.translateDoor = true;
print("PorteTRIGGERenter");
elseif onLeave then
self.translateDoor = false;
print("PorteTRIGGERleave");
end;

end;

function Porte:delete()

removeTrigger(self.triggerId);
removeDeleteListener(self.triggerId, self.deleteListenerId);
end;

function Porte:update(dt)
print("PorteUPDATE=OK");
if self.translateDoor then
print("PorteTRANSLATETRUE");
setTranslation(self.porte, -2.41198, 1.60437, -4.4471);
else
setTranslation(self.porte, -2.41198, 1.60437, 0.0967);
end;

end;

The "print" displayed are :
- Trigger pour stabule
- PorteLOAD=OK
- PorteTRIGGERenter
- PorteTRIGGERleave

Thanks again for your patience and help! ;)

Heady Planet-ls (Headshot XXL) 11.08.2009 00:09
do write this line at end of the lua:

addModEventListener(Porte);

B. Benjamin (unknown) 11.08.2009 01:34
Nope, it doesn't work... :(

Here is the error in the log :

Error running lua function: update
[string "dataS/scripts/BaseMission.lua"]:361: attempt to call method 'loadMap' (a nil value)
Error running lua function: draw
[string "dataS/missions/mission00.lua"]:297: attempt to index field 'inGameMessage' (a nil value)


Arg, this makes me crazy...
Anyone else have an idea ? Face ? S. Geiger ?

Heady Planet-ls (Headshot XXL) 12.08.2009 01:13
you should not delete the "Porte:loadMap(name)" function.

extrafiles must be have these functions:

loadMap(name),deleteMap(), mouseEvent(posX, posY, isDown, isUp, button), keyEvent(unicode, sym, modifier, isDown), update(dt), draw()

for reference view the ExtraFile.lua form the LS09/sdk/samplemod at your CD.

So, add this to your lua:

function Porte:loadMap(name)
end;

function Porte:deleteMap()
end;

function Porte:mouseEvent(posX, posY, isDown, isUp, button)
end;

function Porte:keyEvent(unicode, sym, modifier, isDown)
end;

function Porte:draw()
end;

B. Benjamin (unknown) 12.08.2009 13:22
I had a look at the ExtraFile.lua in the SDK "pack".
So, I updated the script again to get something like this :

PorteEventListener = {};

_G.PorteonCreate= function(id)
Porte:load(id)
print("Trigger pour stabule:", id);
end;

function PorteEventListener:loadMap(name)
end;

function PorteEventListener:draw()
end;

function PorteEventListener:mouseEvent(posX, posY, isDown, isUp, button)
end;

function PorteEventListener:keyEvent(unicode, sym, modifier, isDown)
end;


function Porte:load(id)
self.triggerId = id;
addTrigger(id, "triggerCallback", self);
self.deleteListenerId = addDeleteListener(id, "delete", self);
self.translateDoor = false;
self.porte = getChildAt(id, 0);
print("PorteLOAD=OK");
end;

function PorteEventListener:deleteMap()
removeTrigger(self.triggerId);
removeDeleteListener(self.triggerId, self.deleteListenerId);
end;

function PorteEventListener:update(dt)
print("PorteUPDATE=OK");
if self.translateDoor then
print("PorteTRANSLATETRUE");
setTranslation(self.porte, -2.41198, 1.60437, -4.4471);
else
setTranslation(self.porte, -2.41198, 1.60437, 0.0967);
end;
end;

function PorteEventListener:load(id)
self.triggerId = id;
addTrigger(id, "triggerCallback", self);
self.deleteListenerId = addDeleteListener(id, "delete", self);
self.translateDoor = false;
self.porte = getChildAt(id, 0);
print("PorteLOAD=OK");
end;

function Porte:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay)

if onEnter then
self.translateDoor = true;
print("PorteTRIGGERenter");
elseif onLeave then
self.translateDoor = false;
print("PorteTRIGGERleave");
end;

end;


addModEventListener(PorteEventListener);


And we have made a step forward! The print "PorteUPDATE=OK" is now displayed.
Unfortunately, the translation doesn't work! :/
The self.translateDoor is true because my " print("PorteTRIGGERenter"); " is also displayed.

Am I missing something in all that ? Scripting trigger seems to be a little bit more difficult than a specialization. I'm not sure to understand all the things... ^^

Heady Planet-ls (Headshot XXL) 12.08.2009 13:51
why made you a mix with the classnames?
"Porte" and "PorteEventListener" are not the same. Please check this.

B. Benjamin (unknown) 12.08.2009 14:12
Sorry Christoph, I'm a little bit lost with these kind of scripts... I'm learning .lua, it's not always easy to understand something by myself! ^^

So, here is the "new" script :

PorteEventListener = {};

_G.PorteonCreate= function(id)
PorteEventListener:load(id)
print("Trigger pour stabule:", id);
end;

function PorteEventListener:loadMap(name)
end;

function PorteEventListener:draw()
end;

function PorteEventListener:mouseEvent(posX, posY, isDown, isUp, button)
end;

function PorteEventListener:keyEvent(unicode, sym, modifier, isDown)
end;


function PorteEventListener:load(id)
self.triggerId = id;
addTrigger(id, "triggerCallback", self);
self.deleteListenerId = addDeleteListener(id, "delete", self);
self.translateDoor = false;
self.porte = getChildAt(id, 0);
print("PorteLOAD=OK");
end;

function PorteEventListener:deleteMap()
removeTrigger(self.triggerId);
removeDeleteListener(self.triggerId, self.deleteListenerId);
end;

function PorteEventListener:update(dt)
print("PorteUPDATE=OK");
if self.translateDoor then
print("PorteTRANSLATETRUE");
setTranslation(self.porte, -2.41198, 1.60437, -4.4471);
else
setTranslation(self.porte, -2.41198, 1.60437, 0.0967);
end;
end;

function PorteEventListener:load(id)
self.triggerId = id;
addTrigger(id, "triggerCallback", self);
self.deleteListenerId = addDeleteListener(id, "delete", self);
self.translateDoor = false;
self.porte = getChildAt(id, 0);
print("PorteLOAD=OK");
end;

function PorteEventListener:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay)

if onEnter then
self.translateDoor = true;
print("PorteTRIGGERenter");
elseif onLeave then
self.translateDoor = false;
print("PorteTRIGGERleave");
end;

end;


addModEventListener(PorteEventListener);

And, it works... almost! :)
The door is now translated, but the movement is "instant".
Does something exist to control the speed of the translation in this line :

setTranslation(self.porte, -2.41198, 1.60437, -4.4471); ?




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