Character not moving where it should [SOLVED]

Started by Gepard, Tue 13/12/2016 20:49:47

Previous topic - Next topic

Gepard

Hello, I have s simple problem with character movement. I have this code in a function:

Code: ags
Character *WhichOne = Character.GetAtScreenXY (mouse.x, mouse.y);
if (((player.x < WhichOne.x)||(player.x > WhichOne.x))||((player.y < WhichOne.y)||(player.y > WhichOne.y))) {
    if (TravelTo.IndexOf("%s" Destination)>=0) {
      player.Move (WhichOne.x, WhichOne.y, eBlock, eWalkableAreas);
      TravelTo = String.Format ("%s", WhichOne.GetTextProperty("TravelChoice"));
    }
    else Display ("This location is too far");
  }
  if ((player.x == WhichOne.x)&&(player.y == WhichOne.y)) {


This second part of the code that should run when players x and y is the same as WhichOnes doesnt run. It was running fine when set to eAnywhere before. But when I decided to move the character only on walkable areas, what happens now is that the character moves to the WhichOne x and y, but not exactly to its x and y. It is always a bit off (255, 111 vs 262, 106). And yes, there is enough walkable area around. Interestingly enough, one location works fine, but three more just do not. Any suggestions?

Thank you!
Drink up me 'arties! Yo ho!

Snarky

This could be one of two issues, one simple and one difficult:

-If both characters are Solid, they can't occupy the same coordinates. Adjust in editor or script. Easy.
-When you follow walkable areas, the pathfinder only checks every third pixel, which can apparently lead to some inaccuracies â€" though this hasn't been definitely confirmed AFAIK. (If so, this is arguably an AGS bug.) Check out this thread: http://www.adventuregamestudio.co.uk/forums/index.php?topic=54260.0

In the latter case, if you need pixel-perfect accuracy you probably need to manually nudge the character to the right coordinates when they stop moving. Scripting this could be a little bit of a pain.

Khris

Just wanted to point out something regarding your coordinate checks:
Code: ags
if (((player.x < WhichOne.x)||(player.x > WhichOne.x))||((player.y < WhichOne.y)||(player.y > WhichOne.y))) {

is the same as
Code: ags
if (player.x != WhichOne.x || player.y != WhichOne.y) {

To cover the opposite case, all you need is
Code: ags
else {

Gepard

#3
Thank you both very much. Btw. I have solved it in a different way. I made the characters solid and instead of checking if they are on the same X and Y, I put the IsCollidingWith function. Thank you for your help anyway.
Drink up me 'arties! Yo ho!

Gepard

OK, just when I thought it was going to work fine, the game just went haywire. I mean, one moment, you can travel to a certain place and the next you just cannot. It is like the game decides itself it will run a certain part of the code or not.

Code: ags
function OpenPlace () {
Character *WhichOne = Character.GetAtScreenXY (mouse.x, mouse.y);
  if ((player.x != WhichOne.x || player.y != WhichOne.y)) {
    if (TravelTo.IndexOf("%s" Destination)>=0) {
      TravelTo = String.Format ("%s", WhichOne.GetTextProperty("TravelChoice"));
      player.Move (WhichOne.x, WhichOne.y, eBlock, eWalkableAreas);
    }
    else Display ("This location is too far");
  }
  if (player.IsCollidingWithChar(WhichOne)==1) {
    // rest of the code is here
    }
  }
}


Sometimes the whole code runs perfectly, sometimes, the game decides to skip the part when the character moves, the one starting with "if (TravelTo.IndexOf..." I have no idea, what is going on... :f (

And btw. each character that I click on has a TravelTo Text Property. For example: "prison oldchapel" and when I click on some character on a map, it sets the Destination String to its name. For example I am in Port and the port has Text Property "TravelTo" set to "prison oldchapel". When I click the Old Chapel character, it sets the Destination to oldchapel, than the code should check, whether TravelTo of Port has the "oldchapel" in it (WHICH IT DOES!!!) and if it does, moves the character towards the character that I clicked. But the code just refuses to run. Why?
Drink up me 'arties! Yo ho!

Gepard

This is a total mystery to me now. There are total of four places in the game. When I make the character solid, it the code works with only one of them. When I make it not solid, it works with the remaining three, but not the one. I have also noticed that the code runs OK but for some reason it decides not to execute the line that moves the character. I even put Display message before and after it. Both work, but not the line that should move the player.
Drink up me 'arties! Yo ho!

Cassiebsg

Just for testing: try changing eWalkableAreas to eAnywhere....
There are those who believe that life here began out there...

Gepard

I already tried that. Works fine that way. I have no idea what is going on with the engine.
Drink up me 'arties! Yo ho!

Cassiebsg

Well, if it works fine with eAnywhere, then there's like 100% (less? more?) chance that your character can't move because he's probably in a non-walkable area.

Try adding the following line before your move:
Code: ags

player.PlaceOnWalkableArea();
There are those who believe that life here began out there...

Khris

You said you have "solved" it by using IsCollidingWithChar but please humor us and turn the characters not Solid, then go back to the coordinate check. ;)

Gepard

Doesn't work. It must be a bug of some sort. I tried everything. Putting all kinds of commands before and after it. All execute no problem. This one: "player.Move (WhichOne.x, WhichOne.y, eBlock, eWalkableAreas);" just gets ignored. Even if you put two of those. Nothing happens. The Move command just gets ignored. This is so frustraging :undecided:
Drink up me 'arties! Yo ho!

Gepard

Khris, strange thing is that whether the characters are solid or not, it seems to have no impact on the result. It always gets the same result. 3 places work just fine. The fourth doesnt. Anyway, did what you asked, and got the same result. My problem now is that with that fourth place, the Move command gets ignored.
Drink up me 'arties! Yo ho!

Khris

Have you tried moving the target character elsewhere? What happens if you replace the character with a freshly created one?

Gepard

Yes, the target character itself can move just fine. I found out another thing though. The character that is causing the problem is placed almost at the top of the screen (Y: 45), but if I move it a bit more to the south, it seems to work just fine. And it is not just that character. If I replace it with another one, that is "up north", than it will cause the same trouble. This is so weird. I mean, how or why does the game decides to just skip one line of command? Can you at least tell me if this is a bug or not?

Edit: Yes, the editor or the game must be broken. I have just changed a location of one character on a map and even the code that worked before is now refusing to run. Like getting a character text property. Did this happaned to anyone before? What can I do to save my game? A lot of work went into it. I'd hate to lose it :(
Drink up me 'arties! Yo ho!

Cassiebsg

Code doesn't "refuse to run" as far as I know. I'm guessing if it's not _visually_ running is because of something else. Do have a walkable area at the top of the screen?

How did you change the location of the "character on a map"? Is it another room? Or did you just move it elsewhere in the same room?
If it's a new room, where is your code located? In the room script or in global script?

You might want to show a couple screen grabs or video to show the problem.
There are those who believe that life here began out there...

Gepard

Drink up me 'arties! Yo ho!

Snarky

Without intending any offense, I am pretty sure this is caused by some bug in your code (or mistake in some project setting), since your approach seems unnecessarily convoluted for the task at hand. For example, I have no idea why you're using string matching and text properties to define where characters are supposed to move. I don't understand why you've defined the locations as characters. And unless the map is generated on the fly, I don't see why you need to check character collisions or coordinates instead of using e.g. regions to determine when a character reaches its destination. You're also apparently doing some string logic on which locations are accessible, which isn't shown here.

Each of these steps is a potential source of bugs. We'd need to see more of your code to find the error.

Gepard

I apologize, I know I can sound a bit chaotic, and I always keep in mind, that when there is something wrong, it is not the AGS, but me who made the mistake. OK, so let me clarify. I use characters as locations, because I want a lot of locations and in AGS there is unlimited number of characters but not hotspots, objects or something else I could use. Also I have each character's properties defined neatly in the Custom properties section. One of those properties is a text property TravelChoice, lets say character A has a TravelChoice "B C". This is to limit player character to a certain location he can travel to. When he clicks the B location, the string Destination is set to B. Than TravelChoice of current char (current place) A is compared to the Destination. If TravelChoice has the destination in it, than player can travel there. As I've mentioned earlier, I have a lot of locations planned, and they will all be in one room, so I don't think there would be enough regions. I really appreciate your help guys, I'm just a bit frustrated because of how much work I've already put into the game.

So, I've solved the issue with getting the character's text property, now just the previous one remains. I will try to post another image explaining it.
Drink up me 'arties! Yo ho!

Gepard



A, B, C, D - places (characters) - not solid
player character is behind place A and is the only one moving around - solid
What I've noticed so far in this placement. Player starts at A, can travel to B and C no problem and back as well. Can travel to D, but only from A. If he tries to go from B or C to D, code skips the move player command. Also skips move player when travelling from D to B or C. When I move D to Y:80 like so:



A, B, C, D are not solid, the character can be solid or not (doesn't seem to matter), everything works fine, but I cannot have D on the map where I want it.

This is the walkable area:

Drink up me 'arties! Yo ho!

Cassiebsg

Try making your walkable area a bit wider. I seem to remember AGS having problems if areas are 3 or less pixels.
There are those who believe that life here began out there...

Snarky

OK. That's not how I would code it personally (in particular, I would not use a custom string property to define game logic, since there's no way to catch typos), but you have your reasons, anyway...

I should warn you that there is a limit to how many characters can be in a room at the same time (or rather, in D3D there's a limit to how many sprites you can initialize, which limits the number of characters to somewhere around 70, depending on other graphical elements).

If everything works OK when you use eAnywhere, Cassie is probably right that you just need to make your walkable areas wider.

Gepard

Bloody hell. That did it! At least for now it seems like it works. Thank you Cassiebsg and everyone else. Sorry for being such a drama queen. I'll be more calm and give more detail in my next topics if there are any. Thanks for the varning Snarky. Will have to bear that in mind.
Drink up me 'arties! Yo ho!

Snarky

Nice! I did notice when searching the forums that you had a very similar problem back in 2007... :P

SMF spam blocked by CleanTalk