Problems with "teleporting" system and mouse cursor not changing immediately

Started by Akril15, Wed 18/10/2017 08:35:54

Previous topic - Next topic

Akril15

I have a couple of small but annoying problems I've run into in AGS.

Problem #1:

I have some code in my game that's supposed to teleport the player character to whenever the player clicks while the space bar is pressed that's in on_key_press and looks like this:

Code: ags

if (mouse.IsButtonDown(eMouseLeft) && IsKeyPressed(eKeySpace)==1 && (mouse.Mode==eModeWalkto || mouse.Mode==eModeArrow)) {
   player.x=mouse.x;
   player.y=mouse.y;
   player.PlaceOnWalkableArea();
  }


This code worked fine for some time, but now, for some reason, it no longer does. I've compared the last saved version of my game where this code was working to the version where this problem first manifested itself, and I can't for the life of me figure out what I did to cause this. Everything in on_key_press seems almost exactly the same, and this problem is made even more difficult since my game is so far along by now that I don't know how I can narrow down what exactly is going on. This might be a lost cause, but does anyone have any idea what might be happening here?


Problem #2:

I have a system in place where the walk cursor changes to an arrow cursor whenever it's over an exit. An example (under a "Mouse moves over hotspot" section) would be something like:

Code: ags

  if (mouse.Mode==eModeArrow) mouse.ChangeModeGraphic(eModeArrow, 1280); //up arrow

  if (mouse.Mode==eModeWalkto) {
    mouse.SaveCursorUntilItLeaves();
    mouse.Mode=eModeArrow;
                               }


The only problem with this system is that if the cursor is set to be a certain graphic, then moves over a region/hotspot/whatever associated with a different graphic, sometimes the cursor doesn't change immediately. It may stay as the previous cursor for a split second or, in some instances, briefly remain stuck as the previous cursor (I've seen this happen when I have the cursor over a hotspot that makes the cursor change to the "right arrow" graphic, and if I leave using that exit, the mouse winds up over a hotspot set to use the "up arrow" graphic, yet the cursor initially appears as the "right arrow" graphic until the mouse is moved off that hotspot, then onto it again). I tried putting mouse.Update(); in repeatedly_execute always, but it didn't help.

This isn't a huge issue, but it is slightly annoying, and if there is a relatively simple fix, I'm all ears.

Thanks in advance for any help you might be able to give!

EDIT: Fixed indents.

Snarky

Could I possibly implore you to fix your indenting? Seeing how you've formatted these code snippets is almost physically painful. It's like someone who iS deMONSTraBly Aware of CAPital lEtTers, bUT WhO JUST scatters thEm rANDomLy aRound In tHe SeNTeNCe.

Quote from: Akril15 on Wed 18/10/2017 08:35:54
This code worked fine for some time, but now, for some reason, it no longer does.

In what way does it not work?

Anyway, I would usually debug a problem like this by setting a breakpoint in the on_key_press() function and seeing where it goes wrong.

Quote from: Akril15 on Wed 18/10/2017 08:35:54
I have a system in place where the walk cursor changes to an arrow cursor whenever it's over an exit. An example (under a "Mouse moves over hotspot" section) would be something like:

Wait, you code this individually for each hotspot? That is... not a good way.

I'm not sure what the exact cause of your problem is (it could be that the "mouse moves over hotspot" event doesn't trigger immediately â€" I've never used it), and the way your logic is organized looks a little odd to me (you test whether the mouse mode is eModeArrow before you potentially set it to eModeArrow, rather than after as I would expect).

But first I would at least factor this out as a separate function (something like setArrowCursorForHotspot(int direction)). Or even better, to avoid having to write a bunch of "mouse moves over hotspot" handlers at all, instead just put a test in repeatedly_execute_always() to check whether it's over a hotspot â€" you can use a custom property to define whether the hotspot uses an arrow and if so in what direction, and the whole thing will just happen automatically.

Akril15

Quote
In what way does it not work?
When I hold down space and click where I want the character to teleport, nothing happens. If I tell the game to simply teleport to the mouse cursor when I press space, it does work, but for some reason, it doesn't register a mouse click.

QuoteWait, you code this individually for each hotspot? That is... not a good way.
No, I don't. Normally I have various regions assigned different directions in the global script. This was just an exception where the problem was pretty obvious.

QuoteI'm not sure what the exact cause of your problem is (it could be that the "mouse moves over hotspot" event doesn't trigger immediately â€" I've never used it), and the way your logic is organized looks a little odd to me (you test whether the mouse mode is eModeArrow before you potentially set it to eModeArrow, rather than after as I would expect).
Just out of curiosity, I tried moving the "if (mouse.Mode==eModeArrow)" line after the "if (mouse.Mode==eModeWalkTo)" line, but the problem still remained.

QuoteBut first I would at least factor this out as a separate function (something like setArrowCursorForHotspot(int direction)). Or even better, to avoid having to write a bunch of "mouse moves over hotspot" handlers at all, instead just put a test in repeatedly_execute_always() to check whether it's over a hotspot â€" you can use a custom property to define whether the hotspot uses an arrow and if so in what direction, and the whole thing will just happen automatically.
That sounds like it might work. I'll look into that when I have the time.

Khris

Regarding the teleporting, that code should be in on_mouse_click, not on_key_press.

Code: ags
    // inside eMouseLeft block
    if (IsKeyPressed(eKeySpace) && (mouse.Mode == eModeWalkto || mouse.Mode == eModeArrow)) 
    {
      player.x = mouse.x + GetViewportX(); // convert screen coords to room coords for scrolling rooms
      player.y = mouse.y + GetViewportY();
      player.PlaceOnWalkableArea();
    }

Akril15

I'm afraid your solution didn't work for me, Khris. My character still doesn't move when I hold space and click where I want her to go.

Khris

Just do some basic debugging. Insert Display() calls, put variables on labels, etc. Find out where and why the code fails.

Akril15

Khris, I think I finally figured out what the problem with that code is. I'm working on this game on a laptop, and when I copied it over to my desktop computer and ran the game form there, the code worked. I'm not sure if this is just an issue with my laptop or not, but if there's a chance that this code won't work on other people's laptops unless the player uses a mouse instead of the track pad, I may have to scrap it altogether.

Snarky, I tried moving the MouseMove code to repeatedly_execute, and that seems to have solved the problem for the handful of hotspots that use it.

Thank you both for your help.

SMF spam blocked by CleanTalk