nacho_chicken
Member
Essentially, I'm trying to do some form of namespacing to aid in passing common functions between out-of-battle characters and in-battle units. Calling a method passed down crashes, but only when it is not static. This appears to be a scope error, as the non-static one appears to be called from the container somehow, despite calling the function specifically through the character struct. This is a bit easier to explain in code, so I'll go ahead and do so:
EDIT: Fixed some stuff here. Forgot the static keyword.
The debug console reads:
GML:
// Create event of objCharacterController
var _container = function() constructor {
// This name variable is for debug purposes so the scope problems are obvious
name = "Container";
static heal_hp_static = function(_amount) {
show_debug_message(name); // Outputs to debug console to show what is calling the function
var _prevHP = hp;
hp = min(mhp, hp+_amount);
show_debug_message(hp);
return (hp-_prevHP);
}
heal_hp = function(_amount) {
show_debug_message(name);
var _prevHP = hp;
hp = min(mhp, hp+_amount);
show_debug_message(hp);
return (hp-_prevHP);
}
}
// commonFunctions is effectively a container struct for functions
// It exists so other objects can pull functions from it
commonFunctions = new _container();
GML:
character = function() constructor {
// For debug output
name = "Character";
// Passing methods
static heal_hp_works = objCharacterController.commonFunctions.heal_hp_static;
static heal_hp_crashes = objCharacterController.commonFunctions.heal_hp;
// HP values set below maximum so I have something to restore
hp = 4;
mhp = 8;
}
player1 = new character();
GML:
player1.heal_hp_works(2);
player1.heal_hp_crashes(2); // Game crashes!
Is this a bug, or is it an expected error? If the latter, why?Character
6
Container
<error code, "Variable anon_gml_Object_objCharacterController_Create_0_122_gml_Object_objCharacterController_Create_0.hp(100268, -2147483648) not set before reading it.">
Last edited: