11/10/2022 0 Comments 3ds max 8 scripts stopped working![]() ![]() It gets the character and if it doesn’t exist, will wait until it’s added. Witnessing this level of elegance is dangerous to our feeble human brains. And I know, I know, “what if the character already exists”? Feast your eyes on this beauty: local character = player.Character or player.CharacterAdded:Wait() This will yield the thread until the player’s character is added. You can yield on an event by calling :Wait() (no relation). Roblox has events for everything you could possibly be waiting for (and if it doesn’t make a feature request!). You mean like this? repeat wait() until LocalPlayer.Character “That’s for my own code, but what if I want to wait for some Roblox code to run.” Plus, this is milliseconds/ seconds saved. In that case, you can do its admittedly uglier cousin: local event, called = Instance.new("BindableEvent"), falseĪgain, this will be ran instantly. “But someAsyncThing can be called immediately in my case!” We instantly get the message, and continue our code.“Tell me when you’re ready for me to run”.The callback to someAsyncThing isn’t ran away, and it’s not documented that it can.Let’s analyze this code again with the last scenario. This is the exact same number of lines of code as the repeat wait() until block. If we assume someAsyncThing will never call before the last line (Roblox Lua is single threaded so unless that’s a defined behavior, it won’t)… local event = Instance.new("BindableEvent") - You could use coroutines, but they're a bit finnicky Our code unnecessarily waited despite it being completely ready to run! The alternative is to simply hook your code up to an event. flag is set to true! Can our code run yet?.“Is flag true? No? Alright, give me a bit.”.The callback to someAsyncThing isn’t ran right away.However, you’re still unnecessarily waiting even after flag is set to true. “Ah, but wait!”, you cry, “I can do this!” local flag = false Despite flag being true, we’re still waiting for no reason. someAsyncThing calls the callback immediately with no yielding.Polling in our case is bad because we’re unnecessarily waiting for something to happen instead of just doing code once it happens. This is known as polling (colloquially, you’ll also hear it called busy waiting in circles for higher level software, although they’re not technically the same). The repeat wait() until is being used to stop code execution until flag is true. Let’s take this piece of code: local flag = false In my opinion, wait() has no place in production code. It’s a super easy solution and it works! This starts a bad habit of just putting wait() to fix your code. We probably learned about wait() at first as the solution to “why are my infinite loops crashing me?”. Not wait(n) (although if you are using small n values to where it’d be barely distinguishable from wait(), you’re probably going to hit the same issues mentioned in this thread). This is simply a better formatted and more fleshed out version. This is an adaptation of a Twitter thread I made a while ago. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |