table value assignement problem.

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
Polaris
Prole
Posts: 9
Joined: Thu Aug 28, 2008 8:50 pm

table value assignement problem.

Post by Polaris »

here is a little chunk of code where i have a problem to assign value to an array.
before bothering the board i have googled and also look at stackoverflow site.
i don't understand why my local add var is not incrementing during my for loop.
can someone give me an advice on what goes wrong?
i also tried with pairs but the result is the same .
so all value are 10 inside the array instead of 10,20,30...and so on.

Code: Select all

function love.load()
an_array={{0},{0},{0},{0},{0},{0},{0},{0}}
put_value_in()
end

function love.draw()
	for b = 1,8 do
		printinfo(an_array[1][1],30*b,10)
	end
end


function printinfo(text,x,y)
	love.graphics.print(text,x,y)
end


function put_value_in()
	local add = 10
	for i =1,8 do
		an_array[i][1]= add
		add = add+10
	end
end
thanks in advance for your time.
sorry for my noobism.
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: table value assignement problem.

Post by Helvecta »

Heya! Your problem was easy to overlook, love.draw was

Code: Select all

    function love.draw()
       for b = 1,8 do
          printinfo(an_array[1][1],30*b,10)
       end
    end
instead of

Code: Select all

 printinfo(an_array[1][b],30*b,10)
So in the for loop each time it came around it printed an_array[1][1] - which is 10! changing the [1] to makes it print each value and yadda yadda :)

The revised code:

Code: Select all

    function love.load()
    an_array={{0},{0},{0},{0},{0},{0},{0},{0}}
    put_value_in()
    end

    function love.draw()
       for b = 1,8 do
          printinfo(an_array[1][b],30*b,10)
       end
    end


    function printinfo(text,x,y)
       love.graphics.print(text,x,y)
    end


    function put_value_in()
       local add = 10
       for i = 1, 8 do
          an_array[1][i] = add
          add = add + 10
       end
    end
Hope this helped!
User avatar
Polaris
Prole
Posts: 9
Joined: Thu Aug 28, 2008 8:50 pm

Re: table value assignement problem.

Post by Polaris »

god!

only careless, about my code i missed that... dealing with that for more than 5 hours...
sorry to have bother.

this is part of much bigger code, and when you read too much code for too long you don't see what is wrong.

you can start throwing me some stone or whatever... :ultrashocked:

thanks so much
sorry again
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 207 guests