I'm working on a small project for the iPad and I simply want to run a script that will halt after certain function calls and then let me resume the script from the same place later on. In fact I only do 'threads' one at a time in a queue so it's really only multitasking between the iPhone OS and Lua.
static int yield__ (lua_State *L) {
//NSLog(@"Do Yield");
return lua_yield(L, 0);
}
//This function adds a script to my queue
- (void) doFileThreaded:(NSString*)filename {
NSString* path = [[NSBundle mainBundle] pathForResource:filename ofType:nil];
const char* file = [path UTF8String];
lua_State* newThread = lua_newthread(luaState);
//Load the file for execution
if ( luaL_loadfile(newThread,file) != 0 ) {
return;
}
//Add file to the queue
[threads addObject:[NSValue valueWithPointer:newThread]];
}
//This Function Executes the queued threads one at a time removing them when they've finished
- (void) doThreads {
lua_State* thread = NULL;
while ( threads.count > 0 ) {
thread = (lua_State*)[[threads objectAtIndex:0] pointerValue];
//Resume will run a thread until it yeilds execution
switch ( lua_resume(thread, 0) ) {
case LUA_YIELD:
//NSLog(@"Recieved Yield");
return;
case 0:
NSLog(@"Removing Thread");
[threads removeObjectAtIndex:0];
thread = NULL;
break;
default:
NSLog(@"Error Executing Threaded Script!");
[threads removeObjectAtIndex:0];
break;
}
}
}
Now For the Lua code:
function wait (seconds)
time = os.time() + seconds;
print("Waiting " .. os.time() .. " to " .. time);
while ( time > os.time() ) do
yield(); --I have written my own yield function
end
end
print("Entered Toad Behavior");
yield();
print("Point 1");
yield();
print("point 3");
wait(1);
print("point 4");
wait(2);
print("point 5");
This code will crash on the second call to wait in lua. With a BAD_MEMORY_ACCESS or lua_resume sometimes returns a runtime error. (I don't know how to check what the error is, so if you could help me with that I'd appreciate that too) Can anyone out there tell me what I'm doing wrong here?