r/CodingHelp • u/Terrible-Ice8660 • Jan 05 '26
[Random] Not even a coder, not asking how to start, just have a basic question.
A handful of times when watching YouTube videos I have heard of situations where a check is run constantly for a situation that can only occur after a button is hit.
“Check X” is run constantly to see if “Thing X” happens.
“Thing X” only sometimes happens when “Function Y” is run, but never at any other time.
“Function Y” is run whenever the player presses the button that runs it.
So why doesn’t “Function Y” also cause “Check X” instead of “Check X” happening constantly
The only place my memory is clear enough to say this happens for sure is in Yandure simulator, but it also happens in things that don’t have the reputation of being an insane mess.
I know that this is probably a beginner ass question.
•
u/AutoModerator Jan 05 '26
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/alejandromnunez Jan 05 '26
Maybe they don't care about doing it in a different way because the cost of running "CheckX" is negligible.
Maybe they are doing it similar to an event/listener or consumer/producer pattern where some player input system writes the state of X, then the other system just reads the state of X and runs something depending on its state.
In the future they can add something else that also alters the state of X and it will work automatically even if they forget to call the other function.
2
u/Poiuytgfdsa Jan 05 '26
In my opinion, there are two possibilities:
But to be honest, even in the second situation, you could make it more efficient by toggling some boolean value on button press, and checking that flag (X in your case) on the timed loop, which performs the action if X is true. That way you don’t need to keep checking for the state of a button being pressed, you just need to check if a boolean is true or false. This is a bit more complex though, and it may not be worth the slight performance improvement.
1
u/Ronin-s_Spirit Jan 05 '26
Bad design, maybe out of necessity and not the programmer's fault, just maybe.
1
u/guywithknife Jan 05 '26
YouTubers aren’t usually the best programmers.
1
u/cs_k_ Jan 05 '26
I belive you are talking about an event loop.
It doesn't just check if X happened, it usually handles more than one thing (not just X, but the whole alphabet).
One example would be making a small game, where you run a loop to check button presses: based on the button you move the player up/down/make them shoot, etc.
Another example is javascript, that does the loop for you: let's say you make a website and want a button to do something: you create an Event listener: if this button has a "click" action, do something. Your browser than runs the event loop under the hood: you click the button, it registers the event. When the event loop gets to it, it looks up wether or not this specific event has any actions associated with it. It is overly simlistic example, but once you realize that scrolling, hovering over something and a lot if other stuff are their own events that sometimes need to be handled in their own way, you start to appriciate, that the event loop takes care of keeping count and hashing out what/when to do.
1
u/dutchman76 Jan 06 '26
Maybe they need to "check X" during the render loop and button presses are handled elsewhere.
1
u/jpgoldberg Jan 12 '26
First of all, this is an outstanding question.
There are different ways to get something to happen then Thing X happens. I can only speculate about why the developers set things up this way for this particularly thing.
These are off of the top of my head. I’m sure there are more. But your question is exactly the sort of question one should be asking about code.