1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

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

Discussion in 'Programming' started by IcyZ1ne, Feb 24, 2019.

  1. IcyZ1ne

    IcyZ1ne Member

    Joined:
    Jan 22, 2019
    Posts:
    9
    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).
     
  2. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    572
    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: Feb 24, 2019

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice