Author Topic: [C++] I need some help regarding C++ -> SQ modules  (Read 2781 times)

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
[C++] I need some help regarding C++ -> SQ modules
« on: July 13, 2012, 10:35:25 pm »
I've been trying and thinking on how to make a module that messages "hello world" ingame to all players, but I obviously fail at it, here is my C file ( as the headers and other requested files are included )

NOTE: 1. I've been seeking help on IRC but it seems I am too annoying for this comunity with my obvious annoying PMs.
          2. I know this is not a place to request C++ Help.

Code: [Select]
#include "SQFuncs.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdio.h>
#include <sqstdaux.h>
#include <sqstdio.h>


        #ifdef SQUNICODE
        #define scvprintf vwprintf
        #else
        #define scvprintf vprintf
        #endif


extern SQAPI sq;

//_SQUIRRELDEF( MessagePeople )
//{
/*SQUIRREL_API SQRESULT sq_compile(HSQUIRRELVM v,SQLEXREADFUNC read,SQUserPointer p,const SQChar *sourcename,SQBool raiseerror);*/
   // char phat [] = "hello world";
//sq->pushstring( v, _SC(" Message(phat) "), -1 );
//sq->call( v, _SC("Message(phat) "), true, true );
//sq->call( v, _SC("Message(phat); "), true, true );
// return SQ_OK;
//}

_SQUIRRELDEF( HelloSquirrel )
{
sq->pushstring( v, _SC( "Hello Squirrel Module!" ), -1 );


return 1; /* return 0 indicates the Squirrel function does not return a value, 1 returns a value */
}

_SQUIRRELDEF( Sum )
{
SQInteger iArgCount = sq->gettop( v ); /* Get the argument count */

if ( iArgCount == 3 ) /* Argument count = 2 arguments + root table = 3 */
{
SQInteger i1 = 0, i2 = 0;

sq->getinteger( v, 2, &i1 ); /* Get argument 1 in slot 2 to 'i1' */
sq->getinteger( v, 3, &i2 ); /* Get argument 2 in slot 3 to 'i2' */

sq->pushinteger( v, i1 + i2 ); /* Return the sum of these two numbers */
return 1;
}

sq->pushbool( v, SQFalse );
return 1;
}

        void printfunc(HSQUIRRELVM v, const SQChar *s, ...)
        {
                va_list arglist;
                va_start(arglist, s);
                scvprintf(s, arglist);
                va_end(arglist);
        }


        void call_Message(HSQUIRRELVM v, const SQChar *s)
        {
                int top = sq->gettop(v); //saves the stack size before the call
                sq->pushroottable(v); //pushes the global table
                sq->pushstring(v,_SC("Message"),-1);
                if(SQ_SUCCEEDED(sq->get(v,-2))) { //gets the field 'foo' from the global table
                        sq->pushroottable(v); //push the 'this' (in this case is the global table)
                        sq->pushstring(v,s,-1);
                        sq->call(v,2,0,0); //calls the function
                }
                sq->settop(v,top); //restores the original stack size
        }


        int main(int argc, char* argv[])
        {
                HSQUIRRELVM v;
                v = sq->open(1024); // creates a VM with initial stack size 1024


        //        sq->seterrorhandlers(v);


                sq->setprintfunc(v, printfunc, NULL); //sets the print function


                sq->pushroottable(v); //push the root table(were the globals of the script will be stored)
 //               if(SQ_SUCCEEDED(sq->dofile(v, _SC("SRPGv0.1.nut"), 0, 1))) // also prints syntax errors if any
//                {
                        call_Message(v,_SC("teststring"));
 //               }


                sq->pop(v,1); //pops the root table
                sq->close(v);


                return 0;
        }


SQInteger RegisterSquirrelFunc( HSQUIRRELVM v, SQFUNCTION f, const SQChar* fname, unsigned char ucParams, const SQChar* szParams )
{
char szNewParams[ 32 ];

sq->pushroottable( v );
sq->pushstring( v, fname, -1 );
sq->newclosure( v, f, 0 ); /* create a new function */

if ( ucParams > 0 )
{
ucParams++; /* This is to compensate for the root table */

sprintf( szNewParams, "t%s", szParams );

sq->setparamscheck( v, ucParams, szNewParams ); /* Add a param type check */
}

sq->setnativeclosurename( v, -1, fname );
sq->newslot( v, -3, SQFalse );
sq->pop( v, 1 ); /* pops the root table */

return 0;
}

void RegisterFuncs( HSQUIRRELVM v )
{
/* Add your Squirrel functions here */

RegisterSquirrelFunc( v, HelloSquirrel, "Hello", 0, 0 );
RegisterSquirrelFunc( v, Sum, "Sum", 2, "ii" );
//RegisterSquirrelFunc( v, MessagePeople, "MessagePeople", NULL, NULL );
}


StixsmasterHD

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: [C++] I need some help regarding C++ -> SQ modules
« Reply #1 on: July 14, 2012, 04:24:35 pm »
Why make it so complicated?

Why use C++ to have it work with SQ? That seems like too many extra steps.

Jest make a simple script snippet thatll do this for you and other users as they join and add it to your main.nut file.

It much easier & less work.
« Last Edit: July 14, 2012, 04:29:19 pm by StixsmasterHD »

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
Re: [C++] I need some help regarding C++ -> SQ modules
« Reply #2 on: July 14, 2012, 06:48:38 pm »
I want to learn from it, but it seems no one knows C++ in this comunity ( or does not want to answer ) ::)

Kevin

  • Newbie
  • *
  • Posts: 32
  • Karma: +9/-35
    • View Profile
    • www.edgarcain.zz.mu
Re: [C++] I need some help regarding C++ -> SQ modules
« Reply #3 on: January 19, 2013, 09:41:55 pm »
hola yo si se de C++ y te puedo ayudar en esos problemas
pero tu me ayudas en el script que eso no se
Kevin  ;)

SugarD

  • Argonath RPG Dev/Manager
  • Tester
  • Sr. Member
  • ****
  • Posts: 820
  • Karma: +37/-75
  • STOP IN THE NAME OF THE COLESLAW!
    • View Profile
    • Clan Xperience
Re: [C++] I need some help regarding C++ -> SQ modules
« Reply #4 on: January 19, 2013, 10:19:57 pm »
I want to learn from it, but it seems no one knows C++ in this comunity ( or does not want to answer ) ::)
There are people here that know C++. LU itself is coded in it. ;)

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
Re: [C++] I need some help regarding C++ -> SQ modules
« Reply #5 on: January 20, 2013, 12:38:54 am »
I want to learn from it, but it seems no one knows C++ in this comunity ( or does not want to answer ) ::)
There are people here that know C++. LU itself is coded in it. ;)

Why bump a topic from "July 13, 2012, 07:35:25" ? I already solved it, like, 5 mins after my last post ?

SugarD

  • Argonath RPG Dev/Manager
  • Tester
  • Sr. Member
  • ****
  • Posts: 820
  • Karma: +37/-75
  • STOP IN THE NAME OF THE COLESLAW!
    • View Profile
    • Clan Xperience
Re: [C++] I need some help regarding C++ -> SQ modules
« Reply #6 on: January 20, 2013, 12:41:10 am »
I want to learn from it, but it seems no one knows C++ in this comunity ( or does not want to answer ) ::)
There are people here that know C++. LU itself is coded in it. ;)

Why bump a topic from "July 13, 2012, 07:35:25" ? I already solved it, like, 5 mins after my last post ?
Look at the date of the post before me.

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
Re: [C++] I need some help regarding C++ -> SQ modules
« Reply #7 on: January 20, 2013, 12:31:00 pm »
WHY BUMP IT? I didn't post before you, get your facts right and stop making non-logical excuses.

Thijn

  • Tester
  • Sr. Member
  • ****
  • Posts: 531
  • Karma: +27/-16
    • View Profile
Re: [C++] I need some help regarding C++ -> SQ modules
« Reply #8 on: January 20, 2013, 02:32:34 pm »
WHY BUMP IT? I didn't post before you, get your facts right and stop making non-logical excuses.
Look at the post above SugarD, lol...

Sure, He might've quoted your post. But he wasn't the one who started bumping..

SugarD

  • Argonath RPG Dev/Manager
  • Tester
  • Sr. Member
  • ****
  • Posts: 820
  • Karma: +37/-75
  • STOP IN THE NAME OF THE COLESLAW!
    • View Profile
    • Clan Xperience
Re: [C++] I need some help regarding C++ -> SQ modules
« Reply #9 on: January 20, 2013, 08:22:28 pm »
WHY BUMP IT? I didn't post before you, get your facts right and stop making non-logical excuses.
If you keep up the negative attitude, you will be forum banned. I have warned you several times already to stop picking fights with people.

 

© Liberty Unleashed Team.