Jump to content


Photo

Problem With Code


  • Please log in to reply
6 replies to this topic

#1 Bobacupcake

Bobacupcake

    GMC Member

  • New Member
  • 342 posts

Posted 30 May 2010 - 06:27 PM

I am making a 2-player game, and currently have two separate bullet objects. Each one has simmilar code, except they have actions that make them their creators bullet (For example, one has collision with obj_pone and the other with obj_ptwo) However there is alot of code in each object, and I wish to combine the two.

When the player creates it, I said:

with instance_create(obj_bulletp)
{
if other.b=1
{
playerId=obj_pone.id;
otherId=obj_ptwo.id;
}
if other.b=2
{
playerId=obj_pone.id;
otherId=obj_pone.id;
}}

Then, in the create event of the bullet object:

type=playerId.type;
type2=otherId.type;

I then did a switch statement for type in the create event, after the above code.

This doesn't work, however. I ran a debug mode, and the bullets were becoming the correct type. However the switch statement always returned 0, causing all of the bullets to do the default event.

Any help?
  • 0

#2 ragarnak

ragarnak

    GMC Member

  • Retired Staff
  • 19468 posts
  • Version:GM8

Posted 30 May 2010 - 06:53 PM

I then did a switch statement for type in the create event, after the above code.

Nope, the "instance_create(...)" command causes the newly-formed and placed instances create-event to be executed before the "if other.b=1" just below it.

In other words : at the time the "playerid" variable in the create-event of the newly-formed instance is checked it has not yet been filled with th apropriate data.

Hope that clarifies it.
  • 0

#3 Bobacupcake

Bobacupcake

    GMC Member

  • New Member
  • 342 posts

Posted 30 May 2010 - 09:19 PM

Thanks. Any idea how to go about fixing it?
  • 0

#4 ragarnak

ragarnak

    GMC Member

  • Retired Staff
  • 19468 posts
  • Version:GM8

Posted 31 May 2010 - 09:07 AM

Thanks. Any idea how to go about fixing it?

Multiple ways. One of them is to put the initialisation-code in a user-event, and call that after you've set the variables right :
with instance_create(obj_bulletp) {

  //set the variables

  event_user(0)

}
(put origional create-event code into Event -> "other" -> "user defined" -> "user 0")
  • 0

#5 NilsAnders

NilsAnders

    GMC Member

  • GMC Member
  • 525 posts
  • Version:GM8.1

Posted 31 May 2010 - 11:37 AM

why use 2 objects? Make one object, ObjBullet
give it its creator when you make it like so:
mybullet = instance_create(x, y, Objbullet);
myBullet.direction = direction;
myBullet.creator = id;
myBullet.player = player;

Now the ObjBullet knows wich player it is, what creator it has and in his collision with:


ObjPlayer (preffered method: Hope you used 1 object for both players, but I doubt that seriously)
if(other.id != creator)
{
	//other refers to the player being hit
	other.hp -= damage;
	instance_destroy();
}
ObjPlayerParent: (Second method: hope you used a parent for your players when using multiple objects for player 1 and 2)
if(player != other.player)
{
	//other refers to the player being hit
	other.hp -= damage;
	instance_destroy();
}
ObjPlayer1: (Worst method: You used seperate objects for your players, thus resulting in more code to type >.< )
ObjPlayer2: use the same code as ObjPlayerParent..but twice, in both players collision event..

Edited by NilsAnders, 31 May 2010 - 11:38 AM.

  • 0

#6 Bobacupcake

Bobacupcake

    GMC Member

  • New Member
  • 342 posts

Posted 31 May 2010 - 06:16 PM

Thanks ragarnak, that worked :)
  • 0

#7 ragarnak

ragarnak

    GMC Member

  • Retired Staff
  • 19468 posts
  • Version:GM8

Posted 31 May 2010 - 10:22 PM

Thanks ragarnak, that worked :unsure:

I'm glad it did work, and you're welcome. :P
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users