Windows [Solved] How do I find an instance I have not collided with?

GMS2 v 2.1.5.332

I am trying to make a feature where the player character can grab a solid object above them, and if it is moving the player will move with it.

my code never lets the player collide with a solid object, I have been trying to get the object using instance_position, but it never appears to find the object, the id returned ends up being -4 every time and the game crashes.
//getting the object
if roofGrabActive {
if (place_meeting(x,y-1,solid)){
isRoofWalking = true
roofGrabbed = instance_position(x,y-1,solid)​
}​
}

//change vertical position
if isRoofWalking {
if !place_meeting(x,y-1,solid) {
isRoofWalking = false
roofGrabbed = noone​
} else {
currentVSP = 0​
// this crashes the game due to the object being unknown, and therefore does not have a y value
y = instance_id[roofGrabbed].y+1​
}​
}

//change horizontal speed
if isRoofWalking{
// I expect this would crash it to if i could get to it.
currentHSP += roofGrabbed.currentHSP​
}​
Thus far i have tried using instance_nearest as well, although i fear that would get me some glitches in the long run, but I run into the same problem. This leads me to think I have a basic misunderstanding of what an 'id' is or something, and I haven't been able to figure out what i'm not understanding by looking through the manual.
 
Last edited:

MudbuG

Member
I have few observations and a suggestion that might help.
  1. Make sure you are using the correct object name in the place_meeting call. The first code block uses "place_meeting(x,y-1,solid)", then second uses "place_meeting(x,y-1,objSolid)".
  2. Are you expecting the roof to be only 1 pixel above the player? If not, you may need to increase the distance checked for in place_meeting. y-maxDistance.
  3. To stick with the room object when it is moving ... The first solution I can think of is:
    1. When player grabs the roof, store the difference between the player X and the room X in a player variable (offsetX = player.x - roof.x)
    2. each step, update players position as long the player is still hanging on to the roof: (player.x = roof.x + offsetX)
If you have trouble with place_meeting detecting the roof, consider using collision_line to detect if a roof is over the player. After you find the roof, you could use distance_to_object to determine if the roof is close enough to "grab" or not.
 
I have few observations and a suggestion that might help.
  1. Make sure you are using the correct object name in the place_meeting call. The first code block uses "place_meeting(x,y-1,solid)", then second uses "place_meeting(x,y-1,objSolid)".
  2. Are you expecting the roof to be only 1 pixel above the player? If not, you may need to increase the distance checked for in place_meeting. y-maxDistance.
  3. To stick with the room object when it is moving ... The first solution I can think of is:
    1. When player grabs the roof, store the difference between the player X and the room X in a player variable (offsetX = player.x - roof.x)
    2. each step, update players position as long the player is still hanging on to the roof: (player.x = roof.x + offsetX)
If you have trouble with place_meeting detecting the roof, consider using collision_line to detect if a roof is over the player. After you find the roof, you could use distance_to_object to determine if the roof is close enough to "grab" or not.
1. I apologize, I cut my code up and changed variable names to make it easier to read. I just fixed the ones i missed.
2. Yes the roof is meant to be one pixel above the player, I and i checked using debug messages to make sure the code block was actually executing
3. The main problem I am having it not that I can't think of a way to do it, but that I cant figure out how to access the instance above me without actually colliding with it, Ill research the collision_line and see if that fixes it

Thank you
 

TsukaYuriko

🌠
Forum Staff
Moderator
Could you please elaborate?
Essentially "you were using function X, then you tried using function Y, what you really need to use is function Z".

On the old GMC, there was a topic that explained the difference between the collision functions rather nicely.


That aside, solid is a reserved variable. You can't name objects like that and then refer to them in code without getting unexpected results.
(Unless you were trying to refer to all instances of objects that were marked as solid, which also doesn't work like that - you should be using parent objects in this case.)
 
Top