lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thursday 02 December 2004 11:32 am, Dan Laufer wrote:
> Whoops.
>
> It was actually all down to me getting in a tangle over :'s and .'s.

only that?  i was thinking that maybe the problem is with the unrolling of the 
':' . in other words:

 Task1Co = coroutine.create(Task1:tick());

becomes

 Task1Co = coroutine.create(Task1.tick(Task1));

but coroutine.create expects a function, not the result of tick(Task1)

one way would be to add a new method 'GetTask'

 function TestTask:GetTask ()
  self.task = self.task or coroutine.wrap (self.tick)
  return self.task
 end

then, you could use like this:

 Task1 = TestTask:new();
 Task1.taskName = "Task1";

 Task1Co = Task1:GetTask ();
 
 -- somewhere in the loop
 Task1Co (Task1Co);

 -- or maybe,
 Task1:GetTask() (Task1Co)

there are better ways to do it... but 'tick' would be better as the name of an 
interface function, not the work function.  for example:

 function TestTask:work()
         while 1 do
                 self.internalVal = self.internalVal + 1;
                 print("Internal val of TestTask "..self.taskName..
    " : "..self.internalVal);
                 coroutine.yield();      
         end
 end

 funtcion TestTask:tick ()
  self.co = self.co or croutine.create (TestTask.work)
  self.co (self)
 end

 -- using it:
 Task1 = TestTask:new();
 Task1.taskName = "Task1";

 -- somewhere in the loop
 Task1:tick()


-- 
Javier

Attachment: pgpXiz5x1pZXl.pgp
Description: PGP signature