[SOLVED] need help with passing variables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
pch
Prole
Posts: 32
Joined: Sun Mar 04, 2012 5:17 pm
Location: poland

[SOLVED] need help with passing variables

Post by pch »

hello,

I'm trying to make a menu so it will be on right side of the screen and the game will stay on left side (game screen and menu share the same screen)

this is a whole file called menu.lua and it is then loaded from main.lua by require(menu)

as a swich between game and menu I chose TAB
when menu is active you can use arrows up/down to move (current position will be highlighted)
then you can use arrows left/right to change active (highlighted) position's values
i take the original values like this (4th position in each line of options table):
val = bspLvl
val = rMin
...

and here I have a problem that i cannot handle

how to update original value of each variable (4th position in each line of options table) according to its actual value from "options[lineNo].val" (changed when left/right arrow is pressed)?

for example :
options[1].val has a value of bspLvl
usin right arrow I will increase its value 1 step
now how to transfer this options[1].val back to bspLvl

probably there is another way of doing such thigs but it is unknown to me - that is all I could think of

Code: Select all

menu = {}

local options = 	{
		{ title = "bsp",              minV = 0,      maxV = 30,     val = bspLvl,  x = 50*fs, y = 3*fs+3 },
		{ title = "room min",     minV = 1,      maxV = rMax, val = rMin,     x = 50*fs, y = 4*fs+4 },
		{ title = "room max",    minV = rMin, maxV = 30,     val = rMax,    x = 50*fs,  y = 5*fs+5 },
		{ title = "wall distance",minV = 0,      maxV = 5,       val = wShift,  x = 50*fs, y = 6*fs+6 },
		{ title = "monsters",     minV = 0,      maxV = 50,      val = monNo, x = 50*fs, y = 7*fs+7 }
		}
					

local lineNo = 1

function menu.keyPressed(key)
	if key == "down" and lineNo < #options then
		lineNo = lineNo + 1
	end
	if key == "up" and lineNo > 1 then
		lineNo = lineNo - 1
	end
	if key == "left" and options[lineNo].val > options[lineNo].minV then
		options[lineNo].val = options[lineNo].val - 1
	end
	if key == "right" and options[lineNo].val < options[lineNo].maxV then
		options[lineNo].val = options[lineNo].val + 1
	end
end

function menu.draw()
	for i,v in ipairs(options) do
		if i == lineNo then
			love.graphics.setColor(255, 0, 0, 255)
		else
			love.graphics.setColor(200, 200, 200, 255)
		end
		love.graphics.print(v.title.." "..v.minV.."-"..v.maxV..": "..v.val, v.x, v.y)
	end
end
Last edited by pch on Fri Apr 13, 2012 8:54 pm, edited 2 times in total.
iemfi
Citizen
Posts: 52
Joined: Fri Mar 30, 2012 11:03 am

Re: need help with passing variables

Post by iemfi »

Umm, that's exactly correct? Doesn't seem like you need help :)
User avatar
pch
Prole
Posts: 32
Joined: Sun Mar 04, 2012 5:17 pm
Location: poland

Re: need help with passing variables

Post by pch »

I just made an edit - a added example so it will be easier to understand

i think I know
i have to write another bunch of lines like

Code: Select all

bspLvl = options[1].val
rMin = options[2].val
rMax = options[3].val
..
edit:

yes that was it

sorry, no more stupid questions - I think thats definitely enough for tonight
iemfi
Citizen
Posts: 52
Joined: Fri Mar 30, 2012 11:03 am

Re: need help with passing variables

Post by iemfi »

You could also "wrap" it in a table since tables are passed by reference.

Code: Select all


bspLvl={50}

options={title="bsp" val=bspLvl}

if key == "left" and options[lineNo].val[1] > options[lineNo].minV then
      options[lineNo].val[1] = options[lineNo].val[1] - 1
   end

Problem is that there's the added ugliness of having to refer to bspLvl[1] everytime instead so I think the simple method is better.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests