Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Functions taking time out of the script?

Asked by 8 years ago

During a script, if you have something that you needed to loop in a separate loop to another loop which is currently active, I'd have thought a function would be a good idea, however with normal functions, after being called they continue to take the "scripts time" rather than executing the function simultaneously with the code around it.

For example, as the code below suggests, it has a look, followed by a function with a loop, which the script will execute this function and THEN move onto the next loop. Ideally i'd like the function to continue while the script plays out.

Other than making the function another script, (meaning I want to keep it in the same script without global or anything) is there any other way to go about this?

function WastingTime()
    local number=0
    for i=1,10 do wait(1)
        number=number+1
        print("Waiting" .. number)
    end
end


wait(1)
for i=1,10 do wait(1)
    print("Wait")
end
WastingTime()
for i=1,10 do wait(1)
    print("Wait2")
end

-Thank you

2 answers

Log in to vote
0
Answered by 8 years ago

For running multiple lines of code you will need to look a Threading in short the code block will continue to run both functions.

Other links that may help:- Coroutine_manipulation

Thread_scheduler

0
Interesting, these are very useful. I'm gonna have a quick read up. Hexcalibur 10 — 8y
Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Please see this question:

https://scriptinghelpers.org/questions/11710/2-functions-at-a-time

Highlights

Yes, you can do this, but most of the time it will not be the best solution

As a simple example, two Script objects will run at the same time. Each Script object is given its own "thread", or, as Lua calls it, coroutine (routine being a piece of a program and co indicating that multiple are working together as in coworker)

A coroutine is similar to a thread (in the sense of multithreading): a line of execution, with its own stack, its own local variables, and its own instruction pointer; but sharing global variables and mostly anything else with other coroutines.

See Also: https://scriptinghelpers.org/questions/11836/can-one-script-run-many-items for more complicated comparison of with coroutine and without

Answer this question