Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411486 Posts in 69371 Topics- by 58427 Members - Latest Member: shelton786

April 24, 2024, 09:12:29 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Unity Crashes when deleting a list
Pages: [1] 2
Print
Author Topic: Unity Crashes when deleting a list  (Read 1847 times)
Vampade
Level 0
**



View Profile WWW
« on: July 14, 2016, 02:40:17 AM »

Hello all, I almost finished my pathfinding algorithm and now I am making the A* pathfinding recalculate a path because in my game the player will move a lot (2d top down game) and there will be a lot of obstacles that will be created on runtime. I made a simple system that works like this:

Code:
 -if the target has changed position (I check this by seeing if the closest node of the target has changed) and a path was found or the target s position has been reached so it will be if(changed_node && (found_path || reached_des))
 
 -Clear the open list, the list that contains the nodes of the path, the list of the adjacent nodes (adjacent to the nodes of the closed list and of the path,the ones that are analyzed) and the closed list.
 
 -Finally call the function FindPath that is the path finder

the problem is at the 2nd point, when I delete the closed_list unity crashes and it's not only a unity's problem because I built the game and I saw that also in game it crashed when deleting the closed list. I really have no idea why it happens. A possible motivation that came to my mind is that the class that handles the movement costs of the nodes uses the closed list to find the G cost. I tried to disable this script but it didn't work. Here's the part of code.

Code:
 // Update is called once per frame
     void Update()
     {
         //launches pathfinding actual_node.my_node is the start node
        //targ_node.my_node is the target's closest node
         FindPath(actual_node.my_node, targ_node.my_node);
 
         //if the path was created follow the path
         if (path_nodes.Count > 0 && current_node < path_nodes.Count)
             FollowPath();
 
         if (target.GetComponent<GetClosestNode>().changed_node && (reached_dest|| found_path))
         {
             found_path = false;
             reached_dest = false;
             path_nodes.Clear();
             open_list.Clear();
             adjacent_nodes.Clear();
             //crashes here
             closed_list.Clear();
             FindPath(actual_node.my_node, targ_node.my_node);
         }
     }
 

found_path is true when a path is found in FindPath(), reahed_dest is true when the npc finished following the path to the target. What should I change to make it recalculate the path correctly? Thank you in advance for your help guys! :D


« Last Edit: July 18, 2016, 07:24:12 AM by Vampade » Logged

quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« Reply #1 on: July 14, 2016, 05:24:04 AM »

Is there a Stack trace or error message?

Does adjacent nodes delete your current node when it's cleared? (Some bad recursion?)
Logged

Vampade
Level 0
**



View Profile WWW
« Reply #2 on: July 14, 2016, 06:43:56 AM »

Hi quantumpotato, when when it crashes it doesn't show me anything in the console. This part of code is executed when a path has been found and it stopped searching. I am sure now that it isn't a problem related with the script that manages the G cost because I made it check that the closed list isn't empty before processing it. I've just tried again but I deleted only the closed list instead of the others and it still crashes. So yeah, I think it's bad recursion, looks like something it's continuously called making unity go crazy Screamy Screamy but how can I determine where it is? because it crashes totally, I can't even scroll the hierarchy or the inspector
Logged

quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« Reply #3 on: July 14, 2016, 06:48:17 AM »

Hm - I would recommend extracting that part of your code into a new project that just does that 1 workflow path to make it crash. If you still have an issue, then it might be something with Unity then?

If you don't have the crash then that tells you there's more going on
Logged

Vampade
Level 0
**



View Profile WWW
« Reply #4 on: July 14, 2016, 07:50:23 AM »

In a new project I manually made a path from point A to B, filled path nodes list and closed list with the nodes and made a simple if that erased the lists. Everything worked fine. The problem is in the other voids I think
Logged

quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« Reply #5 on: July 14, 2016, 12:55:57 PM »

In a new project I manually made a path from point A to B, filled path nodes list and closed list with the nodes and made a simple if that erased the lists. Everything worked fine. The problem is in the other voids I think

Ok that's good to know! So now you can start isolating.. adding in function by function until you crash
Logged

Vampade
Level 0
**



View Profile WWW
« Reply #6 on: July 15, 2016, 11:01:56 AM »

Hi, thank you very much for your great input, it helped me to isolate the problem. Without you I would be in the total panic  Grin. So the problem is in the function that makes the enemy follow the path but it is pretty strange because in this method I never use the closed list, take a look:

Code:
//follows the path
    void FollowPath()
    {
        path_nodes.OrderBy(x => Vector2.Distance(path_nodes.First().transform.position, x.transform.position));
        dest = GetNodePos(current_node);
        node_dist = Vector2.Distance(transform.position, dest);
        //if we haven't reached the destination yet
        if (reached_dest == false)
        {
            //if the distance is greater than 0.1 move towards the node
            if (node_dist > 0.1f)
            {
                transform.position = Vector3.MoveTowards(transform.position, dest, en_vars.speed * Time.deltaTime);
                transform.rotation = UtilityFunctions.FaceTo(GetNodePos(current_node), transform.position);
            }
            else
            {
                current_node++;
            }
        }
        if (current_node >= path_nodes.Count)
        {
            reached_dest = true;
            current_node = 0;
        }
    }

    //gets the node position based on its index
    Vector3 GetNodePos(int n_node)
    {
        if (n_node < path_nodes.Count)
        {
            return path_nodes[n_node].transform.position;
        }
        else
        {
            return Vector3.zero;
        }
    }
as you can see I don't use the closed list, this leaves me a bit disappointed because it looks nonsense that this is the part that is making the game crash. What could it be guys?
Logged

readyplaygames
Level 2
**


View Profile WWW
« Reply #7 on: July 15, 2016, 11:16:34 AM »

Does it crash now? That's what's really important.
Logged
Vampade
Level 0
**



View Profile WWW
« Reply #8 on: July 15, 2016, 01:04:56 PM »

With the code as it is yes, it doesn't crash only if I don't run the function FollowPath() (I found that the problem is there) or I don't delete the closed list when recalculating the path but the AI then wouldn't work properly if I don't call that function or I don't delete the closed list, so what do you suggest?
Logged

valambrian
Level 0
**


View Profile
« Reply #9 on: July 15, 2016, 06:41:13 PM »

Can closed_list share elements with another list? I.e. does the code crash if you call closed_list.Clear(), but not adjacent_nodes.Clear()?
Logged
Vampade
Level 0
**



View Profile WWW
« Reply #10 on: July 16, 2016, 02:08:43 AM »

Yes it does, even if I delete only closed list
Logged

quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« Reply #11 on: July 17, 2016, 04:54:41 AM »

Yes it does, even if I delete only closed list

Hm... can you post code?
Logged

Vampade
Level 0
**



View Profile WWW
« Reply #12 on: July 17, 2016, 01:53:14 PM »

Of course, here's the code http://hastebin.com/amaqefepuc.cs I commented everything so you guys can understand more easily. If you need any clarifications about the code don't hesitate to ask me. Thank you very much for your help  Grin
Logged

quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« Reply #13 on: July 19, 2016, 06:22:15 AM »

Of course, here's the code http://hastebin.com/amaqefepuc.cs I commented everything so you guys can understand more easily. If you need any clarifications about the code don't hesitate to ask me. Thank you very much for your help  Grin

Hm just a quick note:

Code:
if(open_list.Contains(open_list.Find(x=> path_nodes.Contains(x))))
        {
            open_list.Remove(open_list.Find(x => path_nodes.Contains(x)));
        }
    }

you have the same code twice you could pull that out into a variable before the if statement then use the variable like
if (open_list.Contains(found_path) {
  .Remove (found)path)

--

I recommend extracting each of those 3 if statements in your Update method to separate methods, since none of them depend on else. It will make the code easier to read like

void Update
{
 FindStartPath();
 AttemptPathFollow()
 EvaluationPathTargetReached()
 CleanUpPaths
}

I don't like the variable name "open_list".. it's meant to be "nodes to visit", right?
« Last Edit: July 19, 2016, 06:38:06 AM by quantumpotato » Logged

bateleur
Level 10
*****



View Profile
« Reply #14 on: July 19, 2016, 06:55:06 AM »

I haven't reviewed the code in detail, but I suspect you're getting a false positive here.

The reason the presence of FollowPath() causes the crash is, I suspect, because that's the only way for reached_dest to be set to true. If so, it isn't really this function that contains the problem, it's just that you were narrowing down the cause in an overly simplistic way.

As far as I can see you never perform any operations on closed_list itself which even have the potential to throw errors. As such, I think the first step should be to do as quantumpotato initially suggested and attempt to obtain a stack trace. Make sure "Error Pause" is selected in the Editor (it's a tab on the Console panel). If necessary put try...catch blocks around things for debugging purposes. If even that doesn't work, try adding some assertions in code to make sure things aren't null and have sensible values, etc.
Logged

Vampade
Level 0
**



View Profile WWW
« Reply #15 on: July 19, 2016, 07:48:26 AM »

Hi, I tested the code and I found it is crashing not because of FollowPath(), but because when the object changes its node after it finished following the path, and when it has to recalculate the path it crashes (with "its node" I mean the closest node to the object), that's why by disabling FollowPath() it didn't crash, because it didn't change its closest node that uses for the pathfinding. I also tried disabling FollowPath() and I manually changed the gameobject's node after it found a path and unity crashed again.

I just found out that it crashes only when it has reached the end of the path and it has to recalculate the path to the player/target. In fact if I start moving to another place while the gameobject is on its way to the end of the path it recalculates the path and follows it, as it should do (I modified the code that so it updates the gameobject's closest node only when it has reached the end of the path). So the problem happens only when it has already reached a path. Here's an updated version of the code http://hastebin.com/atubekegen.cs . Thank you very much for your great help guys Grin
Logged

quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« Reply #16 on: July 20, 2016, 05:28:44 AM »

Congrats, now git commit!
Logged

Vampade
Level 0
**



View Profile WWW
« Reply #17 on: July 20, 2016, 08:55:18 AM »

Congrats, now git commit!
ahahah no, not yet but I'm pretty close to it
Logged

Vampade
Level 0
**



View Profile WWW
« Reply #18 on: July 28, 2016, 07:11:39 AM »

Hi guys, I'm back Waaagh! I recently modified the part that recalculates the path. I rewrote it from scratch and it works pretty well but it can be improved. The problem (again) is that it crashes making the game unplayable.

It crashes after the gameobject reached the target and clears all the lists before looking again for a path.
In the update, it crashes when it calls DeleteAllLists() both in the if that checks if it reached the destination (reached_dest) and when it checks if the space key has been pressed (because if you press space the player spawns an obstacle wich is a line) and also while I'm moving it doesn't crash because it didn't reach the target (wich is the player). Here's the code http://pastebin.com/swK48cdz what should I change to stop making it crash Crazy??

Thank you very much for your great help  Grin
Logged

quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« Reply #19 on: July 28, 2016, 08:09:06 AM »

Is it crashing in the same way as before when the path is reached?
Logged

Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic