3 posts
Location
Bothell, WA
Posted 14 February 2014 - 02:42 PM
This might seam a simple or stupid question but becasue I have encountered this issue and what "should" be working is not working I must now ask the question anyway.
How do you change a variable once it has been set?
menuId = 1
That is not working in my script. When I first declared the variable I set it with an initial value of 0 but I try to change it in 2 seperate places but instead of changing it remains set to 0. This is a crittical issue as without the ability to change a variable there is no way for me to continue writing my program. The tutorial on
variables is absolutely useless as I have read it multiple times.
Initial setting =
local menuId = 0
Attempts to change it within a function later in the script with =
menuId = 1
results, variable retains intial set value of 0
8543 posts
Posted 14 February 2014 - 02:48 PM
Please post the script that you're noticing the issue in and what is leading you to believe that this is an issue. I suspect that you are not yet fully aware of the scope rules in Lua, so it will be easiest to find and explain any misconceptions if we can see what is leading you this way.
3 posts
Location
Bothell, WA
Posted 14 February 2014 - 02:53 PM
I do not know of a way to copy to code out of the Turtle I am writing it on in order to paste it.
Building a menu system and when the switch from 1 menu to a second failed I had the program print the contents of menuId and that is when I discovered that it was not being changed.
8543 posts
Posted 14 February 2014 - 02:57 PM
If you have http enabled, you can use the pastebin program to upload it. If you are working on it in a singleplayer world, run `id` on the turtle. Go to your world save folder, find the computer directory, then open the folder corresponding to the ID of the turtle. You'll be able to open the files in any text editor to copy and paste from them.
3 posts
Location
Bothell, WA
Posted 14 February 2014 - 02:58 PM
Just figured out how to use pastebin to get the code
-- Turtle Tree Farm
-- Written by Krahazik
-- Version 0.1a140214.10
local menuId = 0
local cornerA = nil
local cornerB = nil
---- Detect Key Press ----
function getKeyInput()
bRead = true
while(bRead) do
local sEvent, param = os.pullEvent("key")
if sEvent == "key" then
if param == 3 then
term.clear()
print("#2 key pressed")
print("Menu ID= "..menuId)
if menuId == 1 then
menuSetup()
end
end
if param == 10 then
if menuId == 2 then
menuMain()
end
end
if param == 11 then
tearm.clear()
break
end
end
end
end
-- MENU SYSTEM ---------------------
function menuHeader()
term.clear()
print("===== Turtle Tree Farm =====")
print(" Version 0.1a")
print("----------------------------")
end
function menuMain()
menuHeader()
menuId = 1
print(" MAIN MENU")
print(" 1 - Start")
print(" 2 - Setup")
print(" 0 - Exit")
term.setCursorPos(5,10)
getKeyInput()
end
function menuSetup()
menuId = 2
menuHeader()
print(" Setup Menu")
getKeyInput()
end
------------------------------------
mainMenu()
Pastebin -
http://pastebin.com/azH3714w
7083 posts
Location
Tasmania (AU)
Posted 14 February 2014 - 05:16 PM
When you run that code, "menuId" gets defined as local to the script. That means that any function in that script can see what's in it and manipulate that value, but once the script ends, the variable gets discarded.
You functions are stored against variables, too - take "menuMain", for example. Only you're not defining your functions as local, so by default, they end up as global - they remain in your computer's memory even after the script finishes up.
At the bottom of your script, after defining your variables, you call "mainMenu". But, your script has no function by that name!
Odds are the only reason this doesn't crash is that earlier, you DID define a function with that name, and since it is global that remained loaded in memory (allowing you to call it without defining it again - but that means it hasn't changed since then). If you rebooted the computer, it'd be wiped and your script wouldn't work at all (it'd crash out with an "attempt to call nil").
Try changing that last line to "menuMain()".
Edited on 14 February 2014 - 04:20 PM