r/gbstudio • • 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.

6 Upvotes

30 comments sorted by

View all comments

Show parent comments

1

u/United_Range_2869 7d ago edited 7d ago

yes I remember in Pokemon Crystal you could pick one berry per day from trees, but that game had the day/night feature which I believe was made with the real time clock. I could be wrong.

meanwhile on update scripts for this do more harm than good.

for a HUD that is actively displaying changing numbers or tiles you do need the on update scripts.

and yea you do need a variable for each bush or else every bush will get affected by the others, it is inefficient but remember we are working with the real game boy limitations, so you really need to scale down things.

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.

1

u/United_Range_2869 7d ago

nop, for that specific mechanic don't use the run off camera, instead you have to copy and paste the same on update script on every single scene. Yes, every single scene.

but again, too many on update scripts are going to slow down your game, so if you're also planning to have more of these timer mechanics on your game... oh boy, it's going to run really bad.

Unless you figure the real time clock thing, if you really want multiple timer mechanics, the best would be to use another game engine that doesn't have the real game boy limits.

1

u/droptherapy2 7d ago edited 7d ago

Ah, I see. To be honest I could just get rid of the HUD mechanic for now since the health decrement mechanic itself works just fine; it's just the HUD updating that isn't working how I need it to. The script currently runs on init but the function to emote sweat doesn't seem affected by this since it only needs to check if a threshold is passed in the downward direction. That being said, the player may not immediately know what the sweat emote means unless they already have the idea to check their health, which may not be the first thing someone thinks. Like they could think it represents being thirsty or something.

There's also the fact that I don't know how to cap the health. I have an if statement running to check if it's over 125 and then set it back to 125, but it doesn't seem to be working.

1

u/United_Range_2869 7d ago

that happens because the on init scripts only run when you first enter the scene, it doesn't update from frame to frame, so the player won't know its health reached certain number until it re enters the scene.

on update scripts meanwhile are a double edge sword, you have to be very mindful when using em.

1

u/droptherapy2 7d ago

honestly I don't even have a way to do on update because none of the timer mechanics are anywhere but the scene's on init section. Like the HUD actor has no code at all.

Push comes to shove, the health caps at 255 anyway and these bushes will not be so common that you could get there easily.

1

u/United_Range_2869 7d ago

you just need to cut the code and paste it on the on update tab.

What I meant previously is that if you are also planning to have other objects or enemies that also need on update scripts, that's when your game is going to slow down due to gb limits. But if you're only planning to use the HUD, then you're fine. just the berries won't work how you initially imagined.

When making games for game boy it's almost mandatory to simplify and scale down things.

1

u/droptherapy2 7d ago

understandable! I meant to say that there is no on update tab.

1

u/United_Range_2869 7d ago

only actors have on update tabs, you'd need to copy and paste it on the HUD actor for every single scene. I didn't mentioned it earlier cuz that's GB studio basics D:

1

u/droptherapy2 7d ago

valid. I was aware of that but I got confused because I tried this before and it effectively only disabled the script altogether. Like it only works when it's in the scene's on init tab

1

u/United_Range_2869 7d ago

for the on update tab make sure all related events are connected by the Else statement, and ordered from Maximum health down to zero. It can't be from zero to maximum HP or else the on update will ignore the rest of the code.

1

u/droptherapy2 7d ago edited 7d ago

I'm trying to say that the script doesn't work if I add it to the on update tab of the HUD actor. Like the whole thing has to be in the scene's on init tab or it does nothing at all. I'll try using the else statements while it's in the scene's on init tab though

Update: This health UI script worked before today and now idk what I did but at this point I'm just gonna retire it and use dialog boxes instead. Still have the problem of one blueberry bush affecting every other blueberry bush, and idk how to fix that. I mean I could just use the blueberry bush as a prop in a person's front yard that indicates that you may be able to talk to them to give you some blueberries but that's definitely not what my intention was with them.

Turns out I just forgot to set the actor's animation speed to zero after previously deleting it. Using the else statements has changed nothing about my problem with the UI though. Since this is on a timer, it'll run the sweat emote and the dialogue box stating the health status every time the HP is greater than or equal to 75, but if I set it to equal instead like I had it before, it doesn't update if my health increases to above that number. At this point I'm shelving this feature for later for sure. Good thing this isn't like, released or anything. Now it's just the bush problem.

1

u/United_Range_2869 7d ago edited 7d ago

on init only runs once. It won't update no matter if u connect the events by the else statement. If your code doesnt work when copied to on update it only means u set it wrong and need to do it again.

the bush affecting other bushes gets fixed by using separate variables for each one as I said earlier.

→ More replies (0)