Forum

> > CS2D > Scripts > Threads in cs2d
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Threads in cs2d

2 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Threads in cs2d

Livia
User Off Offline

Zitieren
I've always used cs2d built in timer to perform some actions serially from simple tasks to balancing out heavy algorithms. I missed the good old "sleep" command from many other programming languages so I decided to implement my own sleep command in cs2d lua. It makes things so much easier and adds a level of syntastic sugar.

This is what I came up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
addhook("say", "say_hook")

function say_hook(id, message)
    if message == "suicide" then

        Thread(function()
            msg2(id, "You will die in 3 seconds!")

            msg2(id, "3..")
            sleep(1000)

            msg2(id, "2..")
            sleep(1000)

            msg2(id, "1..")
            sleep(1000)

            parse("killplayer "..id)
        end)

        return 1 
    end
end

What happened here? I created a Thread that is actually instantly running lua coroutine. Every time you call sleep(time) inside a coroutine it will yield the current coroutine and resume it after the specified time using cs2d timer.

Because it is so simple you don't have (yet) any control over the threads. You can't stop/resume/suspend them outside the thread itself. Ofcourse this can be easily added but I'm feeling kind of lazy today. If anyone is willing to take this any further, please do share your modifications!

Here's my implementation of threads >

alt Re: Threads in cs2d

tontonEd
User Off Offline

Zitieren
coroutine != thread, but if even lua.com use "thread" as determination, let it go..let it go.. can't hold it back anymore
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht