Community Forum

loadAttachmentsFromXMLFile doesnt work

Forum Overview >> Scripting

Created21.09.2020 12:44

Kenny Je (kenny456) 21.09.2020 12:44
Hi, i cant get this function work in my specialization.

function mySpec.registerOverwrittenFunctions(vehicleType)
SpecializationUtil.registerOverwrittenFunction(vehicleType, "saveAttachmentsToXMLFile", mySpec.saveAttachmentsToXMLFile)
SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadAttachmentsFromXMLFile", mySpec.loadAttachmentsFromXMLFile)

function mySpec:saveAttachmentsToXMLFile(superFunc, xmlFile, key, vehiclesToId)
local spec = self.spec_mySpec

if superFunc ~= nil then
superFunc(self, xmlFile, key, vehiclesToId)

function mySpec:loadAttachmentsFromXMLFile(superFunc, xmlFile, key, idsToVehicle)
local spec = self.spec_mypec

if superFunc ~= nil then
superFunc(self, xmlFile, key, idsToVehicle)

saveAttachmentsToXMLFile works good. I need to write something to savegame file after all vehicles are saved. But i need to read that information after reload savegame, but loadAttachmentsFromXMLFile function doesnt execute in my script. I especially need 'idsToVehicle'. Can this table be reached in some other function?

Bilbo Beutlin (BBeutlin) 21.09.2020 17:28
Trying to overwrite these functions is crap. Besides there is no function "loadAttachmentsFromXMLFile" (anymore). This is obsolete stuff from earlier FS.
Instead use the standard and always compatible ways.
For saving "myMod:saveToXMLFile(xmlFile, key, usedModNames)" - this is executed automatically (in specializations).
For loading "myMod:onLoad(savegame)" and register this function with "SpecializationUtil.registerEventListener()".
See LUADOC examples.

Kenny Je (kenny456) 22.09.2020 08:19
Thank you but i am not sure you understand my problem. I need to get specific vehicle (whole table) from savegame by id (this vehicle has not mySpec). Id is saved in savegame in vehicle with mySpec. Just like function 'loadAttachmentsFromXMLFile' from class 'AttacherJoints' (yes, this function really exists in FS19). Function loads this data from savegame (which are saved after all vehicles are saved in file i think and loaded after all vehicles are loaded ):
<attachments rootVehicleId="2">
<attachment attachmentId="3" inputJointDescIndex="1" jointIndex="6" moveDown="false"/>
<attachment attachmentId="6" inputJointDescIndex="1" jointIndex="3" moveDown="false"/>

I hope this make a sence.

Bilbo Beutlin (BBeutlin) 22.09.2020 09:08
If you need to modify a dedicated spec function, you should not use "SpecializationUtil.registerOverwrittenFunction()", since this could collide with the game engine function methods.
Besides, if you call the 'superFunc' the original method becomes executed.

Instead use "Utils.overwrittenFunction" (or according 'appended/prepended' if possible) and specify the target specialization. In this case "AttacherJoints.saveAttachmentsToXMLFile" and "AttacherJoints.loadAttachmentsFromXMLFile".

Kenny Je (kenny456) 22.09.2020 11:59
So i tried this:
AttacherJoints.saveAttachmentsToXMLFile = Utils.overwrittenFunction(AttacherJoints.saveAttachmentsToXMLFile, mySpec.saveAttachmentsToXMLFile)
AttacherJoints.loadAttachmentsFromXMLFile = Utils.overwrittenFunction(AttacherJoints.loadAttachmentsFromXMLFile, mySpec.loadAttachmentsFromXMLFile)

self.saveAttachmentsToXMLFile = Utils.overwrittenFunction(self.saveAttachmentsToXMLFile, mySpec.saveAttachmentsToXMLFile)
self.loadAttachmentsFromXMLFile = Utils.overwrittenFunction(self.loadAttachmentsFromXMLFile, mySpec.loadAttachmentsFromXMLFile)

Also tried appendedFunction / prependedFunction. In both cases function 'saveAttachmentsToXMLFile ' work in my script but 'loadAttachmentsFromXMLFile ' not. Function 'saveAttachmentsToXMLFile ' is allways execute after 'saveToXMLFile' (like in AttacherJoints spec.), but when loading there are only 'onLoad' and 'onPostLoad' executed but not 'loadAttachmentsFromXMLFile '.

Bilbo Beutlin (BBeutlin) 22.09.2020 14:17
If you see a certain function never is executed, obviously there's another method used actually. Then either this function is obsolete or perhaps reserved for future usage.

In this case you must search another function, which you can determine executed for sure, here eg. 'onLoad' or 'onPostLoad'.

Kenny Je (kenny456) 22.09.2020 15:41
I have these two functions in my script of course but there is no 'idsToVehicle' table which i need...

Bilbo Beutlin (BBeutlin) 22.09.2020 18:06
With 'onLoad' you get the 'savegame.xmlFile'. From this you can read the xml values according the tags in file <vehicles> <vehicle id=".." etc.

Kenny Je (kenny456) 22.09.2020 18:27
Yes i know that. I could get right vehicle according to my id. I can read all information from that file, but how can i get table of that vehicle like function 'idsToVehicle' do?

Bilbo Beutlin (BBeutlin) 22.09.2020 18:43
If the function isn't executed, simply call it yourself.
At first define
local idsToVehicle = {};
Then execute the function 'loadAttachmentsFromXMLFile(xmlFile, key, idsToVehicle)'
Afterward you can evaluate the table.

Kenny Je (kenny456) 23.09.2020 10:22
If i tried this, idsToVehicle is empty table as i expected. No vehicles are in there like original function in AttacherJoints.

Bilbo Beutlin (BBeutlin) 23.09.2020 12:35
Read the function code. It runs on a specified vehicle only and needs as key the 'rootVehicleId' (see savegame/vehicles.xml)..
For a complete list you have to scan the vehicles one after another.

On the other hand this is executed already at 'onLoad()'. Should be sufficient then to read the table 'vehicle.spec_attacherJoints.attacherJoints' (for each vehicle).

Kenny Je (kenny456) 30.09.2020 16:27
So i find solution. I can get table of target vehicle from 'g_currentMission.vehicles'. This table is indexed same way like vehicles in savegame.xml. Thank you for your time.

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