GameMonkey Script

GameMonkey Script Forums
It is currently Tue Mar 19, 2019 6:51 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Lazy bindings snippet
PostPosted: Sun Sep 20, 2009 12:37 pm 
Offline

Joined: Tue Apr 28, 2009 12:32 pm
Posts: 4
I decided to share this with you.
It enables you to call 'simple' functions in your gamemonkey script from C/C++.
This might be interesting for lazy coders :)

Code:
#include <stdargs.h>
#include <string>

/*
 * callFunction
 *
 * calls a function in the current gamemonkey script.
 * Using an format string because I'm lazy..dont wanna write too much scriptbinds
 *
 * EXAMPLE CALL : script::callFunction( "doBlah", "%i%s%d", myInt, myString, myDouble );
 */
int callFunction( gmMachine *machine, string functionname, char *str, ... )
{
  va_list ap;
  int resultInt = 0;
  int d;
  char c, *s;
  float f;
  gmCall call;
  if(call.BeginGlobalFunction( machine, functionname.c_str() ))
  {
    printf("calling gm's '%s'()", functionname.c_str());

    /* do some funky formatstring stuff! */
    va_list ap;
    int d;
    char *s;
    float f;
    va_start(ap, str);
      while (*str)
      switch (*str++) {
        case 's':              /* string */
            s = va_arg(ap, char *);
            call.AddParamString( s );
            break;
        case 'i':              /* int */
        case 'd':              /* int */
            d = va_arg(ap, int);
            call.AddParamInt( d );
            break;
        case 'f':              /* int */
            f = va_arg(ap, float);
            call.AddParamFloat( f );
            break;
      }
    va_end(ap);

    printf("gm > %s() "), functionname.c_str() );
    call.End();
    call.GetReturnedInt(resultInt);
    if( !resultInt ){
      printf("gm > *** error somewhere in function '%s'", functionname.c_str() );
      error();
    }
  }else printf("gm > (?!?) calling unknown gm function '%s'()", functionname.c_str());
  return resultInt;
}


I stripped this from my c++ class, so no compile guarantees :)
Anyways, if anyone has some additions/ideas to have more verbose output rather then just "error somewhere in function"....make some noise!


Top
 Profile  
Reply with quote  
PostPosted: Sun Sep 20, 2009 1:44 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
Thanks for sharing this.
One thought would be to make a version which has a set of overloads with 1,2,3,4,N params. I surveyed game code once and found most script funcition in our, and other peoples game code used less than 4 params, so you would not need many overloads. Now these overloaded functions don't take var_args, instead use C++ objects as parameters which auto construct from basic types int, float, string. Once you do this, the code can be a little safer and you can perform type checking easily.
Code might look like:
Code:
class BoxedType
{
public:
  enum
  {
    TYPE_INT,
    TYPE_FLOAT,
    TYPE_STRING,
  };

  BoxedType(int a_int)  {m_value.m_int = a_int; m_type = TYPE_INT;}
  BoxedType(float a_float) {m_value.m_float = a_float; m_type = TYPE_FLOAT;}
  BoxedType(const char* a_string) {m_value.m_string = a_string; m_type = TYPE_STRING;}

  int m_type;
  union // Store in union or just separate members
  {
    int m_int;
    float m_float;
    const char* m_string;
  } m_value;
};

int callFunction( gmMachine *machine, string functionname);
int callFunction( gmMachine *machine, string functionname, BoxedType a_param1);
int callFunction( gmMachine *machine, string functionname, BoxedType a_param1, BoxedType a_param2);
int callFunction( gmMachine *machine, string functionname, BoxedType a_param1, BoxedType a_param2, BoxedType a_param3);

To simplify implementation you can just use wrapper overloads that pack multiple params into an array and handle a variable size array of params in a single function.
You could also save the return value as gmVariable and provide interface to retrieve the actual return value if desired by the caller.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group