Welcome Guest Search | Active Topics | Log In | Register

Hello, couple of noob-ish questions
#1 Posted : Friday, January 6, 2017 6:04:58 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/6/2017(UTC)
Posts: 1

Thanks: 2 times
Was thanked: 0 time(s) in 0 post(s)
New user here, embedding squirrel into an windows IDE-style app to add all kinds of extensibility and automation. Previously using a Lisp, which everyone said would be easy to get used to after a while (IT WAS LIES!)

It's been very easy to get up and running, and get a semi-working REPL already. (So, thank you Alberto!)

Firstly, my REPL is sending each input command to sq_compilebuffer, then sq_call. The raiseerror arg is true, but my errorfunc never gets hit. No problems with the printfunc. Anything obvious I might have missed?

Secondly, I want to make very frequent calls to some simple user-defined script functions for counting events etc. Basic code is something like this:

sq_pushstring(v,"MyScriptFn",-1); // look up user function MyScriptFn(i, j)
sq_get(v, -2);

sq_pushinteger(v, i); // arguments
sq_pushinteger(v, j); // arguments
sq_call(v, 3, SQTrue, SQTrue);

int ires;
if (!SQ_SUCCEEDED(sq_getinteger(v, -1, &ires)))
// blah

sq_pop(v,3); // pop the roottable, function, result

Good news is this is pretty damn fast already, but I would like to streamline it as much as possible. Maybe there's a way to store a pointer to MyScriptFn or even just the string "MyScriptFn" so I don't have to look these up every time?

Thanks all!

#2 Posted : Tuesday, January 10, 2017 2:44:15 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 128
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 2 times
Was thanked: 10 time(s) in 10 post(s)
Compiler errors and runtime errors differ and have their own error hooks. Sorry, I don't have the time to go into detail right now, also you should provide more info how exactly you hooked your error function(s).

To speed up your calls, you could do (typed by heart, no guarantees!):

static HSQOBJECT MyFunc ;

void init(HSQUIRRELVM v)
int top=sq_gettop(v)
sq_pushstring(v,"MyScriptFn",-1); // look up user function MyScriptFn(i, j)
sq_get(v, -2);
sq_getstackobj(v,-1,&MyFunc) ;
sq_addref(v,&MyFunc) ; //REMEMBER TO: sq_release in deinitialization (BEFORE closing the VM)
sq_settop(v,top) ;

then do the calls:
sq_pushobject(v,&MyFunc) ;
sq_pushroottable() ; //THIS object
sq_puhsinteger(v,i) ; //parameter
sq_puhsinteger(v,j) ; //parameter
sq_call(v,3,SQTrue,SQTrue) ;
//get result here
sq_pop(v,3) ;

Should speed up things quite a bit. YOu can't significantly optimize the call itself (beginning with pushing MyFunc)
Also note, however, that this code lacks all error handling.
1 user thanked absence for this useful post.
Wonko on 1/12/2017(UTC)
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.106 seconds.