Forum

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

Englisch Threaded hooks in cs2d

1 Antwort
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Threaded hooks in cs2d

Livia
User Off Offline

Zitieren
Hello everyone,

I love to create abstract stuff with lua and came up with some weird hook-thread combination. It uses coroutines, metatables and some hidden magic. It allows you to use sleep almost anywhere in your code. I can't think of any practical applications yet but it's rather cool anyway.

This is a continuation to my last attempt in lua threading.

Here we go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- create a threaded say hook
threadhook('say', function(id, message)
    if message == 'suicide' then
        yield(1) -- don't display message in chat
    
        msg2(id, 'You are about to suicide in 3 seconds!')
        sleep(1000)

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

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

        parse('killplayer ' .. id)
    end 
end)

A threadhook allows you to yield the return value beforehand (line 4). It can also pause execution with sleep (lines 7, 10, 13).

Why would you want to wrap all the hooks inside threads? Because cs2d lua is all about running code which react to different hooks. If you cover all the hooks inside threadhooks, you can freely use sleep and yield anywhere in your code!

In this example we're using sleep inside another function, and it works fine >

You could even use yield in another function and the first one called will be returned to the hook. This allows you to parse chat commands anywhere and not having to worry about return values and complex program flow! Once you cover all your hooks inside threadhooks things get very interesting... eg. you could create a menu function that waits for the user entry and returns the button pressed as a value, no need to manually handle menu hooks.

Okay if you're not sold on the threadhooks, there's also a manual way to create threads. >


I would love to hear your feedback!

thread.lua >

alt Re: Threaded hooks in cs2d

VADemon
User Off Offline

Zitieren
I'd not use it for menus, because we have 2 forks of UniMenu already (I can't comprehend how people are still going after the malady of manually scripting menus...).

However it's great for some situations that require this kind of pause/execution:
• delayed and time quant based execution
• wait-for-reply commands (such as confirmation dialogues)
• anything else that requires sequential action

But I'm not sure about the last point. Would I dynamically create a new thread/function for each advanced command or is there another way?
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht