Page 1 of 1

how to call an array to draw an object after a certain amount of DT has passed?

Posted: Sat Oct 28, 2023 2:49 am
by viviavi
im currently working on my first game, and i have a way to brute force a functionality thats good enough for this project, but with significantly more lines of code than id likely need if i could figure out how to call an array.

Code: Select all

Counter = 0 

function love.load ()
    require "enemy" 
        enemy = Enemy()
        enemy2 = Enemy()
        enemy3 = Enemy()
        enemy4 = Enemy()
            --[[ect... 

function love.update(dt) 

Counter = Counter + dt

 
 if counter >= 0 then 
      enemy:update(dt)
end
  if Counter >= 3 then
      enemy2:update(dt)
end
  if Counter >= 6 then
      enemy3:update(dt)
 end
  if Counter >= 8.6 then
      enemy4:update(dt)
end
    --[[ ect... 
and than doing the same thing in love. draw() with the same numbers but draw instead of update.

i know how to set up an array to draw something every time a key is pressed, but im not sure how to call it once my counter variable reaches certain numbers

additionally, how would i increase the required counter number each time the array draws a new object?

Re: how to call an array to draw an object after a certain amount of DT has passed?

Posted: Sat Oct 28, 2023 2:45 pm
by darkfrei
viviavi wrote: Sat Oct 28, 2023 2:49 am im currently working on my first game, and i have a way to brute force a functionality thats good enough for this project, but with significantly more lines of code than id likely need if i could figure out how to call an array.

Code: Select all

Counter = 0 

function love.load ()
    require "enemy" 
        enemy = Enemy()
        enemy2 = Enemy()
        enemy3 = Enemy()
        enemy4 = Enemy()
            --[[ect... 

function love.update(dt) 

Counter = Counter + dt

 
 if counter >= 0 then 
      enemy:update(dt)
end
  if Counter >= 3 then
      enemy2:update(dt)
end
  if Counter >= 6 then
      enemy3:update(dt)
 end
  if Counter >= 8.6 then
      enemy4:update(dt)
end
    --[[ ect... 
and than doing the same thing in love. draw() with the same numbers but draw instead of update.

i know how to set up an array to draw something every time a key is pressed, but im not sure how to call it once my counter variable reaches certain numbers

additionally, how would i increase the required counter number each time the array draws a new object?
Welcome to the forums!
Do what you need inside the objects:

Code: Select all

 -- require lib:
Enemy = require ("enemy") 

function love.load ()-- set counter
	Counter = 0
	enemy1 = Enemy:newEnemy (Counter)
	enemy2 = Enemy:newEnemy (Counter + 3)
	enemy3 = Enemy:newEnemy (Counter + 6)
	enemy4 = Enemy:newEnemy (Counter + 8.6)
	enemies = {enemy1, enemy2, enemy3, enemy4}
end

function love.update (dt)
	Counter = Counter + dt
	for i, enemy in ipairs (enemies) do
		enemy:update (Counter)
	end
end

-- same in love.draw
You can also provide to each enemy just dt and store own counter inside the each object.

Code: Select all

function love.update (dt)
	for i, enemy in ipairs (enemies) do
		enemy:update (dt)
	end
end

Re: how to call an array to draw an object after a certain amount of DT has passed?

Posted: Sat Oct 28, 2023 10:25 pm
by dusoft
I mean what's difficult about arrays? They work similarly in each language (that has them). In Lua it's even easier, because Lua has tables. And tables can be anything you need, objects, arrays, you choose.

Code: Select all

enemies={}
for i=1,5 do
	enemies[i]=Enemy()
end
A programmer without arrays is like a chef without a knife.