Admittedly there are some other posts about this topic, but none of them are fully resolved. I found a solution but want to know is there a better, more practical way. If so, please inform me, if not maybe this can help someone else.
Problem: I have a Tower Defense game. One of my towers (which i call defender2 here) shoots 3 bullets, waits a for a little bit, and then shoots three bullets again. The goal is for each tower to shoots its 3 bullets based on its own timer of when it was created. I tried the solution here, but every tower in the game shot the bullets at the same time no matter when they were created. I wanted them to all be different.
Solution: I added an object variable to my tower (called “instanceTimer” here), and increment that using a scene timer. I then base the shooting off of that object variable rather than a scene or object timer. That object variable is different for each instance (since its initiated at 0 every time one is created), and this allows for the desired output.
But for some reason this solution sems … inelegant to me. Is there a better way to get a timer of an object where each instance has a different value?
There are literal Object timers in the engine. It seems like you’re basically manually recreating that logic using a scene timer and object variables instead.
You should use an object timer (along with a “For Each” event) instead of a scene timer.
By using an object timer, you wouldn’t even need to know row/column of an object. Just “For Each defender2” as an event, then the condition of “Timer firerate of Defender2 is greater than yoursecondsvalue here” with your actions to fire, and in the same event reset the object timer. Object timers are unique per instance of an object.
but right now your events are using Scene timers, which are not unique per object (as they’re for the whole scene), and are completely different conditions/actions than object timers.
By using Object Timer events, they will only be created on the instances selected by the conditions (and why you would use a “For each” event type, rather than a normal event with the “Select all objectname” condition)
Ah, its not the regular event list. I see it now. Okay, I thought my primary issue was the objet timer but I think it was because I was originally using “all objects” instead of “for each object”. Thanks! Will try it out and report back.
@Silver-Streak I didn’t know whether to make a new post or to add on to this one
There’s something weird going on in my game, that I can’t figure out. (Maybe i just don’t understand timers in GDevelop). Maybe you, or another contributor in the forum, can help me out?
Notice in the video, how the third instance only shoots one bullet. And the four instance only shoots two bullets. They all should shoot three bullets at a time from the above code, correct? What am I doing wrong?
Also, the more instances I create, the weirder it gets. Any ideas thoughts?
I played around with the bullet cooldown (currently set at 0.01), the game’s FPS (currently a minimum of 60 and maximum of 120), and even trying my old original code. Nothing seems to get all instances of my “defender2” object to do the desired behavior.
Figured out it was the “Trigger Once” condition that was causing the issue. Completely removing it “fixed” the problem but caused the undesired output of a million bullets being fired during that 1 second time interval. This, plus changing the bullet cool down period to 0.5 seconds, is what I came up with that works.
I was messing with a Mario type game earlier. Making those yellow blocks that lose platform behavior for so many seconds after collision. I had set up a timer on the block for 10 seconds. The problem was that every block would gain its behavior back at the same time. I’m still a noob so I assumed all the same objects would share an object timer. So I got frustrated and started cleaning up the mess to try a different approach. But after removing a couple starts and stops, it works perfectly. Each block has its own 10 seconds on individual time frames. It was really simple too. 2 things.
I have player collision with block while block is on animation 0. From there it will change the animation to 1, deactivate it’s behavior, and start an object timer.
Then I have “timer is greater than 10 seconds.” will activate its platform behavior again and set the animation back to 0.
That’s all. Nothing anywhere else. If you’d seen what all junk I had going on you’d shake your head. Lol.
I know this isn’t the same thing but it is about object timers and maybe this will help.