game-makergmlgame-maker-language

Method inheritance in GML


So, I have a "trigger" system in a mod for a Pizza Tower, i made so that there is a obj_trigger parent and an obj_delay child, the parent has a method to execute the main functionality of a trigger.

obj_trigger Create

trigger_id = -1;
target_trigger_id = -1;

on_enter = 1;
on_touch = 0;
on_trigger = 0;

multi = 0;
activated = 0;

execute = function(triggered) //gml_Script_anon_gml_Object_obj_trigger_Create_0_0_gml_Object_obj_trigger_Create_0
{
    var triggered = argument0;
}

obj_trigger Room Start

if (on_enter && (!activated))
{
    execute(false)
    if (!multi)
        activated = 1;
}

obj_delay Create

event_inherited()
delay = 1
execute = function(argument0) //gml_Script_anon_gml_Object_obj_delay_Create_0_0_gml_Object_obj_delay_Create_0
{
    var triggered = argument0
    if (triggered && (!on_trigger))
        return;
    if (triggered && activated)
        return;
    if (triggered && (!multi))
        activated = 1
    if ((delay == 0))
        alarm[0] = 1
    else
        alarm[0] = (delay * room_speed)
}

The problem here is that I'm trying to overwrite gml_Script_anon_gml_Object_obj_trigger_Create_0_0_gml_Object_obj_trigger_Create_0 with the new method but it instead turns into gml_Script_anon_gml_Object_obj_delay_Create_0_0_gml_Object_obj_delay_Create_0

I am using UndertaleModTool so I don't know if that has something to do with it.


Solution

  • The way to fix it is: Always prefix your methods with self.

    For example to avoid the function buffer thing from creating a non-existing function in any other files you can do for example: self.execute() This way you can do execute = function() {things here} on the child and the parent script will execute the new function definition!

    Also, this is using UTMT:CE and if you put self. and it removes it is because it is not necessary if it keeps it, it matters.

    I made this conclusion by a bit of testing so everything might not be true.