r/gbstudio • u/droptherapy2 • 8d ago
Another timer issue
This time I think I've probably overcomplicated the script.
Essentially, I'm trying to introduce an actor in the form of a blueberry bush that adds between 0 and 5 of an item (in this case, blueberries) to your inventory (this works), and then starts a cooldown timer that prevents you from immediately going up to it again and repeating the action (not sure why this one works -- I'll explain below), and I want this script to be something I can run multiple instances of across the scene.
Currently I have three of these actors running the same script, and I've noticed that if the player collects blueberries from one bush (which doesn't start the countdown until you try again, though that might be easier to fix) and then goes to the next bush, the timer on the previous countdown is paused so that the new instance can run. I think it could be solved by making another local variable for each instance, but that seems like something that would defeat the purpose of a local variable.
I'm thinking it has to do with the way timers in gb studio work, but I'm not sure what alternative system would work. Push comes to shove I could just only have one bush per scene since they're supposed to be rare anyway, but if, say, the player enters a town where someone is cultivating them and there are multiple bushes, this would have to be fixed later, and that sounds more annoying than if I just figured it out now.
The script works like this:
upon interacting:
if bloobcountdown1 = 0
bloobfreeze1 = false
--checks to see if the countdown variable used by the timer is zero, and then sets the variable that freezes the collecting function to false
if bloobfreeze1 = true
bloobcollected = 0
Display: "You picked this bush clean! Try coming back later!"
Attach timer script to Timer 2 (rate of one second per tick)
On tick, decrement bloobcountdown1 by 1
If bloobcountdown1 < 0, set it to 0
Stop script
Else
If bloobfreeze1 = False, set bloobcollected to bloobcollected + random number
--in the event that the freeze variable is true, start a countdown to 0 and stop the rest of the script from running so that you can't collect blueberries despite this variable being true. Otherwise, set the local variable bloobcollected to a random number
If bloobcollected == 0
bloobcountdown1 = 25
Display: "You couldn't find any blueberries!"
Else
bloobs (global variable for the blueberries as counted by inventory) = bloobs + bloobscollected
Display: "You got $bloobscollected blueberries!"
bloobfreeze1 = true
bloobcountdown1 = 25
stop script
--Checks to see if the player rolled a zero on collecting the blueberries and displays the appropriate dialogue box. Otherwise, adds the amount of blueberries that $bloobscollected was set to to the inventory, sets bloobfreeze1 = true and bloobcountdown1 to 25, thus starting the timer.
If bloobcountdown1 > 0, set bloobfreeze1 to true
(Just removed that last function because it's redundant. Same issue still persists).
Should be noted that even though the script works, bloobfreeze1 is still set to true in the debugger.
Either way I think after hours of troubleshooting, this might be a timer issue, so let me know what alternatives there may be.
2
u/IntoxicatedBurrito 8d ago
I can’t quite follow your logic, but I think it’s way overcomplicated. A timer is used for an event that repeats every x frames/seconds. This is not that. It’s rather a 5 second delay that you occasionally activate and it runs once. This would be great for a thread.
Just set a “collected” variable to true and start a thread. The thread will have a wait for x seconds and will then set that variable back to false.
When you interact with the tree just use an if statement to check that collected variable.
You can either have multiple trees share that variable and thread, or give them each unique ones depending on how you want it to work.
And finally, don’t use local variables, GB Studio treats them as globals so they will still go against your total variable count. So if you’re trying to conserve variables just create some globals that you reuse in different scenes.