GMS 2 How to set variables for clones of the same object?

IcyZ1ne

Member
So I am working on a fighting game, and in it (of course) I want to have two players to fight against eachother. The problem is that I have 2 instances of the same object and they both use the same controls. How do I make them have separate "moves"? (I would give some code but I have no clue on how to even start).
 

dannyjenn

Member
I can think of a few ways you could fix the problem:

1.) The easiest (and most efficient) way is to just use two different objects. Duplicate the one object and change the controls. That way they won't have the same controls.

2.) You could use a single object with two different sets of possible controls... the actual controls would be determined with a variable and if statement. e.g.
Code:
// when the instance is created:
controls = 1; // for player 1
// or
controls = 2; // for player 2

// step event:
if(controls==1){
    if(keyboard_check(vk_right)){
        punch_right();
    }
    if(keyboard_check(vk_down)){
        dodge();
    }
    // etc.
}
else{
    if(keyboard_check(ord("D"))){
        punch_right();
    }
    if(keyboard_check(ord("S"))){
        dodge();
    }
    // etc.
}
3.) (This'll probably work as long as you don't use any strange keys like control or shift.) You could keep track of the controls in variables. e.g.
Code:
// when the instance is created:
// for player 1:
right_key = vk_right;
down_key = vk_down;
// etc.
// or
// for player 2:
right_key = ord("D");
down_key = ord("S");
// etc.

// step event:
if(keyboard_check(right_key)){
    punch_right();
}
if(keyboard_check(down_key)){
    dodge();
}
// etc.

Note: The best practice is probably to use fully-customizable controls (like my third way but with an in-game screen allowing the player to set the controls himself). That way there won't be problems for people who have different keyboard layouts, or cheap keyboard which don't support multiple keys at the same time, etc.
 
Last edited:
Top