• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Referencing variables GMZ

C

Carpe Zythum

Guest
I truly do miss some features of OOP languages. I'm having a problem implementing a modular upgrade system for my game in Game Maker.
I have a GUI object "+", and I want to be able to upgrade several aspects of my tower. Ex: max hit points ( soul ), damage, etc.
The problem is, I want to abstract this functionality. I'd like to be able to send a "pointer", ( a reference ) to a field in my Tower class to this button so it can update the variable.
Code:
...
// Create the GUI button for upgrading tower's soul ( hit points )
soul_increase = instance_create(topx + 210, topy + 58, obj_plus);

    with(soul_increase) {
        amt = 10; // The amount the + button should increase
        controll_var = * (pointer to, is it possible though?) tower_instance.soul;
    }

damage_increase = instance_create( ..., obj_plus );
   with(damage_increase) {
    amt = 3.2;
   controll_var = * tower_instance.damage
}
...
This way, I would save me a lot of unnecessary lines of code.
The only solution (using the resources in GMZ ) I came up with is passing a string name of the field I want to change, and then using a switch statement to find the appropriate field in the Tower object.
Or is there a way to do something like javascript can do ex: object["instance_variable"] = value?
 

RangerX

Member
You can access and change the variables in an instance you did catch in a variable like this:

DidCatch.Variable=Value;
 
C

Carpe Zythum

Guest
Yeah, I know. But I want my other object, in this case, "damage_increase" or "soul_increase", to dynamically change some instance variables.
If you do know javascript here's what I'm trying to do in JS:
Code:
UpgradeButton = function( tower, tower_field, amt ) {
   this.tower = tower;
   this.tower_field = tower_field;
   this.amt = amt;
}

UpgradeButton.prototype.click = function() {
   this.tower[this.tower_field] += this.amt;
}

tower = {
   x: 100,
   y: 100,
   soul: 250,
   damage: 10
}

damage_upgrade = new UpgradeButton( tower, "damage", 1 );
soul_upgrade = new UpgradeButton( tower, "soul", 50 );

console.log("Tower soul: " + tower.soul); // 250
console.log("Tower damage: " + tower.damage); // 10

damage_upgrade.click(); // When the user clicks the button, tower damage increased
soul_upgrade.click(); // When the user clicks the button, tower soul increased

console.log("Tower soul: " + tower.soul); // 300
console.log("Tower damage: " + tower.damage); // 11
 

FrostyCat

Redemption Seeker
The closest way is to store these properties as a map instead of individual variables.

Tower Create:
Code:
props = ds_map_create();
props[? 'damage'] = 10;
props[? 'soul'] = 250;
Tower Destroy:
Code:
event_user(13);
Tower Room End:
Code:
if (!persistent && !room_persistent) {
  event_user(13);
}
Tower User Event 13:
Code:
ds_map_destroy(props);
Constructor script for towers:
Code:
///Tower(x, y, soul, damage)
{
  with (instance_create(argument0, argument1, obj_tower)) {
    props[? 'soul'] = argument2;
    props[? 'damage'] = argument3;
    return id;
  }
}
The Create event is the main focus of what you're asking for. The other three events is a standard combination for cleaning up instance-scope resources, and the script is a standard analog of constructors in GML.

Once this is set up, you can simply do something like this:

Button Create:
Code:
tower = instance_find(obj_tower, 0);
property = "damage";
change = 10;
Button Mouse Left Pressed:
Code:
tower.props[? property] += change;
Constructor script for creating a a button:
Code:
///UpgradeButton(x, y, target, property, change)
{
  with (instance_create(argument0, argument1, obj_upgrade_button)) {
    tower = argument2;
    property = argument3;
    change = argument4;
    return id;
  }
}
 
Top