• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Parent question

L

Latch

Guest
Am I right in assuming I can create an object with all the code required inside it, and then have other objects use the code inside of it?

For example I could create an object that each of 4 players (human controlled) use the same parent that code is taken from but are each unique and wont interfere with each other?

Here is how I setup the player object:

Code:
if canmove = true//If the player is allowed to move
{
    //PLAYER ONE
    if global.p1input = 0 && player = 1//If the user is using a keyboard
    {
        up       = keyboard_check (ord('W'));//Set the movement keys
        down     = keyboard_check (ord('S'));
        left     = keyboard_check (ord('A'));
        right    = keyboard_check (ord('D'));
        attack   = keyboard_check_pressed (ord('Q'));
        interact = keyboard_check_pressed (ord('E'));
        useitem  = keyboard_check_pressed (ord('F'));
    }
    
    //PLAYER TWO
    if global.p2input = 0 && player = 2//If the user is using a keyboard
    {
        up       = keyboard_check (vk_up);//Set the movement keys
        down     = keyboard_check (vk_down);
        left     = keyboard_check (vk_left);
        right    = keyboard_check (vk_right);
        attack   = keyboard_check_pressed (vk_space);
        interact = keyboard_check_pressed (vk_rcontrol);
        useitem  = keyboard_check_pressed (vk_numpad0);
    }
    
    //Movement
    if up    = true {if canmoveup    = true {y-= playerspeed;}}//If the player presses the correct key, perform the actions if they are allowed to
    if right = true {if canmoveright = true {x+= playerspeed;}}
    if down  = true {if canmovedown  = true {y+= playerspeed;}}
    if left  = true {if canmoveleft  = true {x-= playerspeed;}}
            
    //Setting the facing of the player
    if up    = true && left  = false && right = false {facing = 0;}
    if up    = true && right = true                   {facing = 1;}
    if right = true && up    = false && down = false  {facing = 2;}
    if right = true && down  = true                   {facing = 3;}
    if down  = true && left  = false && right = false {facing = 4;}
    if down  = true && left  = true                   {facing = 5;}
    if left  = true && up    = false && down = false  {facing = 6;}
    if up    = true && left  = true                   {facing = 7;}   
}

Would I then create two objects that use this as the parent, assign them the 'player' variable relevant to the player they are and they should move around freely able to alter their speed etc independently, or have I gone wrong somewhere?
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
That is correct; To execute parent object's code from a child object, you would use event_inherited().

You could also assign the variables determining the buttons to be passed into keyboard_ functions instead of having separate branches for each player's input.
 
Top