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
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.
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:
- Create a game object that is responsible for the score.
- Create a property on that object to hold the score. (See: https://www.defold.com/manuals/properties/)
- In your update method use go.Get("/ScoreGameObject", āScoreā) to get the current score.
- Update internal attributes appropriately.
This approach isnāt inherently better than the message based one above, just has different advantages and disadvantages.
Advantages:
- Requires less code/has less moving parts.
- Is, as far as I understand Defold, synchronous.
- Can alter the score from the Editor UI.
Disadvantages:
- Is, potentially, more costly. As the check happens every update rather then only when a actionable event happens. This may or may not matter.
- 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.
where do I put the message post?
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.
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.
@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.