This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Melerion's profile picture

[LUA][Question] access function upvars

Started by Melerion, 18 October 2012 - 02:56 AM
Melerion #1
Posted 18 October 2012 - 04:56 AM
Hi,

I'm trying to implement a save function in a class, however I cannot for the life of me figure out how to access the content of the variables.

Suppose the following code in a file "testAPI"


-- test API - test saving params
function new()
	local self = {}
	local vara = "string"
	local varb = 1
	local varc = true
	local othervar = "string"
	local statusVars = {"vara", "varb", "varc"}

	function self:saveStatus()
		for _, v in pairs(statusVars) do
			-- not working!
			print("saving: " .. v .. " - " .. tostring(self[v]))
		end
		return true
	end

	function self:someFunc()
		-- meaningful code here
		self:saveStatus()
	end

	return self
end

and have a program that makes use of this API:

os.loadAPI("testAPI")
t = testAPI.new()
t:someFunc()

if I used the line

		print("saving: vara - " .. vara)

I can access the variable just fine, however I'd have to use one line per variable. And this I consider inelegant. Aside from the fact that a loop allows for the easy inclusion of additional error checking that would seriously blow up the code if copied over and over.

So the question is: how can I access the variables defined in the new() function within an enclosed function?
GopherAtl #2
Posted 18 October 2012 - 05:40 AM
in the for loop in saveStatus, the _ is catching the key, i.e., the string representing the name of the variable. Change it to k (conventionally _ means "not using this") and use it in your print stament.

Not 100% sure this answers your question, as I don't entirely understand your question, but this is my best guess, heh.
Melerion #3
Posted 18 October 2012 - 06:27 AM
Thanks for your answer, but that's not it.

The "_" variable in the loop is a throwaway holding the array key for the table statusVars, a numeric index.
ChunLing #4
Posted 18 October 2012 - 07:00 AM
You can load the entire thing into a table, then use textutils.serialize to save the entire table. That should kinda take care of your need to track the save progress too.
Cloudy #5
Posted 18 October 2012 - 10:39 AM
It would be better putting them all in a table. It's either that or you set a new environment for your function, declare the variables global and iterate through the environment - the end result would still be a table though.
Melerion #6
Posted 18 October 2012 - 12:16 PM
Thanks, this solution is so simple, it is almost embarrassing.

I usually try to keep the global namespace as clean as possible, so I won't go there. But having one var containing everything else makes it even easier to un/-serialize and store.
Cloudy #7
Posted 18 October 2012 - 07:07 PM
Thanks, this solution is so simple, it is almost embarrassing.

I usually try to keep the global namespace as clean as possible, so I won't go there. But having one var containing everything else makes it even easier to un/-serialize and store.

Well it still wouldn't be global - just relative to the function. But I agree the other solution is the better one.