Game Mechanics How to handle pick-ups with multiple players?

T

TTJ

Guest
Hi,

I was wondering how you guys are handling item pickups since i believe my method could use improvement.

Usually i create multiple player variables inside the item. For example item Shield has player1= false and player2 = false. Then in the collisionevent with the parent of the players it checks which player it is and turns the false to true. And so when player1=true, the shield follows player1.

But there has to be a better way. I use the same method tracking which bullet belongs to whom so the player doens't die by its own bullet. (bullet = instance_create, bullet.player1 = true, etc)

Greetings!
 

Yal

šŸ§ *penguin noises*
GMC Elder
You could have a single 'user' variable and set it to the id of the player that has it, or noone if nobody is using it.
 

Phil Strahl

Member
@Yal laid it out best. If there can be only one person performing the pickup, then it's much safer to have also just one "slot" (= variable) in your instance to indicate this, so it's never possible to accidentally have player1 = true and player2 = true.

For a shmup where I re-used bullets for both player and enemies, I added an "origin_type" variable to it and referred to an enum, like this:
Code:
// enum somewhere in the initialization code of my game
enum TYPE
{
  player, enemy, item
}
Code:
// for example, this is when an enemy shoots a bullet:

var new_bullet = instance_create(x,y,obj_bullet)
new_bullet.origin_type = TYPE.enemy

So when the bullet collided with either player or another enemy, the game would know whether to ignore the collision or to destroy the instance and who to award points to, e.g. this would be the collision check in the player-instance:
Code:
// if collision with bullet, the "other" being the bullet instance:
switch other.origin_type
{
  case TYPE.enemy:
  {
    // player loses health / dies
  } break;
  case type: // that would be the "type" variable of the instance, TYPE.player for the player, TYPE.enemy for the enemy, etc.
  {
   // ignore collision
  }
}
 
Last edited:
Top