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

One more variable question

Started by emufossum13, 16 January 2013 - 03:23 PM
emufossum13 #1
Posted 16 January 2013 - 04:23 PM
ok I'm sorry for all the questions, but I'm new to lua (only been using it for about a month)
This is the last question about variables (i promise)
I was wondering if there was a way to declare an "unknown" variable
this way I could use a global variable to store a number that I keep adding to, without resetting that variable when the program starts over
Luanub #2
Posted 16 January 2013 - 05:01 PM
If you're wanting a persistant variable just use either the FS or IO API and save the variable to a file and read its value each time the program starts.

Here's an example

--lets make sure the file exists, this is more for error handling.
if not fs.exists("varSaves") then  --checks if file exists
  local file = io.open("varSave", "w")  -- if not open the file "varSave" in write mode
  file:write() -- write the file
  file:close() -- close the handle, always do this.
end

--now lets save the variable to the file. Will use y from your previous post.
local file = io.open("varSave","w")
file:write(y) -- write y to the file
file:close()

--now lets assign whats in the file to y
local file = io.open("varSave", "r") -- open in it read only mode
y = file:read()
file:close()

Depending on how you're wanting to it you may want to create some functions to handing the read and writes. Hope this helps.
ChunLing #3
Posted 17 January 2013 - 08:57 AM
Hmmm…I don't know that the question is really about being able to store information so that it is retained in the event of a shutdown.

It looks more like the question is whether variables are variable rather than constant. To which the answer would be a simple yes, you can change the value of a variable as often as you like. In Lua, you can even change the type (though really this is just assigning a new variable to be used with the identifier of the old variable, leaving the old variable without a reference so that it is left to garbage collection). I know that it seems unlikely that this could be the question, but I've seen stranger ones.
Orwell #4
Posted 17 January 2013 - 09:49 AM
I think he's asking for nothing else than declaring variables out of the scope of the running program. Which basically is the same as declaring them without the local keyword. :)/> Why do you need the variables across restarting your program?
emufossum13 #5
Posted 18 January 2013 - 01:22 PM
Sorry, I should have explained a little more about what I'm doing. But Lua Nub hit the nail right on the head. Thank you very much for your responses. I'm creating an automatic ATM, where people have to insert physical tokens through a pipe system. Only certain items are accepted (wrong items filtered out) and those represent 1, 10, and 50 dollars. When the item hits it's "gate" it will send a redstone signal to a certain side of the computer, and the program will know how much money to add. However, I couldn't figure out how to keep the same information on one variable through using a for loop. Let's say y was the amount of money to add too. This is one thing I hate about Lua, is that in order to declare a variable it's gotta have a value. Well whatever value I set y too (usually 0 as default) It would go back to that value, erasing any already stored information. This would happen either every time I started the program, or every time the for loop came back around. So this information will really help, thank you very much. :D/> :D/> :D/> :D/> :D/> :D/> :D/> B)/>
ChunLing #6
Posted 18 January 2013 - 01:28 PM
You can declare a variable without assigning it (or you can assign it nil, if you want, same thing).

And if you omit the local keyword as Orwell suggested, then you get a global variable (which will persist until the computer is shutdown/rebooted or the identifier for the variable is assigned to nil).
theoriginalbit #7
Posted 18 January 2013 - 01:29 PM
This is one thing I hate about Lua, is that in order to declare a variable it's gotta have a value.
Not quite true… the following would work

local someVar
if someBool then
  someVar = "this"
else
  someVar = "that"
end
We actually create the variable there as a local but with no value ( its nil )… we assign it a value later.

EDIT: Damn ChunLing ninja'd… but I have code example :P/>
Edited on 18 January 2013 - 12:30 PM