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

[Lua][Question] reaching a variable in an API

Started by InputUsername, 24 December 2012 - 04:58 AM
InputUsername #1
Posted 24 December 2012 - 05:58 AM
Hello.

I don't really know how to describe this problem but I'll do my best.

I'm currently trying to make a button API (I'M AWARE THAT THERE ARE ALREADY TONNES OF THEM ON THE FORUMS) which stores all buttons in a table called tButton. The buttons are tables themselves. The problem I'm facing is that in the API there is a function called init(), which initializes tButton.

On one of my computers the 'startup' program loads the API.
Another program utilizes this API.

Now my question is: how do I change or 'reach' (I don't know a better word) the variable tButton or any of the buttons (tables) in it?

For example:

print( tButton[1].x )

where x is a variable in the first table in the table tButton (tableception?).

Because doing so will throw an error and tButton doesn't exist and stuff.

I would greatly appreciate a quick response or even a response at all.

Thanks in advance!
InputUsername
Orwell #2
Posted 24 December 2012 - 06:25 AM
If the API's filename is buttonAPI, then you'd load it like this:

os.loadAPI('buttonAPI')
And access it like this:

print( buttonAPI.tButton[1].x )
remiX #3
Posted 24 December 2012 - 08:15 AM
What people usually do is have a function like getVersion() which returns the version of the api.
So make a function within the api to return button values


function getValue(_buttonNumber, value)
	return tButton[_buttonNumber][value]
end

I might be wrong with the indexing there :P/> But something like that

Then use it like:

print(buttonAPI.getValue(1, x))
InputUsername #4
Posted 24 December 2012 - 11:26 PM
If the API's filename is buttonAPI, then you'd load it like this:

os.loadAPI('buttonAPI')
And access it like this:

print( buttonAPI.tButton[1].x )
I tried this but it still doesn't work. Also tried it with

buttonapi.tButton[1][x]
but that would also throw the same error (trying to index ? (a nil value))

What people usually do is have a function like getVersion() which returns the version of the api.
So make a function within the api to return button values


function getValue(_buttonNumber, value)
	return tButton[_buttonNumber][value]
end

I might be wrong with the indexing there :P/> But something like that

Then use it like:

print(buttonAPI.getValue(1, x))
I was already afraid of someone suggesting this because it's not the most elegant solution :P/> but I suppose, because nothing else works I will have to stick to this option :)/>

Thanks to both of you :)/>

EDIT: both options fail. If anyone knows a better solution (or at least can explain what I'm doing wrong) then that would be awesome.
Orwell #5
Posted 24 December 2012 - 11:30 PM
I was already afraid of someone suggesting this because it's not the most elegant solution :P/> but I suppose, because nothing else works I will have to stick to this option :)/>
Actually, using getter functions is usually seen as a rather elegant solution. :)/> It's more robust than directly altering the table (e.g. you can define conditions and restricitions in the getter).
InputUsername #6
Posted 25 December 2012 - 12:28 AM
I was already afraid of someone suggesting this because it's not the most elegant solution :P/> but I suppose, because nothing else works I will have to stick to this option :)/>
Actually, using getter functions is usually seen as a rather elegant solution. :)/> It's more robust than directly altering the table (e.g. you can define conditions and restricitions in the getter).
Really? I didn't know :P/>

Anyway, I tried using a get and set function:


function getValue(number,variable) --number = button number, variable = variable to return the value of
  return tButton[number].variable
end

--I tried two different 'set' functions:

function setValue(number,variable,value) --number = button number, variable = variable to change, value is new value for the variable
  tButton[number].variable = value
end
--or
function setValue(number,variable,value)
  tButton[number][variable] = value
end

The get function doesn't error but both set functions do.




I do know a solution but it requires me to mess around with the entire api;

changing the variable names to keys (ex. x = 2 would be "x" = 2, so that it can be 'reached' (lol) using tButton[1]["x"] = (value)
remiX #7
Posted 25 December 2012 - 12:54 AM
What error does it give exactly?