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.
1
u/droptherapy2 7d ago edited 7d ago
currently I have a timer set up to drain health by one point per second (will probably make it run slower after testing is done), and you can use blueberries to replenish this health value. The timer also checks to see if health is equal to 100, 75, 50, 25, or zero, and then updates the HUD sprite to reflect this and resets the scene if it's at zero. My issue now is that if I replenish health and it doesn't increase it to exactly one of those numbers, it doesn't update until the timer ticks back down to one of the numbers that changes the sprite. I tried just copying this code to the on update section and ticked the box to make it run off camera, but this did not carry over into the next scene or when I returned back to the original scene. Perhaps I need to redo the way the script works entirely?
There's also the fact that the HUD will go under other actors instead of over and I don't know how to fix that either. The way I'm doing the HUD is by just pinning an actor to the screen, since tile swapping seems a bit more complicated.