How do I check if self.x has changed

Get help using Construct 2

Post » Tue May 02, 2017 12:45 am

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.
B
15
S
8
G
2
Posts: 68
Reputation: 2,465

Post » Tue May 02, 2017 1:26 am

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
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499

Post » Tue May 02, 2017 11:53 am

That seems to have sorted it, thanks @SoldjahBoy
B
15
S
8
G
2
Posts: 68
Reputation: 2,465

Post » Wed May 03, 2017 4:23 am

No worries bud :)

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499

Post » Thu May 04, 2017 1:34 am

@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.

Image
B
15
S
8
G
2
Posts: 68
Reputation: 2,465

Post » Thu May 04, 2017 4:01 am

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
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499

Post » Thu May 04, 2017 12:45 pm

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.
B
15
S
8
G
2
Posts: 68
Reputation: 2,465

Post » Thu May 04, 2017 2:02 pm

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
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499

Post » Thu May 04, 2017 3:25 pm

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.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950


Return to How do I....?

Who is online

Users browsing this forum: jezjones24 and 6 guests