12 posts
Posted 04 June 2013 - 01:23 PM
i have a section of code that repeats exactly with the logic processing, but each time with a different set of variables.
i could definetly just copy/paste my logic, and that'd work just fine, but rather than doing the easy thing, i want to learn the more efficient way.
i tried the following, with no effect;
function logic(current,maxium,next)
if arrow == "up" then
if (current > 1) and (current <= maximum) then
elseif current == 1 then
current = maximum
elseif current == 0 then
current = 1
end
elseif arrow == "down"
if (current >= 1) and (current < maximum) then
elseif current == maximum then
current = 1
elseif current == 0 then
current = maximum
end
end
return
end
i know that the code goes wrong whenever i write "current ="
but i'm not sure how to change the value of current, when current itself is a variable of an index's value.
992 posts
Posted 04 June 2013 - 01:45 PM
If you could give some more information about this functions purpose I might be able to help. some variables are not defined in your code.
Is this similar to the functionality you are looking for.
Spoiler
-- this is how I normaly do it
local termX,termY = term.getSize()
local posX = math.floor(termX/2)
local posY = math.floor(termY/2)
while true do
term.clear()
term.setCursorPos(posX,posY)
term.write("X")
local event = {os.pullEvent()}
if event[1] == "key" then
if event[2] == 200 then -- up key
posY = posY - 1
elseif event[2] == 208 then -- down key
posY = posY + 1
elseif event[2] == 203 then -- left
posX = posX - 1
elseif event[2] == 205 then -- right
posX = posX + 1
elseif event[2] == 14 then -- Press [Backspace] key to exit
break
end
if posY > termY then
posY = termY
elseif posY < 1 then
posY = 1
end
if posX > termX then
posX = termX
elseif posX < 1 then
posX = 1
end
end
end
871 posts
Posted 04 June 2013 - 02:05 PM
current is an argument to the function; changing it will
not affect the variable that was passed
into the function.
example to explain/demonstrate:
--i exists outside function...
local i=1
--function takes an arg, called i. this is unrelated to the i declared above, and effectively "hides" that i while inside the function
local function increment(i)
i=i+1
end
--will print "1"
print(i)
increment(i)
--will still print "1"
print(i)
This could be resolved a couple of ways. The simplest is probably going to be returning the new, modified value of current at the end of the function, and catching it. Doing this with my example above would give this:
local i=1
local function increment(i)
i=i+1
return i
end
--will print "1"
print(i)
i=increment(i)
--will print "2" this time
print(i)
12 posts
Posted 04 June 2013 - 02:22 PM
i'm trying to access a table which has all the characteristics of my menu and sub menus and the possible selections therein.
i store my current selection in another table
i then get my current level variable, the maxLevels in the current level and the next level variable.
with that i can either use my redundant logic to move between sub menus, go another layer in (sub sub menu) or exit the current sub menu, going back to the menu.
this last function i want to be variable based on current level, max level and next level, so that my code does not repeat for every possible instance of menus.
992 posts
Posted 04 June 2013 - 02:23 PM
Here is an example using GopherAtl method
Spoiler
local termX,termY = term.getSize()
local posX = math.floor(termX/2)
local posY = math.floor(termY/2)
local function range(nInput,nMin,nMax)
if nInput > nMax then
nInput = nMax
elseif nInput < nMin then
nInput = nMin
end
return nInput
end
while true do
term.clear()
term.setCursorPos(posX,posY)
term.write("X")
local event = {os.pullEvent()}
if event[1] == "key" then
if event[2] == 200 then -- up key
posY = posY - 1
elseif event[2] == 208 then -- down key
posY = posY + 1
elseif event[2] == 203 then -- left
posX = posX - 1
elseif event[2] == 205 then -- right
posX = posX + 1
elseif event[2] == 14 then -- Press [Backspace] key to exit
break
end
posX = range(posX,1,termX)
posY = range(posY,1,termY)
end
end
12 posts
Posted 04 June 2013 - 02:33 PM
ahh, yes i see, that should help a lot.
if i don't use return (variable) then any changes i attempted to make simply don't occur globally outside of the function
is that a correct summary?
992 posts
Posted 04 June 2013 - 02:47 PM
variables like "strings" and "numbers" are not passed as a reference . The content of the variable is given to the function and the function works on this information then can return a variable the returned variable can be put into any variable.
local function addOne(input) -- assign the first variable passed to this function the name "input"
local input = input + 1 -- add one
return input -- return a varible
end
local nNumber = 10
local OtherNumber = addOne(nNumber)
print(nNumber)
print(OtherNumber)
functions take input's and can return an output this doesn't change the variables that were used as inputs *(with the exception of tables they work slightly different.)
[EDIT]
This explains it well.
http://stackoverflow.com/questions/6128152/lua-function-variable-scope-pass-by-value-or-reference
871 posts
Posted 04 June 2013 - 03:16 PM
yes, that is a correct summary, with one exception. tables passed in as parameters ARE changed in the original variable - unless you actually assign a new table entirely. Ex, in "function foo(argTable)", "argTable[1]='foo'" would change the original table passed in; argTable={} would not.
12 posts
Posted 04 June 2013 - 08:01 PM
well, that's astoundingly helpful. Sorry if my questions seem a little noobish. I'd rather ask a dumb question than make garbage code. thanks again for the quick replies. :)/>