GML Coordinates not storing values correctly (or wholly)

Discussion in 'Programming' started by Dawn, May 23, 2019.

  1. Dawn

    Dawn Member

    Joined:
    Sep 5, 2016
    Posts:
    58
    I wrote a script for alarm events that an object changes coordinates over time to the destination.

    I set the length, the direction and the duration (steps) and the alarm will move the object's coordinates by [length/duration] for [duration] steps. The code below is not an actual code and excludes the repeating part.

    Code:
    //STEP
    var len=total_length/n;
    var len_x=lengthdir_x(len,dir);
    var len_y=lengthdir_y(len,dir);
    x+=len_x;
    y+=len_y
    //run this n times
    
    The sum of (length/duration)*duration and the length should match... and they don't.
    So, I set additional variables for storing coordinates then copied the values to coordinates then it fixed the problem.

    Code:
    //STEP
    var len=total_length/n;
    var len_x=lengthdir_x(len,dir);
    var len_y=lengthdir_y(len,dir);
    ACTUAL_X+=len_x;
    ACTUAL_Y+=len_y;
    x=ACTUAL_X;
    y=ACTUAL_Y;
    //run this n times
    
    I got a feeling that this is actually intended for maybe the optimization issue, but I'm not sure. Honestly I think this is pretty dumb.
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,244
    Can we see your code for total length? How much off are the results?
     
  3. Dawn

    Dawn Member

    Joined:
    Sep 5, 2016
    Posts:
    58
    Total length is just set from other script. There's nothing unusual about that.
    I found out something weird about coordinates and normal variables.

    This is the alarm script that you don't really need to read.
    Code:
    if slide_goal>0 && slide_time<slide_goal {
        slide_time++;
     
        var len;
        switch slide_cur{
        case 0:
            len=slide_len/slide_goal;
        break;
        case 1:
            len=slide_len*(sin(.5*pi*((slide_time)/slide_goal))
                -sin(.5*pi*((slide_time-1)/slide_goal)));
        break;
        case 2:
            len=slide_len*(sin(.5*pi*(-1+(slide_time)/slide_goal))
                -sin(.5*pi*(-1+(slide_time-1)/slide_goal)));
        break;
        }
     
        var len_x=lengthdir_x(len,slide_dir);
        var len_y=lengthdir_y(len,slide_dir);
     
        X_TEMP+=len_x;
        Y_TEMP+=len_y;
        x=X_TEMP;
        y=Y_TEMP;
    
        draw_x=x;
        draw_y=y;
    }
    
    if slide_time<slide_goal {
        alarm[slide_alarm]=1;
    }
    else {
        slide_len=0;
        slide_dir=0;
        slide_cur=0;
     
        slide_goal=-1;
        slide_time=-1;
    }
    Let's say the object's x is moving from 250 to 190 over 45 steps. (1.3333... per step)

    This is the "x" values from directly adding value to the coordinates.
    Of course this is not right.

    Then here's the value of "x" from X_TEMP (x=X_TEMP).
    This worked perfectly.

    Now then, the weird part.
    The upper values are "x" that copied X_TEMP (x=X_TEMP) and the lower values are "X_TEMP", the original. This is kinda confusing.
     
    Last edited: May 23, 2019

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice