• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Discussion Feature suggestion - "Def" function for creating methods

C

C.H. Davis

Guest
So, most other programming languages widely used to make games have a function called "Def" or "Define", which allows you to make scripts local to each object, called methods. Since each method is created by running code within an object, it can be dynamically written using different variables within that object. And, since each a child-object can have different methods under the same names than its parent, these could be used in the place of user events as well, and you could have way more than 15 of them per object. I think this would be a great addition in GM:S 2.0, but what do you guys think? Is there a reason that this hasn't been implemented that I'm overlooking?

Thanks!
 

chamaeleon

Member
So, most other programming languages widely used to make games have a function called "Def" or "Define", which allows you to make scripts local to each object, called methods. Since each method is created by running code within an object, it can be dynamically written using different variables within that object. And, since each a child-object can have different methods under the same names than its parent, these could be used in the place of user events as well, and you could have way more than 15 of them per object. I think this would be a great addition in GM:S 2.0, but what do you guys think? Is there a reason that this hasn't been implemented that I'm overlooking?
There's inline functions on the roadmap that could be somewhat relevant.
Code:
var a = function( a, b ) { ... }
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
That particular schema is easily simulated.
Say, you make a script named "def", which does just
Code:
return [self, argument0];
then, you would make a second script, named "mt" (method) or "call" or anything,
Code:
var q = argument0;
with (q[0]) return script_execute(q[1]);
show_error("Couldn't find " + string(q[0]) + " to execute script "
    + script_get_name(q[1]) + " on.", false);
now, if you had a script called scr_greet with
Code:
return name + " says 'hi'!";
you could use it in your Create event,
Code:
talk = def(scr_greet);
and later do
Code:
var text = mt(myobj.talk);
which, quite expectedly, would call scr_greet on that instance.

With this approach you can also store a "method reference" in a variable and call it later.

Edit: After a while I wrote a post about this.
 
Last edited:
Top