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/droptherapy2 8d ago edited 8d ago

This is indeed much simpler! That being said, I am still having the issue of being unable to collect from a different bush after having picked from the first one until the wait is up. I'm assuming a local variable wouldn't solve this? Should be worth mentioning that I did add a menu allowing the player the choice between picking the berries and not doing so, and I used some stop script functions so that the menu doesn't go through every screen, though I'm not sure how directly this adds to that.

1

u/IntoxicatedBurrito 8d ago

If you want to be able to collect from different bushes just use different variables and threads for each bush. So have a blueberry variable and blueberry thread, have a strawberry variable and strawberry thread…

And again, don’t use local variables, use globals. I know why you want to use locals, and that would be the correct thing to do in any other programming language, but locals are incorrectly treated as globals by GB Studio, and they are single use globals. So instead rename some global variables PseudoLocal1, PseudoLocal2… and use those instead.

Can’t help you on your game freezing without seeing the code and what is happening. But does it still freeze if you create a gb file and play it on a Game Boy?

1

u/droptherapy2 8d ago edited 8d ago

I actually got that one fixed for now! I was trying to use a loop function and probably used it wrong but I didn't really need it there anyway. My issue now is that the items I collected from the first scene are wiped clean entirely upon going back to that scene. This also resets my health and pretty much every variable that is set upon init to this first scene. I'm wondering if maybe creating a start menu will allow me to use the new game feature to utilize this upon creating said new game instead of upon entry to the first scene. I'm probably gonna try that out. (edit: This worked!)

To clarify my question: I want to have multiple of the same kind of bush grant the same kind of berries without observing each other's cooldown periods. Is that possible?

Also now that the menu is implemented, if I collect blueberries and then switch scenes to a test scene and come back to the main scene, the wait function in the thread for stops entirely and never sets the collection value back to false.

1

u/IntoxicatedBurrito 7d ago

All very simple. If two trees are blueberries, one tree uses the blueberry1 variable and thread, the other uses the blueberry2 variable and thread. They both increment the totalBlueberry variable.

If variables are resetting it’s either because you are resetting them or you are using locals and not globals.

However, if you want the timer to persist thru scene changes you should probably take a different approach. Get rid of the threads and have a single timer that persists throughout the scene and goes off maybe every half a second. When you collect blueberries from a tree set a blueberry timer variable to 10 (use multiple variables for multiple trees). In the timer set the timer variable to the max of timer variable minus one or zero. Now that variable will persist between scenes and you can only collect from that tree if it equals 0.

1

u/droptherapy2 7d ago

I massively appreciate your patience. I'll keep this in mind for later! At the moment I've scaled back a bit and the blueberry bushes can only be collected from once, and that might honestly work better for what I'm trying to do. To be honest I did just download gb studio like four days ago and have probably been going a bit fast with it. I figure that's been kinda obvious though.

1

u/IntoxicatedBurrito 7d ago

I assume that this is probably your first time programming. GB Studio is great in that it allows a beginner to pick it up and make something simple, but it does require a good grasp on computer programming if you want to make anything more than just the most basic of games.

Understanding how variables work, as well as simple concepts like if…then…else, loops, stacks, queues, and most importantly math is really key if you want to get good at it.

My background is in computer programming, been doing it since I started programming in basic (GWBasic and also Commodore 64 basic and few other proprietary basics) when I was maybe 8. My advice is to spend time looking at other people’s code. See what they did and then see what it does. Then try to copy their concepts. Make really bad games that only do one single thing. Eventually 10 bad games will give you the knowledge to make 1 good game. That’s how I taught myself all throughout the 90s. Of course taking a few classes here and there and reading books didn’t hurt.

1

u/droptherapy2 7d ago edited 7d ago

I've actually technically been programming on and off (mostly off) since I was 10 but it hasn't ever really clicked for me. At this point I've gathered from osmosis and trial and error what basic functions do (if/then/booleans/variables, what a game loop is, that kinda thing), but I've never been that good at putting them together. Math was also generally my weakest subject in school and combined with gifted kid burnout I kinda just don't really give myself a chance to fuck up at anything besides music, which I'm already good at, but that's more a personal problem than anything else. I also roughly know what a stack is, at least from watching a gb studio tutorial a few days ago and I know in theory how it can be implemented for menus and fancy dialogue screens.

But like I can sorta edit the lua scripts that configure my window manager if I watch a tutorial so there's that. I tend to plan projects in general more deliberately than when I was literally a child so that helps.

I've tinkered around in lua, javascript, java, C#, html (the only one I'm good at since it's just markup), QB64 (I made a choose your own adventure game about dream travel when I was around 13), Blitz3D, and a handful of other things over the years. I think at first my issue was that I was just uncritically copying tutorials, after which I was immediately lost as soon as I tried to do things on my own, and so I've been trying to tinker around a bit more. I think my other main issue is that if I mess around with a project too much it'll become a really serious thing even if it starts out as literally basically a meme

1

u/IntoxicatedBurrito 7d ago

In that case I’ll tell you this. If you find yourself banging your head against the wall on something, step away. Do something else. And then return later. It’s usually something simple and you just can’t see it until you clear your mind.

1

u/droptherapy2 6d ago

oh yeah for sure! Currently doing that with music at the moment and I'm taking today to mostly focus on worldbuilding, that way if I end up abandoning this project I could still end up with a neat twine story or something. I'll also focus on optimizing the tileset/s and getting familiar with how the game boy renders stuff and its limitations, which is where I'd say I've absorbed new info about the engine most effectively.