|Bilbo Beutlin (BBeutlin)||05.07.2015 23:44|
|I have created a small utility mod which handles hotkeys to toggle quickly between vehicles.
Works fine, but now I want to save and reload the hotkeys together with the "savegame/vehicles.xml".
I thought, adding a function to "Drivable.getSaveAttributesAndNodes" and "Drivable.loadFromAttributesAndNodes" was the best way by using "func0 = Utils.appendedFunction(func1, func2)".
However I don't get it baked. Placed in 'myMod:loadMap(name)'
Drivable.getSaveAttributesAndNodes = Utils.appendedFunction(Drivable.getSaveAttributesAndNodes, myMod.onSave)
doesn't work. Neither my attributes nor the 'Driveable' attributes are saved. Tried already exchanging func1 <-> func2, no result. Also no error in log. Same thing with ".loadFromAttributesAndNodes". The "myMod:onSave()" as well as "myMod:onLoad()" functions have exactly the same structure as referenced in Script-Docs getSaveAttributesAndNodes and loadFromAttributesAndNodes.
So what's the trick with this appendedFunction?
|Emil Drefers (Unknown)||06.07.2015 08:09|
first a general thought.
The hotkeys are so to say global?
In this case I would not save the information per vehicle.
Or is there a reason why that would make sense?
If you have a look at the script Baler.lua
you can see how a function can be appended.
|Bilbo Beutlin (BBeutlin)||06.07.2015 12:50|
|My hotkeys are vehicle specific, so I think the "vehicles.xml" was the best place to save.
When sitting in a vehicle, rCtrl + NumPad_key assigns the hotkey. Then from anywhere lCtrl + NumPad_key switches to that vehicle.
In the xml there should be added a simple attribute <vehicle id="x" ... hotKey="n" />
However the 'func0 = Utils.appendedFunction(func1, func2)' doesn't work here. I'd expected it concatenates two functions, so that func1 gets executed, then func2, where func0 overrides the calling orig. function.
Like described above, neither the 'Drivable.func1' gets executed, nor my 'myMod.func2'. For test I coded a simple print(..) instruction and dummy return values.
I've seen also other appendedFunction examples in docs and elsewhere. The 'Baler.lua' doesn't really help, from my sight the 'getIsTurnedOnAllowed' is executed twice?
So what's wrong with my code
Drivable.getSaveAttributesAndNodes = Utils.appendedFunction(Drivable.getSaveAttributesAndNodes, myMod.onSave) ?
I placed it in 'myMod:loadMap(name)' , my function is for test merely
return nil, nil;
The result is just nothing. Neither the print instruction is executed, nor the Drivable tags (cruiseControl, operatingTime, attacherJointComboDir) are written to xml file. And no error in log.
|Emil Drefers (Unknown)||07.07.2015 08:37|
u protect tyour system from assigning one hotkey to two vehicles at the same time it might be OK to save the binding per vehicle.
About the Baler.lua ...
self.getIsTurnedOnAllowed = Utils.overwrittenFunction(self.getIsTurnedOnAllowed, Baler.getIsTurnedOnAllowed);
So, the function getIsTurnedOnAllowed is eventually executed two times.
First the fucntion in the Baler.lua is executed, but this function might execute the superFunction (original function which has been overwritten).
In contrast to this:
self.setFillLevel = Utils.appendedFunction(self.setFillLevel, Baler.setFillLevel);
setFillLevel is executed "two times".
First the original function and afterwards the function of the Baler.lua
Well, if you would apply your mod's functionality as a specialization to the vehicles the load and save functions would get called automatically.
|Bilbo Beutlin (BBeutlin)||07.07.2015 15:08|
|I'm wondering only why the 'Utils.appendedFunction' doesn't work at all for the Drivable.getSaveAttributesAndNodes?
I don't find anything what could be wrong with my code. Is there anything like a system protection?
Was it possible to manage something with the 'superClass()' functions?
So if 'Utils.appendedFunction' doesn't work, I'm afraid 'Utils.overwrittenFunction' also wouldn't.
And finally I had to use "Drivable.getSaveAttributesAndNodes = myMod.onSave" what I would have liked to avoid for compatibility reasons.
And yes, the hotkey assignment tests existing hotkeys and deletes case allocated already. But from design nothing could go wrong, the first appearance would be used.
Well *sigh* - perhaps I should consider to write a separate vehicle spec. - though I'd wished to avoid that with such simple function extension.
Anyway much thanks for your answers.
|Emil Drefers (Unknown)||07.07.2015 15:21|
The 'appended approach' does not work, because the return values of the functions can't be handled this way.
The 'overwritten approach' should work, if you handle the return values in your new function correctly.
(call the the superFunc and append your new/custom attributes/values)
The superClass does not help in this case, at least I don't see how it could.
You're right ... to overwrite the original function with the hardcore method should be avoided in any case : - )
|Bilbo Beutlin (BBeutlin)||08.07.2015 01:40|
|Thanks for your hints, Emil : )
For my mod I now decided to use a vehicle spec. - it allows me easier extensions, I'm quite sure in future I'll have a few other ideas. ; )
Note: Log in to post. Create a new account here.