• 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!

2 Player Co-Op Instance Creation not functioning as intended.

Dcm05

Member
So i'm working on a fighting game and i would like each of the players to be able to choose their characters. everything works up until the final bit. When just player one is activated, the code works as normal. but, when player 2 is activated, two instances are created where player two starts and the second character object is controlled by player 1???
This is what i have:

Code:
obj_player1 Step Event:
if (room == rm_game) && (done == false)
{
    instance_create(player_x, player_y, object_create);
        with (instance_create(player_x, player_y, object_create))
        {
            controller = global.p1_controller;
            ///global.p1_controller is set to 0
        }
    done = true;
}
here's player 2's step event
Code:
obj_player2 Step Event:
if (room == rm_game) && (done == false)
{
    instance_create(player_x, player_y, object_create);
        with (instance_create(player_x, player_y, object_create))
        {
            controller = global.p2_controller;
            ///global.p1_controller is set to 1
        }
    done = true;
}
any idea why a second instance is being made? the default controller value is 0 for every character but after changing it the same issue occured...
 

FrostyCat

Redemption Seeker
You are making a second instance because instance_create() got called twice. Get rid of your first.
GML:
if (room == rm_game) && (!done)
{
    with (instance_create(player_x, player_y, object_create))
    {
        controller = global.p1_controller;
        ///global.p1_controller is set to 0
    }
    done = true;
}
 

sp202

Member
Seems like you're using individual objects for each player rather than making them both instances of the same object, doing the latter will prevent a lot of duplicate code.
 

Dcm05

Member
You are making a second instance because instance_create() got called twice. Get rid of your first.
GML:
if (room == rm_game) && (!done)
{
    with (instance_create(player_x, player_y, object_create))
    {
        controller = global.p1_controller;
        ///global.p1_controller is set to 0
    }
    done = true;
}

Thank you so much! This worked perfectly.

Seems like you're using individual objects for each player rather than making them both instances of the same object, doing the latter will prevent a lot of duplicate code.
Can you explain further what you mean? I want to make sure I optimize my code well and don't create issues later on.
 

FrostyCat

Redemption Seeker
Can you explain further what you mean? I want to make sure I optimize my code well and don't create issues later on.
He means to set up only one player object, and use a variable in it to indicate whose side it plays for.

For example, would you make two different objects, one for a visible enemy and another for an invisible but otherwise identical enemy? No, you would instead make just one object and set the visible variable. Your case deals with a custom variable instead of one that's built in, but that doesn't change the idea.
 

Dcm05

Member
He means to set up only one player object, and use a variable in it to indicate whose side it plays for.

For example, would you make two different objects, one for a visible enemy and another for an invisible but otherwise identical enemy? No, you would instead make just one object and set the visible variable. Your case deals with a custom variable instead of one that's built in, but that doesn't change the idea.
Ahhhhhh, I get it. Thank you so much for the help šŸ˜
 
Top