Community Forum

Rotation Script Example

Forum Overview >> Farming Simulator 2009

CategoryFarming Simulator 2009
Created16.05.2010 19:22


Jim Panousis (Jimi540) 16.05.2010 19:25
Hello!

I made a trailer and it has a door. I want this door to open and close with a button. I read a lot other scripts and i understand whats is going on. I used the below script, but it didn't work. Some help to see what is wrong with this? Thanks!

In the modDesc.xml
_________________________________________________________________________
<inputBindings>
<input name="DOOR" key="KEY_KP_5" button="" />
</inputBindings>


In the .xml
_________________________________________________________________________
<door index="8" minRot="0 0 0" maxRot="-83 0 0" rotTime="2" touchRotLimit="10"/>



In the .lua
_________________________________________________________________________

PM28 = {};

function PM28.prerequisitesPresent(specializations)
return SpecializationUtil.hasSpecialization(Trailer, specializations);
end;

function PM28:load(xmlFile)

local rotationDoor = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.door#index"));
if rotationDoor ~= nil then
self.rotationPartDoor = {};
self.rotationPartDoor.node = rotationDoor;

local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.door#minRot"));
self.rotationPartDoor.minRot = {};
self.rotationPartDoor.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
self.rotationPartDoor.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
self.rotationPartDoor.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.door#maxRot"));
self.rotationPartDoor.maxRot = {};
self.rotationPartDoor.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
self.rotationPartDoor.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
self.rotationPartDoor.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

self.rotationPartDoor.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.door#rotTime"), 2)*1000;
self.rotationPartDoor.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.door#touchRotLimit"), 10));
end;

self.isDoorOpen = false;

end;

function PM28:delete()

end;

function PM28:mouseEvent(posX, posY, isDown, isUp, button)

end;

function PM28:keyEvent(unicode, sym, modifier, isDown)

end;

function PM28:update(dt)

if self.isEntered then
if InputBinding.hasEvent(InputBinding.DOOR) then
self.isDoorOpen = not self.isDoorOpen;
if self.rotationPartDoor ~= nil then
local x, y, z = getRotation(self.rotationPartDoor.node);
local rot = {x,y,z};
local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartDoor.maxRot, self.rotationPartDoor.minRot, 3, self.rotationPartDoor.rotTime, dt, not self.isDoorOpen);
setRotation(self.rotationPartDoor.node, unpack(newRot));
end;
end;
end;
end;

function PM28:draw()

end;

Friedrich L. (Unknown) 16.05.2010 20:02
--
-- Freibewegliche Türen
-- Spezialization um Türen um ihren Pivot öffnen zu lassen, Bestimmung von Minimal und Maximal
--
-- Author Friedrich L. - Skullman
-- Date 16. Mai 2010
-- Web www.modding-society.de
--
-- Copyright (C) Friedrich L. - All Rights Reserved.
--

Container = {};

function Container.prerequisitesPresent(specializations)
return SpecializationUtil.hasSpecialization(Motorized, specializations);
end;

function:Container:load(xmlFile)
local self.rotationDoorRight = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.rotationDoorRight#Index"));
if self.rotationDoorRight ~= nil then
self.rotationDoorRightmin = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.rotationDoorRight.minimal"),0);
self.rotationDoorRightmax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.rotationDoorRight.maximal"),270);
end;

end;

function Container:delete()
end;

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

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


function:Container(draw)

if self.self.rotationDoorRightmax then
g_currentMission:addHelpButtonText(g_i18n:getText("Tür zu"), InputBinding.min);
else
g_currentMission:addHelpButtonText(g_i18n:getText("Tür auf"), InputBinding.max);
end;

end;

function Container:update(dt)


local x,y,z = getRotation(self.rotationDoorRight);
if InputBinding.isPressed(InputBinding.min) and y < self.rotationDoorRightmin then
y = y - 0.01;
self.jointmove = true;
elseif InputBinding.isPressed(InputBinding.max) and y > self.rotationDoorRightmax then
y = y + 0.01;
self.jointmove = true;
end;
setRotation(self.rotationDoorRight, x,y,z);
self.jointmove = false;


end;

Jim Panousis (Jimi540) 17.05.2010 00:55
Thanks Skully for your script. I fixed some typografical errors to the headers of the functions and i tested. Black screen. I cant get into the game.
I'm taking always the error: unexpected symbol near '.' for the below line:

local self.rotationDoorRight = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.rotationDoorRight#Index"));

I changed the "components" to "rootNode" but nothing again. I can't see any error at this line. Do you?

With my the code i wrote above, i'm getting into the game normal but when i'm pressing the KP_5, as the code says, nothing happens. The script is not working and i can't see why too.
:(

Friedrich L. (Unknown) 17.05.2010 18:22
change:

if self.rotationDoorRight ~= nil then
self.rotationDoorRightmin = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.rotationDoorRight.minimal"),0);
self.rotationDoorRightmax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.rotationDoorRight.maximal"),270);
end;

to:

if self.rotationDoorRight ~= nil then
self.rotationDoorRightmin = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.rotationDoorRight#minimal"),0);
self.rotationDoorRightmax = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.rotationDoorRight#maximal"),270);
end;

And probe ;)

Jim Panousis (Jimi540) 17.05.2010 19:31
:) I saw that and i had fixed that too. Also i fixed the line:

if self.self.rotationDoorRightmax then

to

if self.rotationDoorRightmax then

But also with these changes i get the same error for the line:
local self.rotationDoorRight = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.rotationDoorRight#Index"));

I cant understand to which '.' is refering to the error. Now the whole code seems to be typographical right.

At my .xml :
<rotationDoorRight index="8" minimal="0" maximal="-83" rotTime="2" />

At the modDesc.xml at the InputBindings :
<input name="min" key="KEY_7" button="" />
<input name="max" key="KEY_8" button="" />

G. Chevalier (Unknown) 20.05.2010 15:11
This is incorrect :
"local self.rotationDoorRight"
"local" won't work with "self". Il you need a local variable use on only used in this function, then don't use "self."
If you need this value in update function for example, don't use local

For the input names in your modDesc.xml, please use more significant names to avoid issue with other mods.
For example use <input name="Nameofthemod_min" key="KEY_7" button="" />

Guillaume

Jim Panousis (Jimi540) 21.05.2010 11:48
thanks you thank you thank you thank you!!!
thanks Guillaume, now all works fine. I made some changes at the code for my application and all work excellent. At last when i'm making a change to my code it works immediately. Now i understand whats about the self, the locals etc.
Thanks to all of you who helped at this thread!
Best Regards,
Jimi.


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