Select Page

I’ve been using constructor functions, already:

function my_func() contructor {
  value_1 = 0;
  value_2 = 0;

  init = function(val_1,val_2) {
    value_1 = val_1;
    value_2 = val_2;
  }
}

// calling as:
my = new my_func();
my.init(1,2);

I was chatting about with GPT and it pointed out something that made me smack myself on the head with a loud DUH!

function my_func(val_1,val_2) contructor {
  value_1 = val_1;
  value_2 = val_2;
}

// calling as:
my = new my_func(1,2);

That’s just a simple example, but the ramifications are far reaching when you take it in context of NPCs and other objects. For example, imagine this:

function NPC_Alpha(owner) contructor {
	NPC_Object = owner;
  dimming = true;
  current_alpha = 1;

  controller = function() {
    if(dimming) {
      current_alpha -= 0.1;
      if(current_alpha <= 0) {
        dimming = false;
      }
    } else {
      current_alpha += 0.1;
      if(current_alpha >= 1) {
        dimming = true;
      }
    }
  }
}

// calling as:
var inst = instance_create_layer(100,100,"instance_layer",obj_ship_type);
inst.npc_alpha = new NPC_Alpha(inst);

// then in your step event for the obj_parent_ship, the thingy
// that runs through all the children and does junk, you'd have...
npc_alpha.controller();

And that would make it so each of your instances would dim and glow repeatedly, each having its own junk. Seems pointless, until you do something like this:

function NPC_Alpha(owner) contructor {
	NPC_Object = owner;
  dimming = true;
  current_alpha = 1;
  alpha_speed = random_range(0.0001,0.1);

  controller = function() {
    if(dimming) {
      current_alpha -= alpha_speed;
      if(current_alpha <= 0) {
        dimming = false;
      }
    } else {
      current_alpha += alpha_speed;
      if(current_alpha >= 1) {
        dimming = true;
      }
    }
  }
}

// calling as:
var inst = instance_create_layer(100,100,"instance_layer",obj_ship_type);
inst.npc_alpha = new NPC_Alpha(inst);

// then in your step event for the obj_parent_ship, the thingy
// that runs through all the children and does junk, you'd have...
npc_alpha.controller();

Now each instance child of the obj_parent_ship will be dimming and brightening at a unique rate from the other ships.

This has far reaching ramifications for doing things like AI, for example. Every one of your NPCs can have their own Finite State Machine steps and so on using this methodology.

It’s probably common knowledge to some, but to me it was definitely a eureka moment, so chalk one up for my GPT buddy!

Progress

  • I’m working on separating out the AI stuff so it can be used on any ship.
  • Changed junk over to the new constructor methodology and the ships are all playing nicely with it. weeee!