Why do they keep going to the same position

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
Seth144k
Prole
Posts: 29
Joined: Wed Jan 19, 2022 8:45 pm

Why do they keep going to the same position

Post by Seth144k »

So right now I'm trying to work on an enemy spawner I want the y to be the same but the x needs to be random. It is random and they are being spawned... But at the same spot. I've tried changing the seed but it still doesn't work. can anyone please help here is my love file
Attachments
conf.love
(4.56 KiB) Downloaded 69 times
User avatar
darkfrei
Party member
Posts: 1186
Joined: Sat Feb 08, 2020 11:09 pm

Re: Why do they keep going to the same position

Post by darkfrei »

The lib src/enemy.lua must have such structure:

Code: Select all

local enemy = {} -- it is local!

function enemy:load()

end

function enemy:update(dt)

end

function enemy:draw()

end

return enemy -- return the local table
and the main.lua:

Code: Select all

local enemy = require("src/enemy")

You can call the math random as https://love2d.org/wiki/love.math.random
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Seth144k
Prole
Posts: 29
Joined: Wed Jan 19, 2022 8:45 pm

Re: Why do they keep going to the same position

Post by Seth144k »

i updated it with

Code: Select all

function enemy:load()
    enemy.spawnTime = 0.1
    enemy.x = love.math.random(0, 800)
    enemy.y = 50
    enemy.w = 20
    enemy.h = 20
    table.insert(enemies, enemy)
    table.insert(enemies, enemy)
end
and it does the same thing as before
Seth144k
Prole
Posts: 29
Joined: Wed Jan 19, 2022 8:45 pm

Re: Why do they keep going to the same position

Post by Seth144k »

oh wait actually it makes the enemies freak out and glitch all over the screen how can i make it so that they just stay where their random number picked
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Why do they keep going to the same position

Post by ReFreezed »

There are several problems in the code, one of which is there seems to be confusion about what the global 'enemy' variable represents. Is it supposed to be a kind of manager for multiple enemies, or is the (one and only) enemy. (Same with the global 'player' variable.) 'enemy' currently references the exact same table at all times. You also assign a new table to the global variable 'bullet', and another different new table to player.bullet (which is not referenced anywhere else in the code), in the player.shoot function. The global variable 'bullet' will thus always reference the last "spawned" bullet.

Another issue is that pairs() is used in loops when ipairs() should be used. The former doesn't care about the order of the items in the table (and is slower). Note that both ipairs() and pairs() may/will skip one element in the two places you call table.remove(player.bullets,i) inside the loops. Removals needs to happen something like this:

Code: Select all

for i = #player.bullets, 1, -1 do -- Loop backwards.
	if whatever then
		table.remove(player.bullets, i) -- Changes position of all bullets after 'i' (that we have already looped through as we loop backwards, so we don't care that their position changed).
	end
end
You should read up on local variables, modules, and classes.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Seth144k
Prole
Posts: 29
Joined: Wed Jan 19, 2022 8:45 pm

Re: Why do they keep going to the same position

Post by Seth144k »

thank you for helping me make it better but how can I do random generation? I just want it to spawn at a fixed y axis but always have the x value be random and i want it to spawn every 0.1 seconds. I got the actual generation working with a random value but no matter what happens the enemies get spawned at the same position when I want it to be diffrent
User avatar
darkfrei
Party member
Posts: 1186
Joined: Sat Feb 08, 2020 11:09 pm

Re: Why do they keep going to the same position

Post by darkfrei »

You are need:

Code: Select all

local function makeNewEnemy ()
	local enemy = {} -- new table, not your library
	enemy.spawnTime = 0.1
	enemy.x = love.math.random(0, 800)
	enemy.y = 50
	enemy.w = 20
	enemy.h = 20
	return enemy
end

function enemy:load()
	 -- self is your library enemy, it has methods self:load, self:update and self:draw; 
	self.enemies = {} -- also your library has now a new empty list of enemies
	local newEnemy = makeNewEnemy ()
	table.insert (self.enemies, newEnemy) -- adding new enemy to the list of enemies
end
So you can draw it as

Code: Select all

function enemy:draw()
	for i, enemy in ipairs (self.enemies) do -- iterate your list of enemies
		love.graphics.rectangle ('line', enemy.x, enemy.y, enemy.w, enemy.h)
	end
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Why do they keep going to the same position

Post by ReFreezed »

Here's one way to spawn enemies dynamically at a fixed interval:

Code: Select all

local enemies = {}

local function spawnNewEnemy()
	local enemy = {
		x = math.random(0, 800),
		y = 50,
		w = 20,
		h = 20,
	}

	function enemy:update(dt)
		self.y = self.y + 40 * dt
	end

	function enemy:draw()
		love.graphics.rectangle("line", self.x,self.y, self.w,self.h)
	end

	table.insert(enemies, enemy)
end

local enemySpawnTimer = 0

local function updateEnemySpawner(dt)
	enemySpawnTimer = enemySpawnTimer - dt

	if enemySpawnTimer < 0 then
		enemySpawnTimer = 0.1
		spawnNewEnemy()
	end
end

function love.update(dt)
	-- Update enemies and other stuff.
	updateEnemySpawner(dt)

	for _, enemy in ipairs(enemies) do
		enemy:update(dt)
	end

	-- Despawn off-screen enemies and other stuff.
	for i = #enemies, 1, -1 do
		local enemy = enemies[i]

		if enemy.y > love.graphics.getHeight() then
			table.remove(enemies, i)
		end
	end
end

function love.draw()
	for _, enemy in ipairs(enemies) do
		enemy:draw()
	end

	love.graphics.print("Enemies: " .. #enemies)
end
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests