Community Forum

cant read vehicle xmlfile

Forum Overview >> Scripting

CategoryScripting
Created08.06.2021 15:07


Bob Bobster (Bobster82) 08.06.2021 15:07
Hello,

Im struggling with the xml part of modding. I can create/modify xml files, but i cant acces the vehicle xml files, for adding stuff to vehicles.xml file

for _, vehicle in ipairs(vehicleList) do
local xmlfile = vehicle.xmlFile

setXMLBool(xmlfile, "vehicle#test", true)
end;

'xmlfile' should be the vehicles xmlfile, but i cant read or add stuff to it. What am I doing wrong?

Bilbo Beutlin (BBeutlin) 08.06.2021 21:56
What kind of stupid idea is that, to change vehicle xml file in a running game?

The xmlFile is read once at game start into a buffer. Then it is used to setup the LUA variables for the vehicle itself as well as all its specializations. Also if you reload the xml, it is read from file (on drive). So changes on xmlFile afterwards won't have any effect.

However, you should be able to read the xml values of xmlFile. Though I'm not sure whether the xml buffer isn't closed after processing the load functions.

Bob Bobster (Bobster82) 08.06.2021 22:54
Thats a bit harsh Bilbo.

FS19 uses that file to save things to and load from. I also want to use that file to write to a certain vehicle (as many more mods do)
I just need a reference to a vehicle as everytime you start the game, the same vehicles have different reference points (like rootNode)
So writing in vehicle.xml when saving the game and load from that file when loading a game.

I want to give a certain vehicle some info that i want to save and reload at start.

Bilbo Beutlin (BBeutlin) 09.06.2021 10:44
Before you fiddle around with something, you should know what you're doing.

The LUA 'vehicle.xmlFile' is the vehicle XML file located in the FS install folder (or mod folder). If you change these files, the installation (or mod) is corrupted.

If you want to read or change the SAVEGAME's "vehicles.xml" use the default methods like documented.
That is at load/save time, append your function to the system's onLoad() or onSave() functions, resp. 'onPostLoad()' or 'saveToXMLFile()', either by using 'registerEventListener()' or by direct 'Utils.appendedFunction()'.
Independent from load/save you must open the "savegame/vehicles.xml" separately to work on it.

Bob Bobster (Bobster82) 09.06.2021 12:00
Thanks for answering (again ;) )

I know what I am doing and what I want, but Im having a hard time finding the working solution. (and explaining it here)
Maybe I wasnt clear enough that I dont want to do anything to the game files. I want to modify the vehicles.xml in the savegame directory ;)

I use a global script, not running on the vehicles itself, but it draws info from the vehicles and it saves some values on it (at runtime) I want to save/load those values

You say to use the default methods, but arent they used with scripts used on vehicles except global scripts?

Is there any other way then running scripts on the vehicles itself to load and save to that file?

Bilbo Beutlin (BBeutlin) 09.06.2021 16:26
Obviously you didn't know that the LUA variable 'vehicle.xmlFile' points to the basic {vehicle}.xml, not to the savegame. Though you would have known by reading carefully the documentations.

To get the "vehicles.xml" from savegame you must do the steps
- local xmlFilename = Utils.getFilename( "vehicles.xml" , g_currentMission.missionInfo.savegameDirectory )
You should check the savegameDirectory for 'nil' before, since it might not exist in a new (not saved) game.
Then read the xml file with
- local xmlBuffer = loadXMLFile('tempXML', xmlFilename)
Now you can read or write values in 'xmlBuffer' with the usual functions.

If you want to save the changed xml
- saveXMLFile(xmlBuffer)
Finally close the buffer to free allocated memory with
- delete(xmlBuffer)
Heed that an auto/save also changes the xml content and thus invalidates the buffer.

Be warned: any changes on the file you do on your own risc!

Bob Bobster (Bobster82) 09.06.2021 17:08
I cant argue on that I thought the vehicle.xmlfile was the savegame xml file. (couldnt find it in the docs, these docs are hard to go trough)
Thanks for clarify that part.


Now I have the vehicle.xml file, how can I know that, for example:
How do i refer to a certain vehicle in vehicle.xml?
g_currentMission.vehicles[1] -- 1st vehicle
vehicle.xml <vehicles><vehicle id="1"> -- is this the same vehicle?

So for my code:

local xmlFilename = Utils.getFilename( "vehicles.xml" , g_currentMission.missionInfo.savegameDirectory )
local xmlBuffer = loadXMLFile('tempXML', xmlFilename)

for index, vehicle in ipairs(vehicles) -- all owned vehicles/tools
local key = "vehicles.vehicle#id"
xmlid = getXMLInt(xmlBuffer, key)

if (index == xmlid) then
print("Same value");
end;

end;

Bilbo Beutlin (BBeutlin) 09.06.2021 17:40
I don't know for sure.
There are two vehicle tables, 'g_currentMission.vehicles' and 'g_currentMission.loadVehiclesById'.
I'd assume the first is the order of vehicles as they came ingame (were bought), the second is the order of appearance in "vehicles.xml", eg. order changed by vehicle reset.
Try out. ;)


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