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

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
viviavi
Prole
Posts: 1
Joined: Sat Oct 28, 2023 2:43 am

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

Post 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?
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 54 guests