GameMonkey Script

GameMonkey Script Forums
It is currently Mon Nov 20, 2017 3:24 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Mon May 31, 2010 7:45 am 
Offline

Joined: Mon May 31, 2010 7:40 am
Posts: 5
Consider the following code:
Code:
test1 = function()
{
   thisTest = table (1,3,5,7);
   thisTest.getTest = function()
   {
      return thisTest[1];
   };
   return thisTest;
};

testers = {golbin=test1(),human=test1()};


if (testers["goblin"].getTest()==3)
{
   msgBox("Worked");
}
else
{
   msgBox("Didnt Work");
}


Have I made an incorrect assumption in thinking you can pass an associated member from the table, or have I done something else wrong?
EDIT: Oh, btw, msgBox is just a c++ bound function I made that does work, replace that with print if it makes you feel more comfortable. :)


Top
 Profile  
Reply with quote  
PostPosted: Mon May 31, 2010 9:15 pm 
Offline

Joined: Mon May 31, 2010 9:12 pm
Posts: 1
seems to be a scope issue (and a typo). this edited version works for me:

Code:
test1 = function()
{
   global thisTest = table (1,3,5,7);
   thisTest.getTest = function()
   {
      return thisTest[1];
   };
   return thisTest;
};

testers = {goblin=test1(),human=test1()};


if (testers["goblin"].getTest()==3)
{
   print("Worked");
}
else
{
   print("Didnt Work");
}


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 01, 2010 1:01 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 698
Welcome to the forum dasmatarix and crapshoot :) Glad you sorted that out.
Yes, the scoping can be a bit confusing at first. Global, local and member are quiet explicit and independent scopes, except for global read access. Using members rather than globals can often make code simpler and safer.


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 01, 2010 5:04 am 
Offline

Joined: Mon May 31, 2010 7:40 am
Posts: 5
*hides head in shame* a typo, woah, thats bad :(

Ok maybe a better question then is what is the best IDE for script editing, with helpful things like script editing and compilation errors like, "well done moron, you made a spelling mistake".

Fixed the typo and it worked fine, no scoping problems (yet).

Thank-you very much :P


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 01, 2010 12:49 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 698
dasmatarix wrote:
...what is the best IDE for script editing, with helpful things like script editing and compilation errors...
Any editor with syntax highlighting for 'C' files, plus the ability to capture output and parse standard errors to jump to line numbers. I use a free notepad replacement called PSPad but there are many compatible editors. DrEvil has a GUI debugger for GM but I'm not sure what its status is, or integrated edit-ability. Most of my own GM code is small scriptlets so a full function debugger is usually not necessary (though definitely a desirable piece of kit). Make sure you enable debug mode and log all errors.


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 01, 2010 1:16 pm 
Offline

Joined: Mon May 31, 2010 7:40 am
Posts: 5
Thanks, I'll definitely check that out


Top
 Profile  
Reply with quote  
PostPosted: Sat Jun 05, 2010 11:23 am 
Offline

Joined: Thu Jan 01, 2004 4:31 pm
Posts: 307
Note that crapshoot's version creates a global table - I don't think you really want that as a change to that will change for all instances, both "goblin" and "human".
Code:
test1 = function()
{
   thisTest = table (1,3,5,7);
   thisTest.getTest = function()
   {
      return this[1];
   };
   return thisTest;
};


testers = {goblin=test1(),human=test1()};


if (testers["goblin"].getTest()==3)
{
   print("Worked");
}
else
{
   print("Didnt Work");
}


As the function "getTest()" is now part of the table, it will have "this" as the scope of the table when called, so the function will invoke the get ind operator on the table


Top
 Profile  
Reply with quote  
PostPosted: Sat Jun 05, 2010 3:39 pm 
Offline

Joined: Mon May 31, 2010 7:40 am
Posts: 5
Yeah, it's a terrible example of script, I'm not actually using it for anything other than testing can use a associated member keyVar from a table to call a function. Where I am using it is part of some net code integration with the script, GM gets the name of the player and the gm variables they have changed from my c netcode, and now it uses that name to reference one of the players in my table of players, and apply the changes.
eg.

// getSyncSource() - c bound function that returns a string containing the name of the source of the net packet
// getSyncType() - c bound function that returns a string containing the name of the variable that has changed
// getSyncVal() - c bound function that gets the value that has changed

// Player is a function that creates a player and has other functions to manage it
player = function(thePlayerName, theIP)
{
thisPlayer = table(playerName = thePlayerName, IP=theIP);
thisPlayer.getName = function(){return .playerName;};

// set method for setting class variables
thisPlayer.set = function(func, param)
{
if (func=="playerName")
{
this.playerName=param;
}
};
return thisPlayer;
};

players = table(player1 = player("mike","145.0.0.1"), player2 = player("john","122.0.0.1"));
players[getSyncSource()].set(getSyncType(),getSyncVal());

I've since changed how it works a bit, but it does help to know I can use a name, not just an index, since if I'm understanding the script reference pdf correctly, gm reorders a table as it expands and contracts, so players[0] might be a different player next time I try to sync, especially across multiple computers.


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