Making Use of Serialised Variables?

Posted by Simimi on Sat 01 Sep 2007 09:10 AM — 5 posts, 23,145 views.

#0
I have a variables serialised as follows:

data = {}
  data.enchantments = {}
    data.enchantments.perfection = 108386
    data.enchantments.kingdom = 55407
    data.enchantments.mercy = 114946
  data.cube = {}
    data.cube.cube = 67697

In theory, I would be adding to the data.enchantments table at any time. I want to have an alias (recharge) that send, in psuedocode;

recharge first thing in data.enchantments (in this case 108386) from cube# (in this case 68697)


I am trying to make an alias to recharge a list of random enchanted jewlery from a given cube, but I want to set it up such that it can be another plugin for people to use. I could not think of a way to do this via script functions and an alias, so I decided to use a serialised variable instead.
The syntax for the in game command is;

Syntax: RECHARGE <item> FROM <cube>

So recharge would have to send the command for each thing in the data.enchantments table, for whatever given person has...

I guess this is 2 questions in essence...
1) How do we interface with a serialised variable, to use the information in an alias?
2) Is there a 'better' (or more plugin/user friendly) way to achieve this goal, aside from a serialised plugin?

Yours,
Mimi
Australia Forum Administrator #1
The quick answer is you can use loadstring to get Lua to "read" the string back into the same table it came from. It isn't totally clear if that is what you want.

http://www.gammon.com.au/forum/?id=4960


If you really want the variable serialized into some sort of local variable, see setfenv:

http://www.gammon.com.au/scripts/doc.php?lua=setfenv


Either way, it doesn't seem to me to be a very natural way of doing things, to get one plugin to communicate to another, or are you doing something like CallPlugin to send data from one plugin to another.
#2
I'm just trying to get the alias itself to work, and then I want to package it as a plugin for others to use. It does not seem natural to me but I can not think of any other way other than a bunch of mush variables and a big fat alias...

Which might not be a bad idea now that I think about it...
USA #3
Although I am not sure of what the whole kingdom/perfection/mercy thing is, probably the best thing to do to grab the 'first' thing in a list is to make a list with numbers as keys instead of words.

data = {}
  data.enchantments = {}
    data.enchantments.perfection = 108386
    data.enchantments.kingdom = 55407
    data.enchantments.mercy = 114946
  data.cube = {}
    data.cube.cube = 67697
  data.order = {}
    data.order.1 = "perfection"
    data.order.2 = "kingdom"
    data.order.3 = "mercy"


Then later, just call the first thing in the list with data.enchantments[data.order[1]] and use table.insert and table.remove to add and remove data to the order list. You can even make it a priority queue by inserting the new enchantment before certain other enchantments.
#4
Ahh ok, that helps a lot! I didn't even think of table.sort and .remove . Thanks once again, Mr. Biggs