How to increase spawn rate either by time or score?

#1
local frequency = .5
local min_y = 100
local max_y = 680

function init(self)
	self.timer = 1/frequency
	-- make the game deterministic
	math.randomseed(0)
end

function update(self, dt)
	self.timer = self.timer - dt
	if self.timer <= 0 then
		self.timer = 1/frequency
		local p = go.get_position()
		p.y = vmath.lerp(math.random(), min_y, max_y)
		local component = "#jellyfish_factory"
		factory.create(component, p)
	end
end
1 Like

#2

You need to change the frequency variable depending on time or score.
You can set score with a message.

msg.post("/spawner", "update_score", { score = 10 })
local min_y = 100
local max_y = 680
local base_frequency = 0.5

function init(self)
	self.frequency = base_frequency
	self.timer = 1/self.frequency
	-- make the game deterministic
	math.randomseed(0)
end

function update(self, dt)
	self.timer = self.timer - dt
	if self.timer <= 0 then
		self.timer = 1/self.frequency
		local p = go.get_position()
		p.y = vmath.lerp(math.random(), min_y, max_y)
		local component = '#jellyfish_factory'
		factory.create(component, p)
	end
end

function on_message(self, message_id, message, sender)
	if message_id == hash("update_score") then
		self.frequency = base_frequency * (1 + message.score / 10)
	end
end

Something like that.

1 Like

#3

The above is an good solution to this. An alternative is to have the factory itself check the score on update and the action if required. This removes the need for the message, and thus, some sort of event/subscription setup to generate the message when the score changes.

I like to think of this as a ‘Polling Pattern’.

So:

  1. Create a game object that is responsible for the score.
  2. Create a property on that object to hold the score. (See: https://www.defold.com/manuals/properties/)
  3. In your update method use go.Get("/ScoreGameObject", “Score”) to get the current score.
  4. Update internal attributes appropriately.

This approach isn’t inherently better than the message based one above, just has different advantages and disadvantages.
Advantages:

  1. Requires less code/has less moving parts.
  2. Is, as far as I understand Defold, synchronous.
  3. Can alter the score from the Editor UI. :slight_smile:

Disadvantages:

  1. Is, potentially, more costly. As the check happens every update rather then only when a actionable event happens. This may or may not matter.
  2. Is an invisible link. There is no way to know, from the game score object, what is hitting the score property. This means that if you change the name of the property, delete it, or remove the game object you won’t know what things need to by updated to work. Where the messaging approach is a little bit clearer in this respect (although still slightly opaque on the receivers end.). Again this may or may not matter to you.

Either approach will work.

2 Likes

#4

where do I put the message post?

0 Likes

#6

Since the game object, the spawner, in sergy.lerg’s example above does not hold a copy of the score (quite rightly) then the presumption would be that you have a game object somewhere that is storing it.

That object when it changes its score, for any reason, would send example msg.post above.

So to answer your actual question; the message post would appear in whatever game object is keeping track of your games score.

2 Likes

#7

I implemented the changes and the spawn rate does increase but it increases when I collect one item. it doenst matter what the score is whenever it increases more spawn. not at a specific interval.

0 Likes

#8

@millern9 please spend a little bit more time when asking for help. I don’t even see a question in your post, except for in the title. What exactly do you wish to do? Be as specific as possible. If you write a detailed post clearly outlining what you wish to do then you will get good answers back.

3 Likes