• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ Creating variations of a character in a multiplayers game

Ralf

Member
Greetings,
I'm discovering (and enjoying !) Gamemaker, and started to watch several hours of tutos before starting my own.
I'm already facing a problem : I'd like to personalize a selected character with a menu, but I don't know how to "give" him informations.

What I have done so far :
  • detected plugged gamepads
  • created an instance of a "player object" for each plugged gamepad
  • In "player object", I have a step event : if I press a button, an instance of a "menu object" is created. So, each "player object" instance creates its own "menu object" instance.
  • I can pass informations from player instance to the menu it's created, with :
GML:
var inst = instance_create_layer(x - (128 / 2), y + 250, "layer_players", obj_menu);
with (inst) {
    player_number = other.pad_num;
}
"player_number" is a variable created for the menu, to give it the gamepad (and so the player) number.

It "works", as the menu is effectively "linked" to the right player instance : if I use the gamepad 2, I move into menu 2.

Now the tricky part : after used this menu, I need to pass the new information to the right instance !
In this menu, I can select a "class" for the character (knight, wizard...).

Of course, this code doesn't work :
GML:
if (gamepad_button_check_pressed(player_number, gp_face1)) {
    obj_player.character = menu[menu_committed];
}
because the "menu[menu_committed]" information (knight, wizard...) will be send to all player object instances, and that's not what I want. The first player could chose knight, the player 2 the wizard...

Is it possible ton send back an info to the "parent" instance (the one which created this one...) ?
Am I completely wrong about the logic of what I want ? Should I use instance IDs ?

After that, all is ready in my code : each player object instance will change its sprites according to the right keyword of my menus (knight...), but I need to give that information to the player object instances ! :)

Thank you for your time.
 

ThraxxMedia

Member
I'm doing something similar with light sources (that can have owners) in my most recent project.
While this won't apply to your specific situation, I hope you'll still be able to get the gist and use the knowledge.

This is what it looks like:

In the owner's CREATE event:
GML:
ls = instance_create_layer(x, y, "Lighting", objLightSource);
with (ls) light_owner = other.id;

If no owner is set, it will default to objLightSys (which is my lighting system controller object) as a fallback.
This is a bit tricky because an object_index is different from an instance id. But if you account for that, it works quite well.

For example, if I want the light source to be destroyed when its owner no longer exists, I do this in the step event:
GML:
if (!instance_exists(light_owner))
    instance_destroy();
And since this method basically establishes a link between two unique instances, you can do whatever you want with that information, exchange other variables, etc.
 
Top