Dump Class - jpbubble/NIL-isn-t-Lua GitHub Wiki
BEFORE YOU READ ON! Make sure you understand how NIL classes work!
NIL can dump out the values stored in a class variable, and it can work pretty easily. The feature has been put in for a quick way to make savegames and such.
Let's first set up a quick class
class DumpClass
int a
int b
int c
void CONSTRUCTOR(int a,int b,int c)
self.a = a
self.b = b
self.c = c
end
end
var DCA
var DCB
DCA = DumpClass.NEW(1,2,3)
DCB = DumpClass.NEW(0,0,0)
So far so good! Now let's do this:
string D
D = DCA[".dump"]
DCB[".dump"] = D
Ooh, what did we do? Easy, D contains a Lua script that contains all the data inside the class variable, that is all field data. Which was 1, 2 and 3 in the fields a, b and c respectively. By defining the ".dump" of DCB NIL executes that same Lua code in order to define those fields there. That should technically mean that DCB should now contain the same data as DCA does, right? Let's check!
print(DCA.a,DCA.b,DCA.c)
print(DCB.a,DCB.b,DCB.c)
Now the example above just merely copied data from one class variable to another (please note if you type DCB=DCA the data is not copied. Only the pointer to the class is, meaning that if you change data in DCB it also changes in DCA and vice versa. The method shown above won't make this behavior happen), but the prime function is most of all to allow you to put all data into a string and save that as a savegame string in accordance of your engines protocols for that.
NOTES!
- Classes cannot be recursed this way. If a field inside a class refers to another class that field is simply ignored. You will have to dump these out separately (at least for now, but I don't have a good idea how to fix that up in the future tbh, but I may think of something).
- Functions and userdata cannot be dumped, so fields containing that will also be ignored.
- When reading .dump static fields will not be dumped, but if you do .fulldump in stead, they will be. When defining .dump it doesn't matter if static values are present, but please note that static values affect ALL variables set for this class.
- When you use NIL in a World of Warcraft add-on, please note that the WOW serializer is not able to analyse classes or variables holding classes. One should therefore not put class variables among the saved variables. If you can use a variable containing the 'dump' outcome and save that in stead and make sure it's properly put in the class when the add-on loads, this should work. I used to be a WOW player, but I never went into the deep of add-ons, and since it's poorly documented at best (actually "hardly" may be a better word for this), I cannot really work out a good demonstration work out for this. Games and applications using a similar approach as Word Of Warcraft, may have similar issues.
- Please note that privates are NOT holy here. This routine was set up to make sure ALL data is preserved, so that includes privates. I would not really recommend to assign data to .dump anyway except for loading savegame purposes. We can deem this an 'unsafe' way to go.
- Last note also taken in order, you should not use this feature unless you are fully sure what you are doing.