How do I check if self.x has changed

0 favourites
  • 9 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • I have an issue with my AI. I am using the platform behavouir and simulating the left/right with events. They do not like the collision of some of the objects they move on, whom have angled collision boxes.

    I have been making them jump to avoid them getting stuck, but I can not figure out how to find if self.x has changed to be able to make them jump to get unstuck.

    I know my system is bodged, but how can I check if the x of the AI has changed and if it has stayed the same for more than say 0.1 seconds, then I need it to jump so it becomes unstuck on the solid collision box. Unless there is a better way to stop my AI from getting stuck and staying in place.

    I can't check for stopped or is moving(inverted) because they are moving in the sense as the event is still moving them, but from my perspective they are static.

  • You can create an instance variable for your character/AI and call it something like, OldX and OldY.

    Every tick (record this value);

    Set OldX to Sprite.X

    Set OldY to Sprite.Y

    but BEFORE that event, in your list of events...

    Every tick (compare the value before it changes with event shown above)

    Compare value Sprite.X to OldX -> is not equal, do stuff

    Compare value Sprite.Y to OldY -> is not equal, do stuff

    So ultimately you'd have something like:

    Every tick

    --Compare value Sprite.X to OldX -> is not equal, do stuff

    --Compare value Sprite.Y to OldY -> is not equal, do stuff

    -----Set OldX to Sprite.X

    -----Set OldY to Sprite.Y

    Hopefully that will do the trick!

    ~Sol

  • That seems to have sorted it, thanks SoldjahBoy

  • No worries bud

    ~Sol

  • SoldjahBoy I've done more tests since I thought it had fixed the issue. I had set up a text event to check the current and old x coords. Only the old coords would show, when I tried showing self.x, nothing would show.

    I'm thinking it is not able to find the x of the sprite, being there are more than one and can not choose which one. Then I deleted all but one and tried again, but it would still not show the x coord in the text object I had set up with an every tick.

    I also tried = and "does not =", I had some results, does not = caused nothing, does = caused all the sprites to jump continuously.

    Though, when the sprite got stuck moving on a platform that had a slope, multiple points, it would stop and not get forced to jump.

  • I think it's because you're setting the OldX and OldY variables, AFTER they're being compared.

    The event sheets run in order from top to bottom, so you need to move your sub event that is setting the OldX and OldY to the event ABOVE your comparison. Currently it's checking the coordinates before it's set them, so it will be getting the current coordinates, not the old ones.

    ~Sol

  • I tried what you said SoldjahBoy, but it still either made the sprite jump continuously or not at all.

    From what I found, when the sprite gets stuck, its x does not change, so both old and new match, so nothing gets triggered. I did however add a small delay for the oldx so it was out of sync with the current x, which worked.

    However you can notice when they get stuck.

    I did a little work around which now basically make the sprites float above the platform by 0.9 pixels, now they never get stuck (I shifted the origin down 1 pixel so it does not look like it is floating). If I go above 0.9 then it starts to hop continuously.

    So I think what I have will work.

  • Ah yeah, makes sense... you can try adding "wait 0" which forces the next piece of code to execute next tick without forcing an actual timed delay.

    Glad you figured something out for it though

    ~Sol

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Setting the oldX/oldY should be done every tick.

    Now they only get set (and therefor change) when the conditions are met.

    Bring the sub in a root event.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)