How can you make an object with independent variables?

B

Boom

Guest
The title is confusing but I don't know how else to describe it.
I will try my best to simplify with this analogy

For example, I have a bullet. I want to make it go a range of different speeds without having to make each speed its own object.

My plan is to find a way to change the variable when it created but I don't know how to do it.

Using a global variable like this will work:

Summoner's step event
global.speed = 0
instance_create_layer(x,y,"Instances",bullet)
global.speed = 3
instance_create_layer(x,y,"Instances",bullet)
global.speed = 5
instance_create_layer(x,y,"Instances",bullet)
Bullet's creation event
placeholder_speed = global.speed
Bullet's step event
x += placeholder_speed
but this feels like a very roundabout way of doing it. Is there a simpler or cleaner way of doing this?
 
I'd look into the random_range and choose functions. For example:
GML:
Bullet's Create Event:

placeholder_speed = choose(0,3,5);

//NOTE: If placeholder_speed is set to zero, your bullet will not actually go anywhere.
 

2DKnights

Member
The instance_create functions return an id that you can store and use to refer to an instance and it's variables directly.


GML:
var b1 = instance_create_layer(x, y, "layer", bullet);
b1.speed_variable = 3;
 
Last edited:

HayManMarc

Member
When you create the instance, attach the variables to that particular instance.

Code:
var bullet = instance_create_layer(x, y, "Instances", bullet);
bullet.spd = 3;
This creates the bullet and assigns its instance id to the variable "bullet", then assigns the value "3" to the variable "spd" for this particular instance of a bullet object.
 
Top