Help understanding some code
I am following along with a tutorial on YouTube for AStar. There is one portion of the code that does not make sense. I will do my best to highlight the code and explain why it does not make sense.
First, we take a look at the node class. From my understanding of this code (I am not fluent in CS) it seems like g and h cost are left uninitialized.
https://github.com/SebLague/Pathfinding/blob/master/Episode%2003%20-%20astar/Assets/Scripts/Node.cs
Knowing that gCost and hCost are never initialized, I assume that it is initialized later on. But I can not seem to find where this happens.
In the main pathfinding function for AStar implementation, the code magically used the gCost value in an evaluation (Line 46). But how could it exist? Furthermore, let's consider it was magically initialized to some arbitrary number, that doesn't represent a valid gCost in the aStar algorithm. Realistically the gCost would not be known until a neighbour observed it... https://github.com/SebLague/Pathfinding/blob/master/Episode%2003%20-%20astar/Assets/Scripts/Pathfinding.cs
GitHub
Pathfinding/Episode 03 - astar/Assets/Scripts/Node.cs at master · S...
Contribute to SebLague/Pathfinding development by creating an account on GitHub.
GitHub
Pathfinding/Episode 03 - astar/Assets/Scripts/Pathfinding.cs at mas...
Contribute to SebLague/Pathfinding development by creating an account on GitHub.
7 Replies
Inetegers are value types. Meaning, they cannot be
null
or uninitialized. Instead, they get a default valueAngius
REPL Result: Success
Result: int
Compile: 230.949ms | Execution: 21.261ms | React with ❌ to remove this embed.
In the case of an integer, it's
0
ahh
okay that makes a lot of sense now
thanks
just keep in mind they are unassinged if ur inside a method for example
i.e.:
that's kind of weird
what's the reason?
because the compiler in order to avoid unnecessary issues prevents local and body variables from being unassigned, These variables depend on either invocation or input to be assigned.
unlike a field for a local variable the compiler doesn't assume anything