Asset - Extension Object.Method *UPDATE*

ZeDuval

Member


UPDATE: v2.0.3; New:

  • Last version included as a rollback-possibility
  • Supports an unlimited amount of methods now, instead of 15 like before
  • Supported number of arguments increased to 20

Object Method enhances the otherwise object-oriented approach of Gamemaker with the possibility to define and then invoke named methods(functions), that are bound to objects, fully functional with up to 20 arguments and a return value.

In the Create - Event of the object obj_abc use...
Code:
method_init();
Method1 = method_def();
Method2 = method_def();
...to initialize the object.Method-system for this object and to define these methods and bind them to the variables Method1 and Method2.

In the User Defined 15 - Event (Add Event > Other > User defined > User 15) you can either have one "Execute Code"-Block like this...
Code:
/// all object.Methods
if method(){
  show_message("Method1 "+A0+A1);
}
if method(){
  RETURN = A0 + A1;
}
...or you could use one "Execute Code"-Block per method like this...
Code:
/// Method1
if method(){
  show_message("Method1 "+A0+A1);
}
and
Code:
/// Method2
if method(){
  RETURN = A0 + A1;
}
You just have to make sure that the methods in the User Defined 15 - Event are in the same order/succession as in the Create - Event where you defined them!

Within the object itself you can then use...
Code:
method_invoke(Method1,"with"," arguments");
var ret = invoke(Method2,1000,337);
show_message(ret);
to invoke Method1 with two arguments and invoke Method 2 with two arguments and to also get the return value. The message shown for Method1 would then be "Method1 with arguments" and the message shown for the return value of Method2 would then be "1337".

To invoke a method from another object that has a reference-variable to obj_abc use...
Code:
abc = instance_create(0,0,obj_abc);
method_invoke(abc.Method1,"with"," arguments");
To invoke a method on all instances of an object use
Code:
method_invoke_obj(abc.Method1,"with","arguments");
or
Code:
method_invoke_obj(obj_abc.Method1,"with"," arguments");
There is also one shortcut-function called invoke() which you can use just like method_invoke().



Special thanks to elementbound for brainstorming af and the suggestion to use only UE15 and to provide an unlimited amount of methods.

Tested for following export modules: Windows, Windows(YYC), HTML5.
 
Last edited:

ZeDuval

Member
Hello everyone,

as I'm about to publish some new assets in the near future, I decided to update my current assets based on my own usage-experience from the past months. object.Method now supports an unlimited amount of methods per object. :)

I changed some parts of this asset and also some function names. If you already use this asset, you will have to make some changes. I included the last version of this asset as an included file for everyone who updated this asset but doesn't want to make those changes!

Let me know if you encounter any bugs or if you have a suggestion!
 

DukeSoft

Member
I like the way you think, but isn't this basically a bit of a workaround? I understand there's very limited support for real object oriented stuff, but this just seems like more of a hassle than making a folder and throwing a lowercased-underscore function in the (obj_name_function).

I don't mean this negatively, I agree with you that a real object oriented method would be awesome in GM, but is feels like "run_script(scriptname, instance, arguments)" isn't really better than with(obj_instance) {run_script();}.

When i saw the name of your asset I was assuming we could have something like;

Create event:
Code:
function myFunction(var1, var2) {
return var1+var2;
}
and then execute like;

show_debug_message(obj_instance.myFunction(2, 4));
 

ZeDuval

Member
When i saw the name of your asset I was assuming we could have something like;

Create event:
Code:
function myFunction(var1, var2) {
return var1+var2;
}
and then execute like;

show_debug_message(obj_instance.myFunction(2, 4));
This would be awesome, of course, and that's obviously the pattern I wanted to emulate with this extension. Sadly, this exact pattern is simply not supported by GM's syntax and the syntax cannot be changed/manipulated/extended by an extension(not talking about external code editors, parsing and interpreting).

I like the way you think, but isn't this basically a bit of a workaround?
A bit? ;)
I don't mean this negatively, I agree with you that a real object oriented method would be awesome in GM, but is feels like "run_script(scriptname, instance, arguments)" isn't really better than with(obj_instance) {run_script();}.
You misunderstand the intention of this extension here. It was never the plan to provide an alternative to
Code:
with(obj){scr_whatevs();}
but to provide an alternative to
Code:
with(obj){event_user(7);}
The user-defined-events have the following pros and cons:
Pro:
  • Unlike scripts, actually in the scope of the object
  • Unlike scripts, actually bound to the object
Cons:
  • Limited amount
  • No arguments
  • No return value
This extension is about utilizing the pros of user-defined-events while negating the cons, which it does.

I understand there's very limited support for real object oriented stuff, but this just seems like more of a hassle than making a folder and throwing a lowercased-underscore function in the (obj_name_function).
When following the object-oriented approach with getters and setters and all, even the most simple objects quickly get a rather big amount of methods and I would not want to have such an amount of scripts per object, where some scripts would not contain much more than for example:
Code:
/// hero_set_name
if is_string(argument0){
  name=argument0;
}else{
  show_debug_message("wrong type of input. expected string.");
}
 

DukeSoft

Member
Aaah, I missed the intention you had then. In that case, sweet! I'm never really using events like that - but this might open up some doors. I'll be sure to check it out man.

Thanks for your contribution :)
 
S

Salvakiya

Guest
aha! I meshed your method with my method... XD the puns! I do like it!
 
Top