C
C#6mo ago
Ab

✅ Something is causing this to go into stack overflow

No description
39 Replies
Ab
AbOP6mo ago
when I would run my code, it would always show a stackoverflow error and I narrowed down the problem to this file if I delete all the content of this class, the entire project runs fine But I can't figure out what's doing it
Ab
AbOP6mo ago
No description
canton7
canton76mo ago
Presumably you can't delete that file without also removing the code which uses it?
Buddy
Buddy6mo ago
Are you using raylib?
Ab
AbOP6mo ago
Yeah Sorry can you rephrase
canton7
canton76mo ago
Presumably you've got code which uses that GameManager class? So if you remove that class, the code which uses is needs to also be removed?
Ab
AbOP6mo ago
I removed that code too The project builds fine as long as this file doesn’t have any code
canton7
canton76mo ago
Right, and are you sure it wasn't that code, which you haven't shown us, which was causing the issue?
Ab
AbOP6mo ago
I doubt it because right now The project builds as long as the above code is gone. Or specifically I found every bit of it except int facing
canton7
canton76mo ago
There's nothing in the code you've shown which should cause a stackoverflow
Ab
AbOP6mo ago
That doesn’t seem to cause an issue leaving it there
canton7
canton76mo ago
So I'm assuming it must be something in the way that code was being used, which you've had to change
Ab
AbOP6mo ago
It’s possible but that would be even more confusing Because that code is gone now So how could it still be happening Should I just show the code left in the other file?
Ab
AbOP6mo ago
No description
Ab
AbOP6mo ago
this is the other file when all the code that references Game manager (except facing) is removed and this builds fine now as long asGameManager is cleared out
Buddy
Buddy6mo ago
What does call stack tell you? Call stack allows you to narrow down stackoverflow's
Ab
AbOP6mo ago
how do I see it
Buddy
Buddy6mo ago
Run in debug mode From VS When the exception is thrown, it should break
Ab
AbOP6mo ago
No description
Ab
AbOP6mo ago
is this what you mean? I'm running in debug mode always
canton7
canton76mo ago
Oh, the Player constructor references GameManager?
Ab
AbOP6mo ago
No description
Ab
AbOP6mo ago
it shouldn't be
canton7
canton76mo ago
Oh, there we go public int X { get => X; set => X = value; } That's sugar for:
public int get_X()
{
return get_X();
}
public void set_X(int value)
{
set_X(value);
}
public int get_X()
{
return get_X();
}
public void set_X(int value)
{
set_X(value);
}
Buddy
Buddy6mo ago
Properties acts basically like a proxy / middleman, it shouldn't set / get itself as that results in infinite recursion
canton7
canton76mo ago
If you want to write the getter/setter manually, you normally need a separate backing field:
private int _x;
public int X { get => _x; set => _x = value; }
private int _x;
public int X { get => _x; set => _x = value; }
So the getter/setter interact with the backing field, and the backing field actually holds the value. With what you have at the moment, the getter and setter just call themselves again and again You can also simplify the above to:
public int X { get; set; }
public int X { get; set; }
That tells the compiler to generate a hidden backing field for you, and implement the getter/setter as above
Ab
AbOP6mo ago
No description
Ab
AbOP6mo ago
okay yeah this worked thank you so much that bug was mind boggling me to hell
Buddy
Buddy6mo ago
The Rectangle can also be shortened to
public Rectangle Rectangle => new Rectangle(X, Y, 10, 200);
public Rectangle Rectangle => new Rectangle(X, Y, 10, 200);
Ab
AbOP6mo ago
oh sweet! good to know it's how I return collider info about the player
Buddy
Buddy6mo ago
Also this isn't Java, so you pretty much never need to use this. unless for certain cases. In that class it isn't needed except for the HP since the declared names are the same (case-sensitive).
Ab
AbOP6mo ago
I'm trying to get out of the habit. It's just easier to visualize immediately class member variables by seeing "this" I know some people use underscore
canton7
canton76mo ago
Yeah, I like using this
Buddy
Buddy6mo ago
Conventions of using underscore in C# is only for backing fields, like this.
canton7
canton76mo ago
There are a ton of different conventions
Buddy
Buddy6mo ago
according to official Microsoft ones
canton7
canton76mo ago
You should try reading MS's own code -- there's a huge mix in there too
Ab
AbOP6mo ago
yep turns out I did the same mistake in my Bullet class so every time the player fires, it spirals it again thanks again for the help!!
Want results from more Discord servers?
Add your server