GameMonkey Script

GameMonkey Script Forums
It is currently Tue Jun 18, 2019 2:40 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Apr 06, 2012 8:18 am 
Offline

Joined: Fri Jun 19, 2009 12:39 pm
Posts: 31
A bit of a feature request.

I was thinking about being able to add script functions to a user type with a minimum of hassle. Now, I know I could have a script function (or table of functions) and then have some sort of native side or even a script side function to bind it. But I'm lazy, and that's a bit too involving for my liking not to mention more error prone,.

What I would REALLY like, is some sort of auto-magic binding in the language-spec and in the VM. Something like the following;

Code:

MyUserType:doSomething = function(someArg, anotherArg)
{
  blah
  blah
  blah
};



So I guess when the script is loaded and run, the VM would be able to bind the script function to the type. And I guess this is the same for any type of variable.

Again, I know I could do this by having a native side table and overloading the required operators, so that I can assign these vars by myself, but I'd like to avoid that.

Thoughts? Criticism? Maybe there's a language feature that I've missed that does it already?


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 06, 2012 10:32 am 
Offline

Joined: Fri Jun 19, 2009 12:39 pm
Posts: 31
And just for reference, this is how I currently do it in my code with a native function to perform the binding. It works perfectly, I'd just rather not have to performing the binding manually, as it could be a bit error prone.

Native code:
Code:
s32 cScriptSystem::_gmBindFuncToType(gmThread* a_thread)
{
   GM_CHECK_STRING_PARAM(pTypeName, 0);
   GM_CHECK_STRING_PARAM(pFuncName, 1);
   GM_CHECK_FUNCTION_PARAM(pFunc, 2);

   ScriptTypeMap& typeMap = canvas::SCRIPTSYSTEM->mScriptTypeMap;

   ScriptTypeMap::iterator findIt = typeMap.find(cCalcCrc32(pTypeName));
   if (findIt == typeMap.end())
   {
      return GM_EXCEPTION;
   }

   cScriptType* pType = findIt->second;

   a_thread->GetMachine()->RegisterTypeVariable(pType->getTypeId(), pFuncName, a_thread->Param(2));

   return GM_OK;
}


Script test
Code:

global setToRed = function()
{
   canvas.gfx.setClearColour(1.0, 0.0, 0.0, 1.0);
};

global setToWhite = function()
{
   canvas.gfx.setClearColour(1.0, 1.0, 1.0, 1.0);
};

BIND_TYPE_FUNCTION("CanvasGfx", "setToRed", setToRed);
BIND_TYPE_FUNCTION("CanvasGfx", "setToWhite", setToWhite);


Top
 Profile  
Reply with quote  
PostPosted: Sat Apr 07, 2012 2:12 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
Thanks for the clarification, I see what you mean. Some kind of syntactical sugar would likely make that possible.
You could have implemented the registration entirely in script. eg.
Code:
register =
{
  CanvasGfx =
  {
    setToRed = function()
    {
       canvas.gfx.setClearColour(1.0, 0.0, 0.0, 1.0);
    },
 
    setToWhite = function()
    {
       canvas.gfx.setClearColour(1.0, 1.0, 1.0, 1.0);
    },
  },
};

// Register all type functions as per their names
foreach( className and classTable in register )
{
  classId = typeId(className);
  print( "class", className, "id", classId );
  foreach( funcName and funcVar in classTable )
  {
    print( "name", funcName, "var", funcVar );
    typeRegisterVariable(classId, funcName, funcVar)
  }
}


Type functions are somewhat efficient, though most script I've seen tends to use tables like that to add member functions to user type instances. I do like the idea of creating and furnishing user types from script in a simple way. It has been on the list for future GM, which time and circumstances have repeatedly delayed.


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 11, 2012 8:33 pm 
Offline

Joined: Fri Jun 19, 2009 12:39 pm
Posts: 31
Greg wrote:
Thanks for the clarification, I see what you mean. Some kind of syntactical sugar would likely make that possible.
You could have implemented the registration entirely in script. eg.

(removed code)



Yup, I considered something like that. The only problem as I see is (keep in mind that I'm lazy :) ) that I would either have to have all type functions in one table, and therefor in one place making the code a bit unmanageable.

I could have separate tables, and then do the registration with a call to a script function at the end of each script that defined type functions. Well...I'd probably do it with a native function as it's likely to get called quite a bit at startup.

Greg wrote:
Type functions are somewhat efficient, though most script I've seen tends to use tables like that to add member functions to user type instances. I do like the idea of creating and furnishing user types from script in a simple way. It has been on the list for future GM, which time and circumstances have repeatedly delayed.


I suspect everybody is using the same sample code when designing their script exposed game objects, which uses a native side table. Not that this is a bad thing, but you need to handle registration every time the object is created and as I'm lazy (have I mentioned that?) it's not something that I want to do.

No doubt any sort of syntax extension to support this won't appear for quite a while as you have better things to do with your time. I toyed with the idea of doing it myself and had a look at the Bison and Flex files . But it looked like more than just a simple chore (as I'd have to learn how the compiler worked) and I'd rather work on my own little game for now.

Thanks for the input though!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


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