SOLVED How to assign and de-assign methods ?

Imperial

Member
Hello

let's suppose we have a player object with 2 events

Create Event
GML:
update = undefined;
Step Event
GML:
if(is_method(update))
{
    update();
}
and then you try to create a player instance and give it a method
GML:
var inst = instance_create_layer(0,0,"Layer 1",obj_player);

inst.update = function()
{
    if(x >= room_width/2)
    {
        show_debug_message("Level Completed");
        //What to do to make this method equals to undefined again ???
    }
}
so the problem is I don't know how to assign undefined again to the update variable to make it stop checking once the condition is true

I appreciate it if you enlighten me
 
Last edited:

FoxyOfJungle

Kazan Games
Simple, set to undefined.
GML:
inst.update = undefined;

Change the Step Event of the player to:
GML:
if (!is_undefined(update)) {
    update();
}
 

FoxyOfJungle

Kazan Games
Thats the problem, I don't have access to that "inst" variable anymore
You can save the variable in the Create Event, so it's no longer a temporary variable.
GML:
player_id = noone;
GML:
player_id = instance_create_layer(0, 0, "Layer 1", obj_player);
GML:
player_id.updated = undefined;
// OR
obj_player.update = undefined;
 

Nidoking

Member
inst.update = function()
{
if(x >= room_width/2)
{
show_debug_message("Level Completed");
//What to do to make this method equals to undefined again ???
}
}
You're running this "method" (seems unbound to me, but if is_method is true, then whatever) inside the instance that is called inst here, yes? Then finding inst is incredibly easy. inst is self. Just say update = undefined; or, in my opinion, more reasonably, update = function() {} and then just call update every step without needing to check whether it's defined. If it's empty, it'll do nothing.
 

FrostyCat

Redemption Seeker
The method is actually bound to the wrong owner. See this reply, particularly the row about ownership.

In your case, you have to change the binding ownership by using method directly:
GML:
var inst = instance_create_layer(0, 0, "Layer 1", obj_player);

inst.update = method(inst, function() {
    if (x >= room_width/2) {
        show_debug_message("Level Completed");
        update = undefined;
    }
});
Then update would be accessible inside the method because it is owned by the instance of obj_player.
 

Imperial

Member
The method is actually bound to the wrong owner. See this reply, particularly the row about ownership.

In your case, you have to change the binding ownership by using method directly:
GML:
var inst = instance_create_layer(0, 0, "Layer 1", obj_player);

inst.update = method(inst, function() {
    if (x >= room_width/2) {
        show_debug_message("Level Completed");
        update = undefined;
    }
});
Then update would be accessible inside the method because it is owned by the instance of obj_player.
Thanks for helping

[Solved]
 
Top