Re: Yet another insomia driven discovery.
Simple tutorial on storing and retrieving data in a PresetName:
Let's say you have an object that creates another object, and you want it to home in on a certain position that can only be retrieved from the first object. This means you have to pass on a vector coordinate. Storing a variable in the newly-created object won't work, as it will be reset to nil next update. The only way to do so is to use PresetName.
First of all, we store the vector values in the new object's PresetName. Make sure to take this step BEFORE adding the object with MovableMan:
Code:
object.PresetName = foo.X .. "," .. foo.Y;
Assuming the coordinates are {500,200}, this is storing them as "500 200".
Now we have to retrieve them in the new object's Create event:
Code:
for num in string.gmatch(self.PresetName, "%d+") do
if i == 0 then
self.bar.X = tonumber(num);
else
self.bar.Y = tonumber(num);
end
i = i + 1;
end
What we're doing here is using string.gmatch to return a list of strings matching a pattern. In this case, the pattern "%d+". "%d" searches for digits, and the "+" tells it to look for 1 or more repetitions of that type (in this case digits) after the first one is found. We then cycle through the loop using a counter. If it's the first loop, we take the X coordinate using tonumber (as gmatch returns a string). If it's the second loop, we take the Y coordinate.
It should be fairly easy to apply this to your own needs. If you want more info on patterns, this is a good link:
http://www.lua.org/pil/20.2.html