Hello world
I'm starting with Lua and have some troubles with the way corountines work. I "just" want to make pauses in my mud (for instance, when you connect, it says "hello", and then "welcome" after a second without freezing the world for everyone else)
As a starting point, i'm using the wait I found wandering around in this forum, http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=8433. Here it is :
And I tried to use it, without any success, I have to admit.
For instance
or
== no error of execution, except that ... it doesn't make any pause in the first case and just stop after #flash1# in the second case :D
I assume that I need to read a little bit about coroutines (I'm doing so) but 'speaking' with people is a powerful way to learn and understand, so I hope people here would help me :)
If I understand well what I've read, coroutine is a sort of multi-threading process (with no preemption) right ? So it basically allows to do what I want.
So, what I've to do with the code is make sure that the 'update' Lua function is regularly called by the C code, so my 'connection' Lua function will make steps, until it encounters a wait.wpause that will freeze it (with coroutine.yield () ) right ? (using my second coding case)
I have this : call_lua(ch, "wait_update", NULL);
in my update.c, during the char_update
And my function 'connections' is called when the player connect : call_lua (ch, "connections", "new_in");
The last argument "new_in" is because I intended to have a different message, depending if it is a new player or someone already known by the database.
So my real function is more like this :
I know at leastthree two things :
1/ my way of checking the arg is wrong, I know that. I'm modifying it, i found how to do what I wanted recently in a tuto
2/I don't get how the whole thing know which thread has to be release.I figure it our but still : Is 'assert (coroutine.running (), "Must be in coroutine")' returning some kind of unique ID ?
3/ you don't have to mention that I'm trying to do something well ahead of my actual level of Lua, I know that :) But the best way to learn is to do, and I have nothing else to do now than making this bloody pause
Regards,
Aiseant (french, so please forgive my strange way of writing english )
I'm starting with Lua and have some troubles with the way corountines work. I "just" want to make pauses in my mud (for instance, when you connect, it says "hello", and then "welcome" after a second without freezing the world for everyone else)
As a starting point, i'm using the wait I found wandering around in this forum, http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=8433. Here it is :
module (..., package.seeall)
local threads = {}
function update ()
-- for each active thread, see if the time is up
for k, v in pairs (threads) do
if os.time () >= v then
threads [k] = nil -- delete from table now
assert (coroutine.resume (k))
end
end
end
function wpause (seconds)
threads [assert (coroutine.running (), "Must be in coroutine")] = os.time () + (seconds or 1)
return coroutine.yield ()
end
function make (f)
assert (type (f) == "function", "wait.make requires a function")
coroutine.wrap (f) () -- make coroutine, resume it
end -- make
And I tried to use it, without any success, I have to admit.
For instance
function connections (arg)
mud.send_to_char ("#flash1#")
wait.make( function ()wait.wpause(1)end)
mud.send_to_char ("#flash2#")
wait.make( function ()wait.wpause(1)end)
return
end --connection
or
function connections (arg)
wait.make( function ()
mud.send_to_char ("#flash1#")
wait.wpause(1)
mud.send_to_char ("#flash2#")
wait.wpause(1)
end)
return
end --connection
== no error of execution, except that ... it doesn't make any pause in the first case and just stop after #flash1# in the second case :D
I assume that I need to read a little bit about coroutines (I'm doing so) but 'speaking' with people is a powerful way to learn and understand, so I hope people here would help me :)
If I understand well what I've read, coroutine is a sort of multi-threading process (with no preemption) right ? So it basically allows to do what I want.
So, what I've to do with the code is make sure that the 'update' Lua function is regularly called by the C code, so my 'connection' Lua function will make steps, until it encounters a wait.wpause that will freeze it (with coroutine.yield () ) right ? (using my second coding case)
I have this : call_lua(ch, "wait_update", NULL);
in my update.c, during the char_update
And my function 'connections' is called when the player connect : call_lua (ch, "connections", "new_in");
The last argument "new_in" is because I intended to have a different message, depending if it is a new player or someone already known by the database.
So my real function is more like this :
function connections (arg)
--blabla, other if with different argument, like "new_in" (but the text is very long
if arg == "out" then
wait.make( function ()
mud.send_to_char ("Good bye runner")
wait.wpause(1)
mud.send_to_char ("You'll be back soon")
end)
return
end -- if listing
end -- function
I know at least
1/ my way of checking the arg is wrong, I know that. I'm modifying it, i found how to do what I wanted recently in a tuto
2/
3/ you don't have to mention that I'm trying to do something well ahead of my actual level of Lua, I know that :) But the best way to learn is to do, and I have nothing else to do now than making this bloody pause
Regards,
Aiseant (french, so please forgive my strange way of writing english )