GameMonkey Script

GameMonkey Script Forums
It is currently Wed Jan 23, 2019 2:24 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Apr 04, 2011 3:48 pm 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
Basically i want the "getdot" c-function to call a script-based gmFunctionObject from within the same thread. I have absolutely no idea how to do it but have gotten interesting results. Here's was i have but it crashes (although the function is called and it prints out correctly...it crashes afterwards).

Code:
void GM_CDECL gmInterfaceOpGetDot(gmThread * a_thread, gmVariable * a_operands)
{
    UserInterfaceLib* thisInterface = static_cast<UserInterfaceLib*>(a_operands[0].GetUserSafe(UserInterfaceLib::GetType()));
    gmVariable& thisObject = thisInterface->GetThisObject();

    gmFunctionObject* getDotFunction = thisInterface->GetGetDot();
    if(getDotFunction)
    {
        a_thread->Push(thisObject);
        a_thread->PushFunction(getDotFunction);
        a_thread->Push(a_operands[1]);

        a_thread->PushStackFrame(1);
        a_thread->Sys_Execute(&a_operands[0]);
    }
}


Here's the function that gets called when the getdot is called:

Code:
            function(name) {
               //this will print correctly..
               PrintLine("Value: ", name);
            }


The aim is to allow users to modify the behaviour of get/set dot for any object. For example i have a table
NOTE: this is just an example and not actual code from my project

Code:
Npc = function() {
  myTable = {x=32, y=32, derp=222};
  return myTable;
};


I only want the users to have access to x,y (and not derp) and also i want to monitor when x/y changes (to send updates when it changes).

Code:
Npc = function() {
  myTable = {x=32, y=32, derp=222, UpdateXY = function(){blah}};

  //Create an interface for this table that limits its access and monitors whats been changed
  myInterface = CreateInterface(myTable);
 
  //Assign a get dot for this interface
  InterfaceGetDot(myInterface,
    function(name) {
      if(name == "x" || name == "y") {
        return this[name];
      }
    });

  //Assign a set dot for this interface
  InterfaceSetDot(myInterface,
    function(name, value) {
      if(name == "x" || name == "y") {
        this[name] = value;
        this.UpdateXY();
      }
    });
  //return that interface to the user. Using this interface they can only access x/y and it can call update methods when they're changed
  return myInterface;
);


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 05, 2011 1:31 am 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
Damn just realised this idea isn't as good as i thought it was. Any member called through the interface will still be limited by the interface object. So if i have code like this:

Code:
npc = InterfaceCreate(npcTable);
InterfaceGetDot(npc, function(name) {
    if(name == "Hide") {
      return this.Hide;
    }
  });
 
npc.Hide();


The code within the Hide() function will also be limited since the "this" object that is being passed to Hide is the interface object and not npcTable x_x

Although i would still like to know how to call a gm-bound function from within c without having to create new thread


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 15, 2011 11:54 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
Just a few thoughts...

I see what you are trying to do, but I don't think modifying Get/Set dot like that is a good idea.
Certainly you can provide or deny access to some named logical members or pass through to a gmTable or other object.
You won't be able to call other functions from within GetDot as the VM is not in a state to allow that. Also you should not be trying to re-use the same thread to call another function. Just let native code create another gmThread. If performance is a real issue, lake a step back and see what you are trying to achieve and implement the slow logic natively and keep GM for control logic.

It is nice to show code where dot operators, members and functions may be native or script and the user doesn't know or care, but there is value in trying to keep things simple if at all possible. You can usually achieve the desired result even if the syntax doesn't look quite as you'd prefer.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 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