Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411613 Posts in 69390 Topics- by 58447 Members - Latest Member: sinsofsven

May 09, 2024, 07:47:22 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)How to load different sprites for various instances of the same object?
Pages: [1]
Print
Author Topic: How to load different sprites for various instances of the same object?  (Read 1968 times)
Ninomojo
Level 2
**



View Profile WWW
« on: November 24, 2009, 10:10:52 AM »

Hi there,

I'm a beginner with Game Maker. I'm trying to create a small game for 4 or more players at the same time. All players will look and behave the same, same animations, just their colors will change (P1 will be red, P2 blue, P3 green, etc.)

I would like to know what's the most elegant way to duplicate the "oPlayer" object, while swapping the sprites for others that have a different color, and swapping the inputs for different ones (because obviously the same keys aren't gonna work for several players).

Do I have to load all color variation of the player sprites and duplicate oPlayer into separate objects like oPlayer_2,.... oPlayer_8 ? Or is there an elegant way for me to just create ONE player object, and then changing its sprites and controls when I instantiate it?

Thanks!

Romain
Logged

Ninomojo
Level 2
**



View Profile WWW
« Reply #1 on: November 24, 2009, 10:56:35 AM »

To put it another way, if I were doing this in Blitz here's one of the ways I would try:

I would load player sprites into an array for P1, another for P2 etc. And then I would put those arrays into another array, and each instance of a player would poke their sprites from this higher array using their player ID number. Example:

player_sprites[player_ID].sprite[sprite_index_for_a_given_animation]
Logged

dbb
Level 4
****



View Profile WWW
« Reply #2 on: November 24, 2009, 11:17:58 AM »

You could assign different sprites to different instances of the same object, but you'll probably want to create separate objects for each player, as they'll be accepting different keyboard / gamepad input.

What I'd suggest is creating one parent object containing all the code and stuff that's common to all players, then have a number of child objects where you just specify the stuff specific to each player, such as what sprite to use and which keys do what. Look up "inheritance" in the GM documentation if you don't understand what I'm talking about.

I'm not an expert, though; if someone else tells you something totally different, you might be better going with what they say.
Logged

Matt Thorson
Level 7
**

c'est la vie


View Profile WWW
« Reply #3 on: November 24, 2009, 11:46:28 AM »

I'd recommend having one player object with a variable designating which player number it is.

For player input, just call scripts with the player number as an argument, such as:
Code:
if (check_jump( player_num ))
   jump();
Within that script you'd then handle all the jump checks for the various player numbers.

For the sprites, just assign the sprite to the player at the same time you assign its player number:
Code:
obj.player_num = 1;
obj.sprite_index = spr_player1;
OR draw different sprites in the draw event based on player number:
Code:
if (player_num == 1)
  draw_sprite( spr_player1, image_index, x, y );
else if ...
Logged

dbb
Level 4
****



View Profile WWW
« Reply #4 on: November 24, 2009, 12:52:14 PM »

Yeah. What he said.  Facepalm
Logged

Ninomojo
Level 2
**



View Profile WWW
« Reply #5 on: November 24, 2009, 02:44:16 PM »

I'd recommend having one player object with a variable designating which player number it is.

For player input, just call scripts with the player number as an argument, such as:
Code:
if (check_jump( player_num ))
   jump();
Within that script you'd then handle all the jump checks for the various player numbers.

For the sprites, just assign the sprite to the player at the same time you assign its player number:
Code:
obj.player_num = 1;
obj.sprite_index = spr_player1;
OR draw different sprites in the draw event based on player number:
Code:
if (player_num == 1)
  draw_sprite( spr_player1, image_index, x, y );
else if ...


Hey thanks, I didn't think of the Draw event based on player number!
But, how could I adapt that to multiple sprites per player? (you kno, one for running, one for jumping, etc.).

Is there a way to "load" sprites in arrays in Game Maker or something similar?
Logged

Noel Berry
Level 4
****


Yarr!


View Profile WWW
« Reply #6 on: November 24, 2009, 07:07:38 PM »

Yup. You can make a variable equal the id of a sprite.

for example:
Code:
running_spr = sprite_name;
walking_spr = sprite_name;
etc.
or, you can put it in an array:
Code:
sprite[0] = sprite_name;
sprite[1] = sprite_name;

and then, depending on which one you want, you set the sprite_index to it, ex:
Code:
sprite_index = running_spr;
or
sprite_index = sprite[0]

and then if you still wanted to draw it, you could just go ahead and draw the sprite_index:
Code:
draw_sprite(sprite_index,image_index,x,y);

EDIT:
Actually, thinking about it, you could do something like:
Code:
sprite[0,0] = sprP1_Running;
sprite[0,1] = sprP1_Jumping;

sprite[1,0] = sprP2_Running;
sprite[1,1] = sprP2_Jumping;

sprite[2,0] = sprP3_Running;
sprite[2,1] = sprP3_Jumping;

and then all you'd have to do to set the sprite is:
Code:
sprite_index = sprite[player_num,0];
(for the running one, in this case)
« Last Edit: November 24, 2009, 07:14:39 PM by Drazzke » Logged

Ninomojo
Level 2
**



View Profile WWW
« Reply #7 on: November 25, 2009, 07:08:19 AM »

Two dimensional arrays rule:)
Thanks a lot Drazzke!

So in fact sprite_index are kind of pointers? Anyway thank you so much, I'm gonna try that.
Logged

ChevyRay
Guest
« Reply #8 on: November 25, 2009, 07:14:41 AM »

They are actually integers. Everything is an integer in game maker.

show_message(string(object_index));
show_message(string(sprite_index));
show_message(string(path_index));
show_message(string(background_index));
etc.

Will all give you integers, which you can pass around to other variables, or use to reference those particular resources.
Logged
Hideous
That's cool.
Level 10
*****


3D models are the best


View Profile WWW
« Reply #9 on: November 25, 2009, 08:56:16 AM »

Actually, everything is a Double in game maker because game maker is stupid.
Logged

Ninomojo
Level 2
**



View Profile WWW
« Reply #10 on: November 25, 2009, 09:45:10 AM »

Very useful info, thank you all.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic