Please Help Me!

Peace Unto You! I’m making a game in ‘Defold’ which involves defending the player, and I need help making code for my enemies to endlessly spawn and then progressively spawn faster the longer the game is running. I’ve tried over 30 variations of the code I’ll copy, but for what ever reason the moment I add anything to do with spawning (proxies, factories, or even just scripts) the enemy doesn’t appear when I run the game, but when I remove anything to do with spawning from my game, EVERYTHING runs perfectly.

PLEASE help me cause once I figure this out I can get my game to a Pre-Alpha state.

“How can I make enemies endlessly spawn progressively spawn faster the longer the game is running?”

This should be the title of your forum post. A forum post with the title “Please Help Me!” is less likely to get any attention from other forum users than a title describing your problem.

What have you tried? Please share one variant of code that wasn’t working. It is easier to help you that way.

Have you seen the side-scroller tutorial?

In this tutorial there are stars that are randomly spawning at a certain interval. It is the perfect starting point to solve your problem. Instead of having a fixed spawn interval you gradually decrease it over time.

Peace Unto You! Thank you for assisting me

I’ll definitely keep all the wisdom you gave in mind.

Here’s one script for the controller that for what ever reason didn’t work

-- game_controller.script

local spawn_interval = 2 -- Initial spawn interval (in seconds)
local min_spawn_interval = 0.5 -- Minimum spawn interval (in seconds)
local spawn_timer = 0 -- Timer to track the time for spawning
local time_passed = 0 -- Time since last spawn interval update
local spawn_area = vmath.vector3(0, 0, 0) -- The area where enemies will spawn

function init(self)
	-- Initialize variables and state
	self.spawn_timer = 0
	self.time_passed = 0
	self.spawn_interval = spawn_interval
	msg.post(".", "acquire_input_focus") -- Acquire input focus if needed
end

function update(self, dt)
	-- Update spawn timer
	self.spawn_timer = self.spawn_timer + dt
	self.time_passed = self.time_passed + dt

	-- Check if it's time to spawn a new enemy
	if self.spawn_timer >= self.spawn_interval then
		self.spawn_timer = 0
		spawn_enemy(self)
	end

	-- Gradually decrease the spawn interval
	if self.time_passed > 10 then -- Adjust this threshold as needed
		self.spawn_interval = math.max(min_spawn_interval, self.spawn_interval * 0.95) -- Adjust the rate of acceleration
		self.time_passed = 0
	end
end

function spawn_enemy(self)
	-- Determine the position where the enemy will spawn
	local position = vmath.vector3(math.random(-300, 300), 400, 0)

	-- Instantiate the enemy game object at the specified position
	local enemy_id = go.get_id("main/level/enemy")
	go.set_position(position, enemy_id)
end

function on_message(self, message_id, message, sender)
	-- Handle messages if needed
end

function final(self)
	-- Release input focus if needed
	msg.post(".", "release_input_focus")
end

The code looks ok as far as I can tell, except that you are not spawning enemies. You have a spawn_enemy() function but it is not doing anything:

Did you make use of ChatGPT or similar when you wrote the code?

After around the 20th time I was like let me use ChatGPT to see if it can find errors. I’m so grateful you pointed that out cause I see that too. So what typically would go there?

I figured as much. The quality of answers is not great. You should stop using it and really learn things yourself.

Well, see this as a learning experience. I linked an example project above which spawns game objects. You should look at it.

We also have really good documentation here: defold.com/learn

Also have a look at the Examples section. There are example of spawning game objects.

2 Likes

Okay thank you! Yeah im grateful I went through all this hassle cause the 7+ hours I was at it helped me innerstand Defold more as a program. I’ll keep everything you said in mind thank you so much for taking so time to guide me loll. This is my 5 day making code

2 Likes