Declaring an unknown amount of 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
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Declaring an unknown amount of variables

Post by Helvecta »

Hello again!

I've been learning a ton since I've been lurking around here, and I've decided I want to rewrite most of what I have worked on recently to make it more efficient, friendly, and generally less glitchy.

One of these is to check the values of axes, but I want pretty much any controller to work, so I thought to write it like so:

Code: Select all

		local axis = {}
		for x = 0, controller["numAxes"] do
			axis[x] = love.joystick.getAxes( 1 )
		end
It's kinda obvious that it would just put the value of one axis for everything x represents, but I was praying otherwise. Why? Because I have no idea how to declare an unknown amount of variables, and I need to know how many axes I need to declare in order for love.joystick.getAxes to work correctly. I've looked high and low, but no bacon!

Can someone lay the knowledge on me? Thanks!
"Bump." -CMFIend420
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Declaring an unknown amount of variables

Post by Helvecta »

Ah! Figured it out; a pretty easy solution:

Code: Select all

		local axis = {}
		for x = 0, controller["numAxes"] do
			axis[x] = love.joystick.getAxis( 1, x )
		end
But if anyone knows how to declare an unknown number of variables, I would love to hear it! I'm sure I'll run into it again.
"Bump." -CMFIend420
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Declaring an unknown amount of variables

Post by Boolsheet »

Ooh, now I get you. You're having trouble with the multiple return values. They're called lists in Lua. See the third paragraph of 2.5 Expressions. There even are some examples.

One way to get the count of the list is with select.

Code: Select all

num_values = select("#", love.joystick.getAxes(1))
But this will not be necessary in your case as you apparently want to pack them directly into a table. You can do this instead.

Code: Select all

axis = { love.joystick.getAxes(1) }
It will put the return values in the integer keys, starting at 1. It's similar to this if it would return three values:

Code: Select all

axis = {}
axis[1] = love.joystick.getAxis(1, 1)
axis[2] = love.joystick.getAxis(1, 2)
axis[3] = love.joystick.getAxis(1, 3)
Note that the joystick id, axis id, and button id starts at 1. This was changed in LÖVE 0.8.0 to be more consistent with Lua.
Shallow indentations.
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Declaring an unknown amount of variables

Post by Helvecta »

Oh, wow! How did I overlook putting the function in the table. . . Ah well, part of the learning process :ultraglee:

Thanks, Boo- :shock: Boolsheet..!!



Also, here's some code that would do the same thing as the table solution but with select (I did it because I wanted to learn about how it is used. It's by no means more efficient, though.)

Code: Select all

	controller = {} 
	controller["numAxes"] = love.joystick.getNumAxes(1)
	
	local axis = {}
	for x = 1, controller["numAxes"] do
		axis[x] = select(x,jtk.getAxes(1))
		love.graphics.print(axis[x], 10, 10 * x)
	end
Also, here's a link to an example using select. Hope this helps someone in the future! And thanks again!
"Bump." -CMFIend420
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests