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
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!
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.
I would love to hear your feedback!