Page 1 of 1

table wont work help me pls

Posted: Thu Apr 23, 2020 10:08 am
by nineret
this is a table

Code: Select all

local BlockTable = {
Block1 = {
x = 181,width = 409,y = 220,height = 220},
Block2 = {
x = 9999,width = 9999,y = 462,height = 462},
Block3 = {
x = 9999,width = 9999,y = 9999,height = 9999},
Block4 = {
x = 9999,width = 9999,y = 9999,height = 9999}
}
script I want to run
f

Code: Select all

function love.update(dt)
 move_up_down(dt)

end

Code: Select all

function move_up_down(dt)
  if love.keyboard.isDown("w") then
    for i, block in ipairs(BlockTable) do
       for h=1, block.Block[i] do
    if player.x > BlockTable[i].Block[h].x and player.x < BlockTable[i].Block[h].height and player.y > BlockTable[i].Block[h].y or player.y > ObjectTable[1] then
		player.y = player.y - 100 * dt
  else
    player.y = player.y - 0 * dt
  end end end end end
table wont work ;-;

Re: table wont work help me pls

Posted: Thu Apr 23, 2020 11:47 am
by steVeRoll
If you want a table to be an array, you don't define its entries with names (Block1, Block2) - just put them one after another. So, BlockTable should become this:

Code: Select all

local BlockTable = {
	{
		x = 181,width = 409,y = 220,height = 220},
	{
		x = 9999,width = 9999,y = 462,height = 462},
	{
		x = 9999,width = 9999,y = 9999,height = 9999},
	{
		x = 9999,width = 9999,y = 9999,height = 9999}
}
And in the ipairs loop, you can access the block simply by using the "block" variable, there's no need to index BlockTable manually.

Also, next time please describe your error with more detail. Instead of just "table won't work", specify what the error was and where it happened.

Re: table wont work help me pls

Posted: Thu Apr 23, 2020 11:56 am
by nineret
thank!

but I don't how to reply

Re: table wont work help me pls

Posted: Thu Apr 23, 2020 2:26 pm
by zorg
nineret wrote: Thu Apr 23, 2020 11:56 am but I don't how to reply
i mean, you just did... :monocle: