Problem with gGUI.X not setting

Started by bx83, Sat 22/05/2021 10:55:11

Previous topic - Next topic

bx83

I've got the following code:

Code: ags

function repeatedly_execute_always()
{  

...

  int gFloatingText_X=0;
  int gFloatingText_Y=0;
  
  // Position the floating text
  if (ConversationIsOn) {
    gFloatingText.X = 0;
    gFloatingText.Y = 120;
  } else {
    if (mouse.Mode==eModeInteract) {
      if ((gFloatingText.X+gFloatingText.Width)>SCREEN_WIDTH) {
        Display("changing x for interact...");
        gFloatingText_X = mouse.x - 64 - 5 - gFloatingText.Width;   //past right-hand edge
        Display("changed x for interact, it is now %d",gFloatingText_X);
        gFloatingText_Y = mouse.y-30;      
      } else {
        gFloatingText_X = mouse.x+17;   
        gFloatingText_Y = mouse.y-30;      
      }
    } else {
      if ((gFloatingText.X+gFloatingText.Width)>SCREEN_WIDTH) {       //<--THIS CODE BLOCK RUNS.....
        Display("changing x for any mode but interact...");
        gFloatingText_X = mouse.x - 64 - gFloatingText.Width;  //past right-hand edge
        Display("TEMP VAR changed x for any mode but interact, it is now %d",gFloatingText_X);
        Display("1 gfloatingtext x is %d",gFloatingText_X);
        gFloatingText_Y = mouse.y-18;
      } else {
        gFloatingText_X = mouse.x;
        gFloatingText_Y = mouse.y-18;
      }
    }
  }
  if ((gFloatingText.X+gFloatingText.Width)>SCREEN_WIDTH) Display("BEFORE ASSIGNMENT TO .X gfloatingtext x is %d from %d",gFloatingText_X,  gFloatingText.X);
  
  gFloatingText.X=gFloatingText_X;
  gFloatingText.Y=gFloatingText_Y;
  
  if ((gFloatingText.X+gFloatingText.Width)>SCREEN_WIDTH) Display("MIDDLE gfloatingtext x is %d from %d",gFloatingText_X, gFloatingText.X);

...


It's godawful, and part of positioning the gFloatingText window on the right or left side of mouse curspr depending on if it's off the right-edge of the screen; this window acts as vessel for some text that displays the Name/'Description' of what's at mouse.x,mouse.y
go here for more context and pictures: https://www.adventuregamestudio.co.uk/forums/index.php?topic=59107.0

The important point about this code is: the gFloatingText window doesn't have it's .X/.Y pos controlled by anything else; yet it keeps resetting, and seemingly setting things on the other side of assignment.

So:
I've searched the code; none of it has 'gFloatingText.X=' in it, except code which isn't run.
I've checked if it's some sort of scoping thing (ie. it will remember the value in an IF statement, but will lose it and go back to before once this branch ends), but it doesn't seem to be.

The Variables are:
gFloatingText_X is a temp int var, declared outside the major block of code, so to hold the new value until it's assigned to gFloatingText.X
gFloatingText.X, which is the top-left-corner 'x' coordinate of a GUI
SCREEN_WIDTH is a #define for the number 1366
ConversationIsOn which is always 'false' for this example

The example OUTPUT is:
changing x for any mode but interact...
TEMP VAR changed x for any mode but interact, it is now 1064.  <--- not yet, this is only for temp variable gFloatingText_X
1 gfloatingtext x is 1064.    <-- text has now flipped to left side of mouse, as it should, because there's no room left on the right
BEFORE ASSIGNMENT TO .X gfloatingtext x is 1064 from 1255   <-- temp variable is new 1064, the .X GUI coordinate is 1255
MIDDLE gfloating text is 1255 from 1255. <-- both .X and _X are now what .X was 1 line ago, even though it was on the left side of assignment....wtf

How it should go:
gFloatingText.X=5;
---> it's now equal to 5, for now and forever until it's assigned a new value.
Why is this happening?

Please feel free to throw any help at this, or choose from the following:
a) 'bx83, you've missed out a semi-colon/mistaken X for Y/have this line assigning the var incorrectly like *right there* on the screen! *rolls eyes*'
b) 'I recognise this from the SetFloatingText function https://www.adventuregamestudio.co.uk/forums/index.php?topic=59107.0, and it's all to do with a component I set up deep in the system to always have gFloatingText.X=mouse.x, or some clever binding you've never heard of'
c) 'I am fascinated by this bug and will add it to the engine 3.5.0.31' (unlikely)

I'm stumped.

Snarky

The problem here comes from the fact that this is running not once, but repeatedly.

The first time around, it positions the tooltip GUI as you expect. But then in the next loop, it checks whether the current GUI position (before repositioning) is outside the screen. Which it isn't, because you just flipped it to the other side of the cursor. So it doesn't run the block that assigns the alternate gFloatingText_X valueâ€"and it doesn't display the "BEFORE ASSIGNMENT" debug line. It just sets gFloatingText_X=mouse.x and assigns it to gFloatingText.X.

If you run this without all the debug lines, I would expect the tooltip to flicker between the two positions.

What you need to be doing is to each turn calculate where the new "default" position of the tooltip would be based on the mouse position. Then check that position against the screen edge, and position it either there or in the alternative position depending on the outcome.

bx83

Solved!
I just had to change

if (gFloatingText.X+gFloatingText.Width)>SCREEN_WIDTH) {
to
if (mouse.x+gFloatingText.Width)>SCREEN_WIDTH) {

to make it dynamic. Thank you so much :D

SMF spam blocked by CleanTalk