• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[SOLVED] Appending a path from resource tree onto dynamic path

Bentley

Member
Does anyone know how to add paths together and have them be relative to the existing path? In other words, if your path is at 0, you add a path that is 16 to the right, and you then add a path that is 16 to the right, I want my dynamic path to be from 0 to 32.

Edit: I think what's happening is I'm appending a path from 0 to 16 onto a path that is from 0 to 16, so the same points are added. Any ideas how to append a that resource tree path onto my dynamic path more than once (it seems to only work the first time).

What I have:
[Create]
Code:
path = path_add();
I want to append a path to that path:
[Step]
Code:
if (keyboard_check_pressed(ord("D")))
{
    path_append(path, path_from_resourcetree);
}
This works one time as shown by draw_path: the path shows up 16 pixels to the right of my player when I press D. I want to be able to press D again and append that same resource tree path onto "path".

The manual does say:
This will not remove the path being appended from the resources. It is still there, only it now has no points in it and if you no longer wish to use it you should remove it with the function path_delete.
I'm pretty sure that does NOT include resource tree paths, only dynamic paths.

But jsut in case, I tried some other funky stuff but I couldn't get it to work. For ex:
Code:
if (keyboard_check_pressed(ord("D")))
{
    var path_to_append = path_duplicate(path_from_resourcetree);
    path_append(path, path_to_append);
    path_delete(path_to_append);
}
That doesn't work either.

Thanks for reading. Let me know if I can clarify anything.
 
Last edited:

CloseRange

Member
I mean I didn't even know path append was a thing. It's more work but I'd say just iterate over every point of the path and append it manually.
Just use a for loop and path_add_point
along with path_get_number
path_get_point_x
path_get_point_y
and path_get_speed
That's all path_append is doing after all (just bundled into one function) it would take you about 4 lines of code, maybe? Depending how you chose to write it.
It would definitely be easier than how much time you probably spent with path_append.
Code:
for(var i=0; i<path_get_number(path_from_tree); i++) {
     var dx = path_get_point_x(path_from_tree, i);
     var dy = path_get_point_y(path_from_tree, i);
     var ds = path_get_point_speed(path_from_tree, i);
     path_add_point(path, dx, dy, ds);
}
ok it took me 6 lines but again you could (technically) have written it like this:
Code:
for(var i=0; i<path_get_number(path_from_tree); i++)
     path_add_point(path, path_get_point_x(path_from_tree, i), path_get_point_y(path_from_tree, i), path_get_point_speed(path_from_tree, i));
EDIT: i forgot to explain why your code didn't work:
only it now has no points in it
you append it once and the points get removed. Path is still there however it's empty. you append it again and you're just appending a blank path. (it includes all paths as far as I'm aware, I don't believe there is any difference in how paths are stored in memory)

As for the duplicate version you made, this is where i suspect something fishy because you said you want the paths to be offset by the other?
I feel like that second code should work but if you are copying the same path twice they will always appear in the same position. Path append does not do any form of offsetting whatsoever. if the first point is at 16 x 16. Any object following the path will just iterate over the same position twice.
That's where the code I showed you comes in handy because you can modify it to be how you mean it to be.

Just add whatever you want to dx and dy and that's your new offset for each path. Then the question is how do you mean to offset them?

This is how I assume you mean (with comments this time)
Code:
for(var i=0; i<path_get_number(path_from_tree); i++) { // loop through the whole path that is used to be added
     var offX = 0; var offY = 0; // if there is not path appended yet offsets should be 0
     var n = path_get_number(path); // get the number of points on the dynamic path
     if(n > 0) { // if there are already points on the dynamic path we should apply offsets
          offX = path_get_point_x(path, n-1); // get the x and y position of the last point on dynamic path
          offY = path_get_point_y(path, n-1);
     }
     var dx = path_get_point_x(path_from_tree, i) + offX; // the new point's position should be the last dynamic paths last position (offset) plus the new points position
     var dy = path_get_point_y(path_from_tree, i) + offY;
     var ds = path_get_point_speed(path_from_tree, i); // I don't think you mean to change speed in any way so just use the speed directly
     path_add_point(path, dx, dy, ds); // append this new point to the path
}
 
Last edited:

Bentley

Member
I mean I didn't even know path append was a thing. It's more work but I'd say just iterate over every point of the path and append it manually.
Just use a for loop and path_add_point
along with path_get_number
path_get_point_x
path_get_point_y
and path_get_speed
That's all path_append is doing after all (just bundled into one function) it would take you about 4 lines of code, maybe? Depending how you chose to write it.
It would definitely be easier than how much time you probably spent with path_append.
Code:
for(var i=0; i<path_get_number(path_from_tree); i++) {
     var dx = path_get_point_x(path_from_tree, i);
     var dy = path_get_point_y(path_from_tree, i);
     var ds = path_get_point_speed(path_from_tree, i);
     path_add_point(path, dx, dy, ds);
}
ok it took me 6 lines but again you could (technically) have written it like this:
Code:
for(var i=0; i<path_get_number(path_from_tree); i++)
     path_add_point(path, path_get_point_x(path_from_tree, i), path_get_point_y(path_from_tree, i), path_get_point_speed(path_from_tree, i));
EDIT: i forgot to explain why your code didn't work:

you append it once and the points get removed. Path is still there however it's empty. you append it again and you're just appending a blank path. (it includes all paths as far as I'm aware, I don't believe there is any difference in how paths are stored in memory)

As for the duplicate version you made, this is where i suspect something fishy because you said you want the paths to be offset by the other?
I feel like that second code should work but if you are copying the same path twice they will always appear in the same position. Path append does not do any form of offsetting whatsoever. if the first point is at 16 x 16. Any object following the path will just iterate over the same position twice.
That's where the code I showed you comes in handy because you can modify it to be how you mean it to be.

Just add whatever you want to dx and dy and that's your new offset for each path. Then the question is how do you mean to offset them?

This is how I assume you mean (with comments this time)
Code:
for(var i=0; i<path_get_number(path_from_tree); i++) { // loop through the whole path that is used to be added
     var offX = 0; var offY = 0; // if there is not path appended yet offsets should be 0
     var n = path_get_number(path); // get the number of points on the dynamic path
     if(n > 0) { // if there are already points on the dynamic path we should apply offsets
          offX = path_get_point_x(path, n-1); // get the x and y position of the last point on dynamic path
          offY = path_get_point_y(path, n-1);
     }
     var dx = path_get_point_x(path_from_tree, i) + offX; // the new point's position should be the last dynamic paths last position (offset) plus the new points position
     var dy = path_get_point_y(path_from_tree, i) + offY;
     var ds = path_get_point_speed(path_from_tree, i); // I don't think you mean to change speed in any way so just use the speed directly
     path_add_point(path, dx, dy, ds); // append this new point to the path
}
Thanks a lot for the reply!

t's more work but I'd say just iterate over every point of the path and append it manually.
That is what I'm now going to do.
for(var i=0; i<path_get_number(path_from_tree); i++) { var dx = path_get_point_x(path_from_tree, i); var dy = path_get_point_y(path_from_tree, i); var ds = path_get_point_speed(path_from_tree, i); path_add_point(path, dx, dy, ds); }
This is perfect. Easy way to manually append a path b/c as you explained...
EDIT: i forgot to explain why your code didn't work:
you append it once and the points get removed. Path is still there however it's empty. you append it again and you're just appending a blank path. (it includes all paths as far as I'm aware, I don't believe there is any difference in how paths are stored in memory)

As for the duplicate version you made, this is where i suspect something fishy because you said you want the paths to be offset by the other?
I feel like that second code should work but if you are copying the same path twice they will always appear in the same position. Path append does not do any form of offsetting whatsoever. if the first point is at 16 x 16. Any object following the path will just iterate over the same position twice.
this is what was going wrong.
I appreciate the reply a lot. Thank you!
 
Top