Please help a Lua newb?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Fourex
Citizen
Posts: 53
Joined: Tue Nov 10, 2009 2:33 am
Location: Earth

Please help a Lua newb?

Post by Fourex »

So, just got started with Love, (and programming in general) so I don't know all that much about how Lua works. I'm experimenting with tables, and have run into a problem when trying to repeat something for each argument in a table. I'm trying to replicate what I have done in part A with part B, but make it a repeating function(and 50 pixels lower), rather than writing each line out. When I run this, I get the error "main.lua:19: bad argument #1 to 'print' (string expected, got nil)"
Something tells me this is a simple fix, but I can't find it. Can anyone help? Thanks in advance!

Code: Select all

a = {1,1,2,3,5,8,13}

x = 0

function love.draw()

	     --***part A:***
	love.graphics.print(a[1], 100, 100)
	love.graphics.print(a[2], 110, 100)
	love.graphics.print(a[3], 120, 100)
	love.graphics.print(a[4], 130, 100)
	love.graphics.print(a[5], 140, 100)
	love.graphics.print(a[6], 150, 100)
	love.graphics.print(a[7], 160, 100)
	
        --***part B:***
	repeat
		x = x+1
		love.graphics.print(a[x], (100+(x*10-10)), 150)
	until x == 7

end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Please help a Lua newb?

Post by bartbes »

Because x is set to 0 only once, so in the second frame it will be higher, however, the usual lua solution is:

Code: Select all

for i, v in ipairs(a) do
    love.graphics.print(v, 100+(x-1)*10, 150)
end
User avatar
Fourex
Citizen
Posts: 53
Joined: Tue Nov 10, 2009 2:33 am
Location: Earth

Re: Please help a Lua newb?

Post by Fourex »

bartbes wrote:Because x is set to 0 only once, so in the second frame it will be higher, however, the usual lua solution is:

Code: Select all

for i, v in ipairs(a) do
    love.graphics.print(v, 100+(x-1)*10, 150)
end
So, that sorta works. I don't get an error, but all the numbers are on top of one another. And I don't understand at all what the parts of this code are, or how they work. Does this have a name so I can look it up?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Please help a Lua newb?

Post by bartbes »

Ah, I messed up the x in the x calculation should be i, then it works.
If you want details on ipairs, check out http://www.lua.org/pil/4.3.5.html
User avatar
Fourex
Citizen
Posts: 53
Joined: Tue Nov 10, 2009 2:33 am
Location: Earth

Re: Please help a Lua newb?

Post by Fourex »

Thanks! Now the code works, and I understand slightly more about ipairs.
Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests