• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2 Different DS_lists get same index

Petrik33

Member
So hello everybody, I am struggling with very strange problem in my code, my object has a variable Path, when I change the state (the step event script) of this object to my PathMovement it does this
GML:
path = ds_list_create
, but then when it executes this code in step event :
Code:
var ttX=ScreenTo_tX(mouse_x,mouse_y);
    var ttY=ScreenTo_tY(mouse_x,mouse_y);
    ttX=clamp(ttX,0,MAP_W);
    ttY=clamp(ttY,0,MAP_H);
    var _target=global.theMap[# ttX,ttY];
    
    if(!_target[TILE.COL] && _target!=currentTile)
    {
        var _path=path_add();//Don't confuse this variable with my path ds_list, this is just for pathmaking
        var _tilePath=ds_list_create();//Index of _tilePath = 0  and index of Path  = 0
        var _pathExists=false;
        
        var lastTile=currentTile;
        var lastIndex=ds_list_size(path)-1  //see, here the lastIndex=-1 as the path ds_list is empty so its size = 0
        if(!ds_list_empty(path)) lastTile=path[| lastIndex]
        
        _pathExists=mp_grid_path(global.path_grid,_path,lastTile[TILE.tX]*TILE_W,lastTile[TILE.tY]*TILE_W,_target[TILE.tX]*TILE_W,_target[TILE.tY]*TILE_W,true);
        
        for(var i=1;i<path_get_number(_path);i++)
        {
            var _tX=floor(path_get_point_x(_path,i)/TILE_W);
            var _tY=floor(path_get_point_y(_path,i)/TILE_W);
            var _tile=global.theMap[# _tX,_tY];
            ds_list_add(_tilePath,_tile);
        }
        path_delete(_path);
        var lastIndex=ds_list_size(path)-1
//And See now Here the var lastIndex = the size of _tilePath -1, because they are actually one ds_list,
 // but I haven't made any of them point the other one

//Rest of code...
And what happens: see the temporary variable _tilePath, it actually gets the index of 0, just like the varible path, so when I change the var _tilePath, the changes also effect path, I checked it using debug mode, see the variable lastIndex is actually the size
 

Nidoking

Member
_path is not a list. _path is a path. If you're using list functions on a thing that is not a list, your game will be chaos and I would be surprised if it does anything you want it to.
 

Petrik33

Member
_path is not a list. _path is a path. If you're using list functions on a thing that is not a list, your game will be chaos and I would be surprised if it does anything you want it to.
Nice help man, including the fact that I had it all working without any performance problems before I started changing my states system, so in fact nothing has changed relatively between working code and this one. The reason why I do that is that the game has isometric view, which is not very simple to include in GameMaker Studio
 

Petrik33

Member
_path is not a list. _path is a path. If you're using list functions on a thing that is not a list, your game will be chaos and I would be surprised if it does anything you want it to.
And I don't use any list functions with _path if you have not noticed it
 

Nidoking

Member
Ah, I see. There is a comment telling people not to confuse path with _path. This is a sensible thing to do and not at all a sign of potentially poor planning or variable name choice.

I now notice that you put "var lastIndex" twice in the same scope. Maybe you should not do that.
 

Petrik33

Member
Ah, I see. There is a comment telling people not to confuse path with _path. This is a sensible thing to do and not at all a sign of potentially poor planning or variable name choice.

I now notice that you put "var lastIndex" twice in the same scope. Maybe you should not do that.
Oh man seriously? I have noticed I am using it to prove the fact that my ds_list path is changing while I change other ds_list, I used debugger to see the value of this variable before and after changing other ds_list(var tilePath)
 

Nidoking

Member
But why are you declaring two copies of it?

And what's the actual original declaration and assignment of path the list? ds_list_create should never return an index that's in use for that data structure type.
 

Petrik33

Member
But why are you declaring two copies of it?

And what's the actual original declaration and assignment of path the list? ds_list_create should never return an index that's in use for that data structure type.
I know, that's why I am shocked, for now I fixed it by this:
GML:
var tile_path=ds_list_create();
tile_path=ds_list_create();//Some vey stupida and strange BUG fix
 

Nidoking

Member
Well, that's a brand new variable that you didn't have before, and you're going to leak memory and list handles that way. I'm still going to say the bug is between keyboard and chair and that you haven't posted a part where the actual problem is.
 
Top