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

Read/Write variables in an API

Started by nxsupert, 20 April 2014 - 04:52 PM
nxsupert #1
Posted 20 April 2014 - 06:52 PM
Hello.
I am attempting to make a medium sized API. However , I have ran into a small problem. I have attempted to modify the value of a variable within the the API by a function call to the API. However when a retrieve the value of the variable via API.variable I have found that it is unchanged. I am asking if there is in fact a way to modify the value of a variable within an API via a function or whether variables withing an API are constant and can not be changed from within the API.
CometWolf #2
Posted 20 April 2014 - 08:25 PM
The reason this happens is because of the way os.loadAPI works. It runs the API within it's own environment table, then stores this table in the global environment under whatever you called the API. This essentially means that the variables in the loaded API table and the variables in the APIs functions do not share the same memory adress, ie they are not the same variable. Because the functions variable are using the internal API variables, meanwhile you're accessing the ones stores in the new API table. There's 3 ways to deal with this.

1)
Store the variables in a table. This works because tables are not recreated when passed around, meaning that if you defined t as a table, then did t2 = t, both t and t2 would be the same table.

2)
Use an API function to return the variable. This works because like mentioned above, the API functions still access the internal variables.

3)
Point the internal variables in your API functions to the loaded API table. I'd reccomend not doing this though, it could cause some derps.
Edited on 20 April 2014 - 06:26 PM
nxsupert #3
Posted 20 April 2014 - 08:49 PM
The reason this happens is because of the way os.loadAPI works. It runs the API within it's own environment table, then stores this table in the global environment under whatever you called the API. This essentially means that the variables in the loaded API table and the variables in the APIs functions do not share the same memory adress, ie they are not the same variable. Because the functions variable are using the internal API variables, meanwhile you're accessing the ones stores in the new API table. There's 3 ways to deal with this.

1)
Store the variables in a table. This works because tables are not recreated when passed around, meaning that if you defined t as a table, then did t2 = t, both t and t2 would be the same table.

2)
Use an API function to return the variable. This works because like mentioned above, the API functions still access the internal variables.

3)
Point the internal variables in your API functions to the loaded API table. I'd reccomend not doing this though, it could cause some derps.

Thank you for the information. I decided to use the second method you suggested as that would be a cleanest for me and technically speaking you are always meant to use Getters and Setters (unless that rule doesn't apply for Lua). It works a treat! Now to make the rest of this API :P/>