Jump to content




variable input logic processing


  • You cannot reply to this topic
8 replies to this topic

#1 discodancepant

  • Members
  • 12 posts

Posted 04 June 2013 - 12: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.

#2 BigSHinyToys

  • Members
  • 1,001 posts

Posted 04 June 2013 - 12: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


#3 GopherAtl

  • Members
  • 888 posts

Posted 04 June 2013 - 01: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)


#4 discodancepant

  • Members
  • 12 posts

Posted 04 June 2013 - 01: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.

#5 BigSHinyToys

  • Members
  • 1,001 posts

Posted 04 June 2013 - 01:23 PM

Here is an example using GopherAtl method
Spoiler


#6 discodancepant

  • Members
  • 12 posts

Posted 04 June 2013 - 01: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?

#7 BigSHinyToys

  • Members
  • 1,001 posts

Posted 04 June 2013 - 01: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...ue-or-reference

#8 GopherAtl

  • Members
  • 888 posts

Posted 04 June 2013 - 02: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.

#9 discodancepant

  • Members
  • 12 posts

Posted 04 June 2013 - 07: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. :)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users