For Loop & Number of Enemys

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
NoAim91
Prole
Posts: 38
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

For Loop & Number of Enemys

Post by NoAim91 » Fri Mar 10, 2017 11:37 am

Hi, i try to make a simple Tower Defense Game. A main feature is that you can run multiple waves at the same time. If a wave is running it should spawn 1 enemy each 2 seconds. If 2 waves are running it should spawn 2 enemys each 2 seconds.

But my for loop only spawn 1 enemy from the first launched wave and if the first wave is finished it starts spawning from the second wave.
spawnWaiting1 = 0,1
spawnWaiting2 = 0,1
spawnWaiting3 = 2
spawnWaiting4 = 2


function spawnController(dt)
spawnWaiting1 = spawnWaiting1 - dt
spawnWaiting3 = spawnWaiting3 - dt

if spawnWaiting3 < 0 then

for i=1, #wave do

if wave > 0 and spawnWaiting1 < 0 then
wave = wave - 1
spawnEnemy(dt)
spawnWaiting1 = spawnWaiting2
end
spawnWaiting3 = spawnWaiting4
end

end

end



And I have a second question. If i have around 1.000+ enemys spawned (they just move from left to right) is begins to stutter :-( how I can stop this?! I mean my enemys are just tiny balls and in other games there are far more than 10.000 things moving at the same time without stutter.

NoAim91
Prole
Posts: 38
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

Re: For Loop & Number of Enemys

Post by NoAim91 » Fri Mar 10, 2017 3:23 pm

Ok i have found a solution for my first problem. I use a second for loop to control the spawn.

But why begins my game to stutter if i have over 1.000 enemys spawned? I draw my enemys with love.graphics.circle ... is this the problem? Would it better to load a image?

Lucyy
Prole
Posts: 42
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: For Loop & Number of Enemys

Post by Lucyy » Fri Mar 10, 2017 6:01 pm

I suggest you do the following:

Create a variable to keep track of time

Code: Select all

-- Time since the last enemy was spawned
local fTimeSinceLastSpawn = 0
In your update loop, you'll want to check if you want to spawn more enemies. If so, you'll want to add the delta time to the variable we just created.
To do this you'll need to know how many enemies you want to spawn and the amount of enemies you've spawned already

Code: Select all

-- Maximum amount of enemies
intMaxEnemies = 1000

-- Current amount of enemies
intActiveEnemies = 0
In your love.update, do the following:

Code: Select all

if intActiveEnemies < intMaxEnemies then
	fTimeSinceLastSpawn = fTimeSinceLastSpawn + dt
end
This will add the delta time to fTimeSinceLastSpawn as long as you've spawned less enemies than the maximum amount.

After that, you'll want to check if fTimeSinceLastSpawn is bigger than 2 seconds in your case. You can do this with a simple if statement:

Code: Select all

if fTimeSinceLastSpawn >= 2 then
In this if statement you want to spawn an enemy for every active wave, to do this you'll need to know how many waves are active. For my example I'll use the variable intActiveWaves.

Code: Select all

-- Amount of active waves
intActiveWaves = 1
You'll want to increment / decrement this variable when a new wave begins or when a wave ends.
Now you'll want to make a for loop to loop from 1 to intActiveWaves and spawn an enemy inside of that for loop:

Code: Select all

for i=1, intActiveWaves do
	SpawnEnemy()
end
Just before you close your if statement: be sure to reset fTimeSinceLastSpawn to 0!

Code: Select all

fTimeSinceLastSpawn = 0
In the end your code would look something along the lines of this:

Code: Select all

-- Amount of active waves
intActiveWaves = 1

-- Maximum amount of enemies
intMaxEnemies = 1000

-- Current amount of enemies
intActiveEnemies = 0

-- Time since the last enemy was spawned
local fTimeSinceLastSpawn = 0

function love.update(dt)
	
	-- Check if the enemy limit has been reached,
	-- if not: add deltatime to TimeSinceLastSpawn
	if intActiveEnemies < intMaxEnemies then
		fTimeSinceLastSpawn = fTimeSinceLastSpawn + dt
	end
	
	-- If TimeSinceLastSpawn is bigger than 2 (at least 2 seconds have passed)
	if fTimeSinceLastSpawn >= 2 then
	
		-- Spawn an enemy for every active wave
		for i=1, intActiveWaves do
			SpawnEnemy()
		end
		
		-- Reset the time since last spawn to 0
		fTimeSinceLastSpawn = 0
	end
	
end
I hope this help you with your game, if you need any more help just let me know!

NoAim91
Prole
Posts: 38
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

Re: For Loop & Number of Enemys

Post by NoAim91 » Fri Mar 10, 2017 6:11 pm

thank you lucyy

I have a new problem (a Tower kills a enemy and 2 enemys disapear ... this happens only when more than 1 wave are running ... i know where the problem is and I think I can find a solution myself).

I think I will post my game next week.... maybe later if more bugs/problems appear. xD

Lucyy
Prole
Posts: 42
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: For Loop & Number of Enemys

Post by Lucyy » Fri Mar 10, 2017 6:16 pm

It's hard to say without seeing the problem for myself, but I'll try to help xD

Assuming the 2 enemies that disappear at the same time were spawned at the same time, and moving at the same speed, this would mean that both the enemies are in the exact same spot (or at least almost in the exact same spot). And when your bullet collides, it collides with both of the enemies instead of just one.
If you end up needing help finding the solution just let me know! :3 If you like I can give you my Discord or Steam so I can help you when you need it.

I'll be looking forward to seeing your game go up next week! I like tower defense games :3

Good luck! \o/

NoAim91
Prole
Posts: 38
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

Re: For Loop & Number of Enemys

Post by NoAim91 » Fri Mar 10, 2017 8:06 pm

The 2 enemies are not at the same position. One of them is out of range of all towers. I assuming that they have the same ID but ... this is a mess xD .. but i will solve it ..

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 39 guests