• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Problem with some code

N

Noba

Guest
So, no idea why this isn't working, it was working before and I'm not sure what has changed.

When you move left, the code checks if another box is right next to the object, and if not then it won't move, same for right, and so on.

Code:
if(left && !place_meeting(x-300,y,oExit or oBoxRightUp or oBoxRightDown or oBoxLeftUp or oBoxLeftDown or oUp or oDown or oLeft or oRight or o4ways))
{
    
    x -= 5;
    
}

if(right && !place_meeting(x+300,y,oExit or oBoxRightUp or oBoxRightDown or oBoxLeftUp or oBoxLeftDown or oUp or oDown or oLeft or oRight or o4ways))
{
    
    x += 5;
    
}
The object just moves right, completely ignoring the box, and won't move left at all.
 

Yal

🐧 *penguin noises*
GMC Elder
So, no idea why this isn't working, it was working before and I'm not sure what has changed.

When you move left, the code checks if another box is right next to the object, and if not then it won't move, same for right, and so on.

Code:
if(left && !place_meeting(x-300,y,oExit or oBoxRightUp or oBoxRightDown or oBoxLeftUp or oBoxLeftDown or oUp or oDown or oLeft or oRight or o4ways))
{
  
    x -= 5;
  
}

if(right && !place_meeting(x+300,y,oExit or oBoxRightUp or oBoxRightDown or oBoxLeftUp or oBoxLeftDown or oUp or oDown or oLeft or oRight or o4ways))
{
  
    x += 5;
  
}
The object just moves right, completely ignoring the box, and won't move left at all.
This never worked, or is a binary maths operator and does not work at all like the word in the english language. What you want is something like this:
if(right and not (place_meeting(x+300,y,oExit) or !place_meeting(x+300,y,oSomeOtherCrap) or !place_meeting(x+300,y,oEvenMoreDifferentObjects) or !place_meeting(x+300,y,oResearchParentsPlease) or !place_meeting(x+300,y,oItsForYourOwnBest))){
x += 5;

}
 

Hyomoto

Member
Are you sure? Read your code. It's doing exactly what you asked. But, you have two problems so let's solve the first one:
Code:
oExit or oBoxRightUp or oBoxRightDown or oBoxLeftUp or oBoxLeftDown or oUp or oDown or oLeft or oRight or o4ways
That isn't how that works. This evaluates to true, which is why a collision isn't found. To simplify, you are asking the computer to find a collision with object id 1. You'll need to read up about parenting to fix this.

As for the second thing your box does move left, and then is immediately moved right. Because both blocks of code run.
 
N

Noba

Guest
This never worked, or is a binary maths operator and does not work at all like the word in the english language. What you want is something like this:
if(right and not (place_meeting(x+300,y,oExit) or !place_meeting(x+300,y,oSomeOtherCrap) or !place_meeting(x+300,y,oEvenMoreDifferentObjects) or !place_meeting(x+300,y,oResearchParentsPlease) or !place_meeting(x+300,y,oItsForYourOwnBest))){
x += 5;

}
I don't really know how parents would work in this, and I already knew what they were anyway. All those different objects are for different blocks, and possible entrances and exits to those blocks.

Other than that, I'll try that and see if it works. Thanks.
 
N

Noba

Guest
Are you sure? Read your code. It's doing exactly what you asked. But, you have two problems so let's solve the first one:
Code:
oExit or oBoxRightUp or oBoxRightDown or oBoxLeftUp or oBoxLeftDown or oUp or oDown or oLeft or oRight or o4ways
That isn't how that works. This evaluates to true, which is why a collision isn't found. To simplify, you are asking the computer to find a collision with object id 1. You'll need to read up about parenting to fix this.

As for the second thing your box does move left, and then is immediately moved right. Because both blocks of code run.
Ahhh okay, I thought it was for all those possible objects that it could check place_meeting for.
I'm not sure why I've had so many errors in this, probably because I'm tired. ;)
 
N

Noba

Guest
I did actually google parents again, and I never knew that code which involved parents was also ran for the child aswell, which is pretty cool. That'll definitely save me a lot of time. Thanks!
 

Yal

🐧 *penguin noises*
GMC Elder
Yeah, that's exactly what parents are used for - children count as special cases of the parents.
(this applies to two things - checking object type, which you're using in your case, and also for the object's own code)

Also, if the parent has an event but the child does not, the child will run the parent's code for that event. This lets you avoid having to replicate common code (e.g. dropping coins when an enemy dies) in hundreds of different objects. If both the child and the parent has code, the child's code is run (so you can override parent stuff that doesn't apply). (If you want both, you can use event_inherited() to manually call the parent's code from the child's code.)
 
N

Noba

Guest
Yeah, that's exactly what parents are used for - children count as special cases of the parents.
(this applies to two things - checking object type, which you're using in your case, and also for the object's own code)

Also, if the parent has an event but the child does not, the child will run the parent's code for that event. This lets you avoid having to replicate common code (e.g. dropping coins when an enemy dies) in hundreds of different objects. If both the child and the parent has code, the child's code is run (so you can override parent stuff that doesn't apply). (If you want both, you can use event_inherited() to manually call the parent's code from the child's code.)
Yeah, I knew the basics about that, just not that if I detected a collision with the parent it would work for the child aswell.
 

Yal

🐧 *penguin noises*
GMC Elder
It's not just collisions, either... it works for all events, and also stuff like with(parent_thingamajig){, place_meeting(parent_thingamajig) etc.
 
Last edited:

Hyomoto

Member
Wait wait wait @Yal, are you sure about that?
Code:
if object_index == parent_object { // do }
I'll test this, but that should be a direct comparison and would only work if object_index is parent_object. I believe you have to use object_is_ancestor to check everything.
 

Yal

🐧 *penguin noises*
GMC Elder
Wait wait wait @Yal, are you sure about that?
Code:
if object_index == parent_object { // do }
I'll test this, but that should be a direct comparison and would only work if object_index is parent_object. I believe you have to use object_is_ancestor to check everything.
I was sure about it, but turns out it doesn't work like that when I tested it. I'll redact it in my post.
(with-loops, place_meeting etc definitely work on all the children as well, though)
 
Top