Log In  

is it possible to create a await function?

for example..

if await(60,"my identifer") then
 --done
end

here is my code

function await(t,id)
  if not cofun[id] then
    --don't know how to return true here
    add(cofun,cocreate(function()
      while t>0 do
        t-=1
    yield()
      end
    end))
  else
   return false
  end   
end

function coupdate()
 for c in all(cofun) do
  if costatus(c) then
    coresume(c)
  else
    del(cofun,c)
  end
 end
end

EDIT: idk if this is possible but maybe something like this..

--id is determined by function execution index
if await(60) then
 --done
end

for example..

await(60)--await id 0
await(60)--await id 1
await(60)--await id 2
--nextframe
await(60)--await id 0
await(60)--await id 1
await(60)--await id 2
P#73368 2020-02-23 01:32 ( Edited 2020-02-23 01:35)

yield cannot return value - so you need to provide a function to your await that will be triggered when done.
Something like:

function wait_async(t,fn)
    return cocreate(function()
     -- wait...
        for i=1,t do
         yield()
        end
        -- trigger user code
        fn()
    end)
end

c=wait_async(60,function()
 print("done")
 end)

function _update()
  coresume(c)
end
P#73373 2020-02-23 07:31

huh odd I guess I could do this with a timer instead of coroutines.

thankyou

P#73393 2020-02-23 22:06

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-04-09 23:40 | 0.010s | 2097k | Q:14