Skip to main content
gamedev.net
🔒 Locked

Squirrel Threads

Started by wilrader 2 replies 1.6k views
Original Post
wilrader
wilrader
Hoping someone knows a little about this. Looking through the Squirrel API, I noticed a section on threads. Kind of reminds me of the GM yield(); functionality. However, I'm not sure. Can it be used to do the same thing? Example: You start a loop in C/C++ and then "execute" the squirrel VM. It then runs through the first script/thread until a suspend(); is called, then moves on to the next script/thread. Once that is done and all the scripts/threads have been executed and then suspended, it returns to the C/C++ loop and then starts over again. I can't really find anything on this and would quite appreciate some insight in to this. Thanks, Wil
evolutional
evolutional
Looking at the page here, I'd say they sound like GameMonkey's threads in that they're co-operative and can be blocked/signalled to control execution but looking over the example there's a few major differences:

a) GM's threads don't require explicit execution, they start automatically - squirrel threads need to be invoked with "call"
b) GM's blocks are explicit, so the squirrel example that blocks on "suspend 1" would require a signal of "suspend 1" to be sent. It appears that Squirrel's threads will wakeup on any signal
c) The Squirrel function thread.getstatus() isn't present in GM, but could easily be added
d) It's not obvious how to yield() a Squirrel thread - maybe with suspend?
e) Can you pass "this" to a Squirrel thread and have an object implicitly available in the thread function?

Squirrel looks interesting - it's on my list of languages to play with.




wilrader
wilrader
I did a few simple tests and it seems as if it would be quite easy to set it up to execute one script right after the other. It seems as if SQPlus simply allows you to execute one script at a time. So all you would need to do is set up an STL list and a quick class that would handle the scripts. You would need to add a script to the list, then an "execute" function would simply loop through all the scripts until the last one has been called and suspended.

I think! I'm pretty sure I've yet to find a set of Squirrel functions that do this for me.

I'm having a silly little problem though:

try
{
SquirrelVM::RunScript(testSquirrel); // run the script
}
catch(SquirrelError & e)
{
sq_getprintfunc(SquirrelVM::GetVMPtr())(SquirrelVM::GetVMPtr(),_T("Error: %s, %s\n"),e.desc);
}

The _T throws an error about it not having been declared and for the life of me I can't figure out what I need to do to get it to declare.

Wil
wilrader
wilrader
Future reference: If you get that error, you solve by doing:
#include <tchar.h>

Anyways, back on topic:

This is basically what I've done here:

class SqCoOp {
public:

bool addScript(const SQChar *file, int limitexe) {
coop newcoop;
newcoop.filename = file;
newcoop.script = SquirrelVM::CompileScript(file);
if (newcoop.script == NULL) return false;
newcoop.totalexe = 0;
newcoop.exelimit = limitexe;
scripts.push_back(newcoop);
return true;
}


bool delScript(const SQChar *file) {
list<coop>::iterator i = scripts.begin();
while (i != scripts.end()) {
if (i->filename == file) {
scripts.erase(i);
return true;
}
i++;
}
return false;
}


int execute() {
list<coop>::iterator i = scripts.begin();
int executed = 0;
while (i != scripts.end() && !scripts.empty()) {

SquirrelVM::RunScript(i->script);

i->totalexe++;
if (i->totalexe >= i->exelimit) {
i = scripts.erase(i);
} else {
i++;
}
executed++;
}

return executed;
}

private:
struct coop {
const SQChar *filename;
SquirrelObject script;
int totalexe;
int exelimit;
};

list<coop> scripts;
};

It is kind of broken though. Given that, once you suspend a thread you must also wake it back up. Not totally sure how to do that outside of the actual script though. Also, it needs to detect a return value/end of script somehow too and then erase it from the list. Don't suppose anyone here knows how to do those two things, or perhaps knows if I'm going in completely the wrong direction?

Wil

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.