Community Forum

Trying to export current vehicle speed

Forum Overview >> Scripting

Created29.07.2019 23:04

Tim Rehder (Unknown) 29.07.2019 23:04
I am trying to export the current speed of the vehicle I am sitting in.
I am not new to Programming but I am new to LUA and Mods for the Farming Simulator.
Thats the code I am trying to execute:

expData = {};
function expData.prerequisitesPresent(specializations)
return true;

function expData:loadMap(name)
expData.SpeedXMLFileId = loadXMLFile("speedXML", "C:/Users/timre/Documents/Speed.xml")
print("--- expData loaded --- (by rehti)")

function expData:deleteMap()

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

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

function expData:update(dt)
expData.currentVehecle = g_currentMission.controlledVehicle;
if not expData.currentVehecle == nil then
setXMLFloat(expData.SpeedXMLFileId, "speed.kmh", expData.currentVehecle:getLastSpeed())

function expData:draw()

The "modDesc.xml" file is correct. The script starts and prints "--- expData loaded --- (by rehti)" to the console.
But this if statement is not true, even if I am in a vehicle: "if not expData.currentVehecle == nil then"


Bilbo Beutlin (BBeutlin) 30.07.2019 00:26
1. The function prerequisitesPresent() is only used in specializations.

2. You can open an xml file for read OR write, but not for read AND write.

3. You should check the xml fileId to make sure it is valid (must be > 0).

4. To check controlledVehicle use "if g_currentMission.controlledVehicle ~= nil then"
It's also recommanded to check "g_currentMission:getIsClient()" and "g_gui.currentGui"

5. What do you intend with saveXMLFileToMemory()? That's only useful if you later loadXMLFileFromMemory().
If you save permanently to memory, this allocates RAM each time and sooner or later you're "out of memory".

6. For writing to file use a timer, which writes perhaps 2-5x per second. Else you'll lame the game and system by writing each update() cycle. Beneath an external program should have the chance to read the file.

Recommanded method for writing:
local xmlFileID = createXMLFile(objectName, fileName, rootNodeName)
if xmlFileID > 0 then
setXMLFloat(xmlFileID, attributePath, myValue)
saveXMLFile(xmlFileID) -- write to drive
delete(xmlFileID) -- free allocated memory

Tim Rehder (Unknown) 30.07.2019 00:51
Thanks a lot!
Everything works now. It helped me alot!

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