C
C#6mo ago
Darmyn

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
Angius
Angius6mo ago
Inetegers are value types. Meaning, they cannot be null or uninitialized. Instead, they get a default value
MODiX
MODiX6mo ago
Angius
REPL Result: Success
class Foo
{
public int Bar;
}

new Foo().Bar
class Foo
{
public int Bar;
}

new Foo().Bar
Result: int
0
0
Compile: 230.949ms | Execution: 21.261ms | React with ❌ to remove this embed.
Angius
Angius6mo ago
In the case of an integer, it's 0
Darmyn
DarmynOP6mo ago
ahh okay that makes a lot of sense now thanks
leowest
leowest6mo ago
just keep in mind they are unassinged if ur inside a method for example i.e.:
public void Something()
{
int x;
// print x, error
// error CS0165: Use of unassigned local variable 'x'
}
public void Something()
{
int x;
// print x, error
// error CS0165: Use of unassigned local variable 'x'
}
Darmyn
DarmynOP6mo ago
that's kind of weird what's the reason?
leowest
leowest6mo ago
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
Want results from more Discord servers?
Add your server