local function StartGame()
-- this can have any amount of code that yields including loops
end
local function End()
-- when end is called I want it to stop running the game code
end
If the StartGame function has connections in it, that can be stopped easily by just putting them in a maid and then destroying.
But what about any arbitrary code that could be running? I could make a boolean flag, but then I would have to check the flag after every time the code yields (which is does often because I have a lot of task.wait()'s and loop timers)
local IsRunning = false
local function StartGame()
IsRunning = true
-- code
task.wait(3)
if not IsRunning then return end -- need to check because "End" could have been called while this code was yielding
-- more code
while IsRunning do -- need to check every single iteration in case End was called
-- code
task.wait()
end
end
local function End()
IsRunning = false
end
As you can see this gets tedious really quickly, but more importantly it doesn’t ensure that all the code really stopped. If I forget to put a check I can have rogue function continue to run. Then what happens if I call StartGame again and both instances of the function are changing a global variable screwing up the game and making it really difficult to track down the bug.
Essentially I want to know how I can write my code where I can effectively have a control panel that can start and stop any game mechanic at will.
Well, you could use Events from a module like sleitnicks Signal, which would allow you to connect to and End event, which would then eventually be allowed to be disconnected. Now, whether or not this will instantly halt execution of Start is unbeknownst to me. You could also use multiple events to provide feedback between the functions, one to call for a stop and the other as a response, similar to a promise or just a return.
I want to be able to stop any piece of code on a dime without having to fill the entire thing with if running then statements.
For example I have a lot yielding code because I want time to elapse before something happens, in the beginning I can do if running then, but then everytime I have a task.wait() I would have to do it again. I would also have to put it in every for loop and in every function that yields my code might call.
There has to be a concrete way to stop a programs because millions of programs can just stop with the press of a button.
Maybe I am doing something wrong, maybe I am not supposed to yield code but then how am I supposed to have timers and things that need to wait.
do you mean that you want to make a the function start not stop the upon lines, right? You’ll need to study about spawn and coroutines, these functions from lua are able to create a new thread.
local function StartGame()
spawn(function()
IsRunning = true
-- code
task.wait(3)
if not IsRunning then return end -- need to check because "End" could have been called while this code was yielding
-- more code
while IsRunning do -- need to check every single iteration in case End was called
-- code
task.wait()
end
end)
end
Maybe you don’t understand how functions works, when you execute a function it’ll read all the lines, when you make while, the function won’t end until while loop ends,
local isrunning = false
local function start()
print("Starting")
isrunning = true
while isrunning do
print("Executing while loop") -- Debbuging
task.wait()
end
end
start() -- Executing function
print("Start function finished") -- You need to know that it won't print, because the function is not finished yet, because while loop is infinity executing, so we'll need to create a new thread, using spawn().
You are not telling me information relevant to my question. Please reread my question before replying.
@wf_sh do you know how? I want to be able to end any program with an event. Like if I press the “x” on the browser, chrome shuts down. What if while I was pressing X another thread was doing
task.wait(2)
-- display some stuff that should only be displayed when chrome is open.
Maybe my code isn’t supposed to yield or something but what if I need to wait.
There has to be a methodical way to write a program so that you can end it easily with a single event. Millions of applications have a close button.
Wrap the entire script in a separate script connection, that relies on a shared variable to stay connected. (Like a value in workspace) And when the value disables, the script connection breaks and the entire thing stops.
could you give anymore context at all? if you truly want just an x-button for some code you do know that enabling the Disabled property to scripts instantly stops them right?