YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

sqPlus
THACO
#1 Posted : Tuesday, August 30, 2005 6:57:00 PM(UTC)
Rank: Member

Groups: Registered
Joined: 8/30/2005(UTC)
Posts: 6

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hi, I am new to scripting and well of course to Squirrel. I am trying to use the sqplus.lib. Are there any Docs to go along with this?
I have used the sqplus.lib examples of DXSquirrel and testSqPlus. I am trying to do simple things and work my way out. I have created my own solution to run a script and do some class stuff (not really sure about class stuff but it works like the testSqPlus example) Currently I am trying to figure out how to call a squirrel function from C++ using SquirrelVM. Basically I have done it via regular squirrel like it shows in the docs. I assume there is a way with sqplus that is simpler? using SquirrelVM::BeginCall? something to do with that? I have yet to figure out how it works any help or resource to look through. I have been trying to look through the SquirrelVM files also can't really understand the use of Squirrel Object since most SquirrelVM calls incorporate a Sq Obj.

-Matt

Update - this is what I have been trying


Below give me the
"This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Press any key to continue"

SquirrelObject a;

a.SetValue("function","foo"); // foo is the function i want to call
SquirrelVM::BeginCall(a);
SquirrelVM::PushParam(1);
SquirrelVM::PushParam(2.0f);
SquirrelVM::PushParam("three");
a = SquirrelVM::EndCall();


Below works (of course not really SquirrelVM)
HSQUIRRELVM v;
v = SquirrelVM::GetVMPtr();
sq_pushroottable(v);
sq_pushstring(v,"foo",-1);
sq_get(v,-2); //get the function from the root table
sq_pushroottable(v); //’this’ (function environment object)
sq_pushinteger(v,1);
sq_pushfloat(v,2.0);
sq_pushstring(v,"three",-1);
sq_call(v,4,SQFalse);
sq_pop(v,2); //pops the roottable and the function
fagiano
#2 Posted : Tuesday, August 30, 2005 7:46:52 PM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 825

Thanks: 0 times
Was thanked: 36 time(s) in 29 post(s)
<pre>SquirrelObject root = SquirrelVM::GetRootTable(); // gets the global scope</pre>
<pre>SquirrelObject func_foo = root.GetValue("foo"); //fetches the function from the root</pre>
<pre>SquirrelVM::BeginCall(func_foo);

SquirrelVM::PushParam(1);

SquirrelVM::PushParam(2.0f);

SquirrelVM::PushParam("three");

SquirrelObject retvalue = SquirrelVM::EndCall(); </pre>
If I have time I'll try to write down few lines of doc on the wiki(Jhon? :) )


I hope this helps


Alberto

THACO
#3 Posted : Tuesday, August 30, 2005 8:53:57 PM(UTC)
Rank: Member

Groups: Registered
Joined: 8/30/2005(UTC)
Posts: 6

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Thanks, that did it getting the root table and such.

I could probably create a simple tutorial with some answers to all the small problems I have encountered if you wanted, you would have to look over it just to double check things since I am well quite a newbie with this, but I since I am just trying and testing things out I could do it in some organized manner to help others.


-Matt
John Schultz
#4 Posted : Tuesday, August 30, 2005 10:55:19 PM(UTC)
Rank: Member

Groups: Registered
Joined: 6/24/2005(UTC)
Posts: 241

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hi Alberto, Matt,



From previous post:



Quote:


<span id="PostFlatView"><span id="PostFlatView">Generalized DXSquirrel (as sqplus.lib) and a simple example test case showing how to use a C++ class with Squirrel (create, operate, delete).

Core Squirrel code modified to allow print(object) to call _print() if defined for the object.

Exposed existing low-level stack dump code.

MSVC 7.1 solution/make included (added dependencies, changed to MT (multi-threaded) libs). After loading the solution, F5 should build everything and run.





More info: I moved the Win32-specific code to new files to allow sqPlus to be able to be used in non-Win32 environments. Both Release (sqPlus.lib) and Debug (sqPlusD.lib) are created so that switching between Release and Debug can be done cleanly.

I recently diff'd work-2 to work-4, and the only differences appear to be weakref-related code. I can merge the code together: however, I'd like to keep updated print() functionality I added- have you decided if you are going to add such behavior to the core code (perhaps implemented in a different way)? I hope that sqPlus (or something like it) will become part of the standard Squirrel distribution.



In the new project, both DXSquirrel and testSqPlus use sqplus.lib. My goal was to see how quickly and easily new projects could be created using sqPlus.lib.

Embedding the script engine, setting up class/variable/function support is relatively simple in Lua and LuaPlus. Dynamically creating a class and automatically deleting the class was not straightforward in Lua and LuaPlus (</span></span><span id="PostFlatView"><span id="PostFlatView"> These are common questions on game development forums</span></span><span id="PostFlatView"><span id="PostFlatView">). The testSqPlus example shows that it is relatively easy to interface C/C++ classes, dynamically allocate them, operate on them, and have them be automatically deleted using Squirrel.



I've been spending most of my time finishing up physics code, thus I have not had more time to investigate all of the API's for sqPlus.



Matt,



Writing down what you have learned while using sqPlus would be very helpful (you'll probably uncover issues that I'll need to figure out when I get back to scripting ;-) ). Improving/documenting sqPlus will help make using Squirrel easier. More (simple/short) code examples would be very useful (more useful than Doxygen'd source code; although Doxygen can be useful in tracing through code and maintaining API documentation).  Visual Assist X ( http://www.wholetomato.com/ ) is also helpful in tracing through source code (very powerful if using MSVC).

</span></span>
THACO
#5 Posted : Wednesday, August 31, 2005 8:49:06 AM(UTC)
Rank: Member

Groups: Registered
Joined: 8/30/2005(UTC)
Posts: 6

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Another question on sqPlus. I was trying to create a C function to be used in squirrel. I was able to reproduce the example showed in the documents the print_args function with just squirrel. With squirrel plus the function works

//Squirrel
sq_pushroottable(v);
sq_pushstring(v,"newfunc",-1);
sq_newclosure(v,print_args,0); //create a new function
sq_createslot(v,-3);
sq_pop(v,1); //pops the root table

//Squirrel Plus
SquirrelVM::CreateFunction(print_args);

The above CreateFunction() compiles and seems to run fine, but I cannot call a function in the script, I also do not know what the function would be called in the script, I tried just calling print_args but it says print_args does not exist. I'll be testing more things throughout the day (school hasn't started yet so I have plenty of time to mess around)

-Matt
John Schultz
#6 Posted : Wednesday, August 31, 2005 1:47:33 PM(UTC)
Rank: Member

Groups: Registered
Joined: 6/24/2005(UTC)
Posts: 241

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
[quote user="THACO"]Another question on sqPlus. I was trying to create a C function to be used in squirrel. I was able to reproduce the example showed in the documents the print_args function with just squirrel. With squirrel plus the function works //Squirrel sq_pushroottable(v); sq_pushstring(v,"newfunc",-1); sq_newclosure(v,print_args,0); //create a new function sq_createslot(v,-3); sq_pop(v,1); //pops the root table //Squirrel Plus SquirrelVM::CreateFunction(print_args); The above CreateFunction() compiles and seems to run fine, but I cannot call a function in the script, I also do not know what the function would be called in the script, I tried just calling print_args but it says print_args does not exist. I'll be testing more things throughout the day (school hasn't started yet so I have plenty of time to mess around) -Matt[/quote]



SquirrelVM::CreateFunction() calls sq_newclosure(). It needs a table and a name before being called, and a slot must be created in the table (as shown in the Vanilla Squirrel example). I searched through all the code and could not find SquirrelVM::CreateFunction() being used elsewhere. I'm curious how SquirrelVM::CreateFunction(), SquirrelVM::CreateString(), etc., are used?



The following is the result of a quick test:



int testFunc(HSQUIRRELVM v) {

  StackHandler sa(v);

  int paramCount = sa.GetParamCount();

  printf("testFunc: numParams[%d]\n",paramCount);

  for (int i=1; i <= paramCount; i++) {

    printf("param[%d]: ",i);

    switch(sa.GetType(i)) {

    case OT_TABLE:   printf("OT_TABLE[0x%x]\n",sa.GetObject(i)); break;

    case OT_INTEGER: printf("OT_INTEGER[%d]\n",sa.GetInt(i));    break;

    case OT_FLOAT:   printf("OT_FLOAT[%f]\n",sa.GetFloat(i));    break;

    case OT_STRING:  printf("OT_STRING[%s]\n",sa.GetString(i));  break;

    default:

      printf("TYPEID[%d]\n",sa.GetType(i));

    } // switch

  } // for

  return SQ_OK;

} // testFunc



// Creates a function in the table currently on the stack.

void CreateFunction(HSQUIRRELVM v,const SQChar * scriptFuncName,SQFUNCTION func,int numParams=0,const SQChar * typeMask=0) {

  sq_pushstring(v,scriptFuncName,-1);

  sq_newclosure(v,func,0);

  SQChar tm[64];

  if (typeMask) {

    if (_snprintf(tm,sizeof(tm),"t%s",typeMask) < 0) {

      sq_throwerror(v,_T("CreateFunction: typeMask string too long."));

    } // if

  } else {

    tm[0] = 't';

    tm[1] = 0;

  } // if

  sq_setparamscheck(v,numParams+1,tm); // Parameters are table+args (thus, the +1).

  sq_setnativeclosurename(v,-1,scriptFuncName);

  sq_createslot(v,-3); // Create slot in table (assigning function to slot at scriptNameFunc).

} // CreateFunction



// Create a Global function on the root table.

void CreateFunctionGlobal(HSQUIRRELVM v,const SQChar * scriptFuncName,SQFUNCTION func,int numParams=0,const SQChar * typeMask=0) {

  sq_pushroottable(v); // Push root table.

  CreateFunction(v,scriptFuncName,func,numParams,typeMask);

  sq_pop(v,1);         // Pop root table.

}// CreateFunctionGlobal



// In main():



    try {

      HSQUIRRELVM v = SquirrelVM::GetVMPtr();



      CreateFunctionGlobal(v,"testFunc0",testFunc);

      CreateFunctionGlobal(v,"testFuncN",testFunc,1,"n");

      CreateFunctionGlobal(v,"testFuncS",testFunc,1,"s");



      SquirrelObject main = SquirrelVM::CompileBuffer("testFunc0(); testFuncN(1); testFuncN(1.23); testFuncS(\"Hello\");");

      SquirrelVM::RunScript(main);

    } // try



    catch(SquirrelError &e) {

      char buff[256];

      sprintf(buff,"Error: %s, %s\n",e.desc,_SC("Squirrel::TestObj"));

//      OutputDebugString(buff);

      puts(buff);

    } // catch



John
John Schultz
#7 Posted : Wednesday, August 31, 2005 6:33:15 PM(UTC)
Rank: Member

Groups: Registered
Joined: 6/24/2005(UTC)
Posts: 241

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
CreateFunction* can be further simplified:



void CreateFunction(HSQUIRRELVM v,const SQChar * scriptFuncName,SQFUNCTION func,const SQChar * typeMask=0) {

  sq_pushstring(v,scriptFuncName,-1);

  sq_newclosure(v,func,0);

  SQChar tm[64];

  if (typeMask) {

    if (_snprintf(tm,sizeof(tm),"t%s",typeMask) &lt 0) {

      sq_throwerror(v,_T("CreateFunction: typeMask string too long."));

    } // if

  } else {

    tm[0] = 't';

    tm[1] = 0;

  } // if

  sq_setparamscheck(v,SQ_MATCHTYPEMASKSTRING,tm); // Determine arg count from type string.

  sq_setnativeclosurename(v,-1,scriptFuncName);

  sq_createslot(v,-3); // Create slot in table (assigning function to slot at scriptNameFunc).

} // CreateFunction



// Create a Global function on the root table.

void CreateFunctionGlobal(HSQUIRRELVM v,const SQChar * scriptFuncName,SQFUNCTION func,const SQChar * typeMask=0) {

  sq_pushroottable(v); // Push root table.

  CreateFunction(v,scriptFuncName,func,typeMask);

  sq_pop(v,1);         // Pop root table.

}// CreateFunctionGlobal



...



    try {

      HSQUIRRELVM v = SquirrelVM::GetVMPtr();



      CreateFunctionGlobal(v,"testFunc0",testFunc);

      CreateFunctionGlobal(v,"testFuncN",testFunc,"n");

      CreateFunctionGlobal(v,"testFuncS",testFunc,"s");

<span id="_ctl0_PostForm_Reply">     

      SquirrelObject main = SquirrelVM::CompileBuffer("testFunc0(); testFuncN(1); testFuncN(1.23); testFuncS(\"Hello\");");

      SquirrelVM::RunScript(main);

    } // try</span>

...





John
Users browsing this topic
Guest
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.362 seconds.