Global Variable vs Objects

C

Chatterb0x

Guest
General question here.

I have an object which spawns blood patterns. An enemy's blood trajectory changes based on how they were killed. I'm thinking about implementing global variables for each. This would mean the game checks global variables for each blood instance. Is this a practical use of resources? Should I make different blood spawning objects for each attack?
 

YoSniper

Member
Actually, I would suggest that if the blood pattern is dependent on how the enemy is killed, why not just write a script to spawn the blood accordingly?

Alternatively, if the blood spawning spans multiple steps, just have one object that uses a variable to determine how it animates the blood spawn.

For example, in the blood spawner's step event, have something like this:
Code:
switch(how_killed) {
    case method_1:
        //Animate blood accordingly
        break;
    
    case method_2:
        //Animate blood differently than for method_1
        break;
    
    //etc...
}
Of course, this would mean that upon crating the instance, you would immediately have to specify the how_killed variable, like so:
Code:
//In event where enemy is killed
var bspawn;
bspawn = instance_create(x, y, obj_blood_spawn);
if condition1 {
    bspawn.how_killed = method_1;
} else if condition2 {
    bspawn.how_killed = method_2;
}
//...etc
 
  • Like
Reactions: Nux
C

Chatterb0x

Guest
Thanks YoSniper. This is actually what I had before and I think we're on the same page. We even had very similar naming convention. :)
This code occurs in obj_player Begin Step event. blood_pattern1,2, and 3 are global variables.

if(x_initial < x_following){
attack_random = ceil(random(3));
switch (attack_random){
case 1:
swipe_ready = 0;
image_xscale = 1;
image_speed = .45;
sprite_index = spr_playerslash;
image_index = 0;
global_speed = -15;
blood_pattern3 = true;
for (var i = 0; i < 10; i++){
var x_random = x + random_range(-125,125);
var y_random = y + random_range(-100,-300);
instance_create(x_random,y_random,obj_bloodspawn);
}
blood_pattern3 = false;
break;

case 2:
swipe_ready = 0;
image_xscale = 1;
image_speed = .45;
sprite_index = spr_playerstab;
image_index = 0;
global_speed = -15;
blood_pattern2 = true;
for (var i = 0; i < 5; i++){
var x_random = x + random_range(200,300);
var y_random = y + random_range(-100,-300);
instance_create(x_random,y_random,obj_bloodspawn);
}
blood_pattern2 = false;
alarm[1] = room_speed * .15
break;

case 3:
swipe_ready = 0;
image_xscale = 1;
image_speed = .45;
sprite_index = spr_playerdownslash;
image_index = 0;
global_speed = -15;
blood_pattern1 = true;
for (var i = 0; i < 10; i++){
var x_random = x + random_range(-100,100);
var y_random = y - 200 + (i*5);
instance_create(x_random,y_random,obj_bloodspawn);
}
blood_pattern1 = false;
break;
}}


Let me rephrase my question:
At which number do global variables become inefficient.

I have 13 currently.
 

YoSniper

Member
Let me rephrase my question:
At which number do global variables become inefficient.

I have 13 currently.
I'd say once you get beyond 2, you have too many.

If there's one thing I've learned from coding professionally, it's that modularization, and keeping code as centralized as possible, are some of the best coding practices.
 
Top