1

I would like to see if the floor the player is stepping on and the player are the same colour. This is my code so far

if place_meeting(x,y,Obj_Floor)
    if !other.colour = self.colour
        instance_destroy()

It is placed in the step event of the Player but it does nothing. What is the problem and how can I fix it?

lolol
  • 69
  • 11

3 Answers3

0

I have no personal experience with Game Maker Language but after a quick search it appears the syntax is very similar to C, C++ and Java.

The problem I see is that you are using the assignment operator = rather than the comparison operator ==.

According to the documentation you are using the deprecated comparison operator =. It is recommended that you only use = for assignments and use == for comparison instead. The opposite of == (equals) is != (not equal).

If you want to check if two colours are not the same your code would look like this:

if (other.colour != self.colour)
{
    // colours are not the same, do some stuff
}
Fibbs
  • 1,350
  • 1
  • 13
  • 23
0

First, as explained by Fibbles, you shouldn't write

 !a = b

because what you do with = is an assignation. You should use != instead, which is equivalent to

! (a == b).

So your code should look like this :

if ( place_meeting(x,y,Obj_Floor) )
    if ( other.colour != self.colour )
        instance_destroy();

Moreover, if you are in the "step" event, the "other" keyword is not necessarily pointing to your obj_floor. You should put this code in the collision event of the player, and remove the first line. So you should have :

In the Player "collision with obj_floor" event :

if ( other.colour != self.colour )
        instance_destroy();

Plus, I recommend putting a semicolon at the end of your line, even if gml allows the contrary.

  • I have edited my code to comply with your suggestions but there is still no change. I have double checked the variables and made them deliberately different but the player will not die. – lolol Jun 29 '16 at 14:58
  • I think that your "other" object may not be the obj_floor you want, because you are in the step event. Try putting your code in the collision event of player, so that "other" will designate the object you are colliding with. I will edit my answer. – An intern has no name Jun 30 '16 at 10:01
  • You're not right about `;`. GM has very flexible syntax, you may use `;` but also you may not use `;`. No difference. – Dmi7ry Jun 30 '16 at 12:59
0

First of all maybe it's y+1? cause the player might be just above the floor (if you are making a platformer usualy player doens't "touch" the solid object). Anyways try this code

if place_meeting(x, y+1, Obj_Floor)
   if colour == other.colour
   //Player same color with floor
   else
   //Player different color from the floor
AresProductions
  • 508
  • 1
  • 9
  • 23