Override a function of a parent object in the child object.

Xsias

Member
Hello everybody,

I've quite a little diffculty on something. Here is the recap :
I have an object obj_aaa_par with a function in his create event :
GML:
function generic_func(_var) constructor {
      obj_idx = _var.object_index;
      x = _var.x;
      y = _var.y;
      name = _var.name;
}
This object is parent of my second object : obj_aaa.
This one have this code in his create event :
GML:
event_inherited();

function generic_aaa_func(_var) : generic_func(_var) constructor {
     type = _var.type;
}
This is the code. Now my problem : When i build my project, i have this error :
GML:
ERROR in
action number 1
of Create Event
for object <undefined>:

Error on load
Unable to find function gml_Script_generic_func
I dond't understand why.
I can override in my child object the function of my parent object, no ?
 

samspade

Member
Do you have the issue if you take both functions and put them in a script asset? What about if they're both in the parent? If the answer is no, then it probably has to do with how inheritance works. You can't inherit from method variables. These aren't method variables, but they are local to the object. It might be that you can't use struct inheritance on script functions in parent objects even with event inherited.

While a little clumsy, you could do this:

GML:
///parent

function generic_func(_var) constructor {
      obj_idx = _var.object_index;
      x = _var.x;
      y = _var.y;
      name = _var.name;
}

///child

event_inherited();

function generic_func(_var) constructor {
      obj_idx = _var.object_index;
      x = _var.x;
      y = _var.y;
      name = _var.name;
      type = var.type;
}
That will simply overwrite the generic_func in the child object though obviously you lose the benefit of struct inheritance. The other solution is just do this in a script asset and assing the func to a variable;

GML:
///script asset

function generic_func(_var) constructor {
      obj_idx = _var.object_index;
      x = _var.x;
      y = _var.y;
      name = _var.name;
}

function specific_func(_var) : generic_func(_var) constructor {
      type = _var.type;
}

/// parent

my_func = generic_func;

/// child

event_inherited(); //if you've got other stuff you need to inherit

my_func = specific_func;
 

Xsias

Member
Do you have the issue if you take both functions and put them in a script asset? What about if they're both in the parent?
Nope and nope. Both of this solutions works, but for code clarity i was looking for an other way of doing this.

While a little clumsy, you could do this:

GML:
///parent

function generic_func(_var) constructor {
      obj_idx = _var.object_index;
      x = _var.x;
      y = _var.y;
      name = _var.name;
}

///child

event_inherited();

function generic_func(_var) constructor {
      obj_idx = _var.object_index;
      x = _var.x;
      y = _var.y;
      name = _var.name;
      type = var.type;
}
That will simply overwrite the generic_func in the child object though obviously you lose the benefit of struct inheritance.
Yes, i can do this, i just found this a little ugly.

The other solution is just do this in a script asset and assing the func to a variable;

GML:
///script asset

function generic_func(_var) constructor {
      obj_idx = _var.object_index;
      x = _var.x;
      y = _var.y;
      name = _var.name;
}

function specific_func(_var) : generic_func(_var) constructor {
      type = _var.type;
}

/// parent

my_func = generic_func;

/// child

event_inherited(); //if you've got other stuff you need to inherit

my_func = specific_func;
It's a good idea, i haven't think about this way.

It's too bad that you can't do the way i first wanted to do it. IMO, it's the cleanest way for what i want. And the more Object Oriented.
 
Top