C
C#4w ago
Ab

✅ Something is causing this to go into stack overflow

No description
39 Replies
Ab
Ab4w 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
Ab4w ago
No description
canton7
canton74w ago
Presumably you can't delete that file without also removing the code which uses it?
Buddy
Buddy4w ago
Are you using raylib?
Ab
Ab4w ago
Yeah Sorry can you rephrase
canton7
canton74w 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
Ab4w ago
I removed that code too The project builds fine as long as this file doesn’t have any code
canton7
canton74w ago
Right, and are you sure it wasn't that code, which you haven't shown us, which was causing the issue?
Ab
Ab4w 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
canton74w ago
There's nothing in the code you've shown which should cause a stackoverflow
Ab
Ab4w ago
That doesn’t seem to cause an issue leaving it there
canton7
canton74w ago
So I'm assuming it must be something in the way that code was being used, which you've had to change
Ab
Ab4w 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
Ab4w ago
No description
Ab
Ab4w 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
Buddy4w ago
What does call stack tell you? Call stack allows you to narrow down stackoverflow's
Ab
Ab4w ago
how do I see it
Buddy
Buddy4w ago
Run in debug mode From VS When the exception is thrown, it should break
Ab
Ab4w ago
No description
Ab
Ab4w ago
is this what you mean? I'm running in debug mode always
canton7
canton74w ago
Oh, the Player constructor references GameManager?
Ab
Ab4w ago
No description
Ab
Ab4w ago
it shouldn't be
canton7
canton74w 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
Buddy4w ago
Properties acts basically like a proxy / middleman, it shouldn't set / get itself as that results in infinite recursion
canton7
canton74w 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
Ab4w ago
No description
Ab
Ab4w ago
okay yeah this worked thank you so much that bug was mind boggling me to hell
Buddy
Buddy4w 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
Ab4w ago
oh sweet! good to know it's how I return collider info about the player
Buddy
Buddy4w 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
Ab4w 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
canton74w ago
Yeah, I like using this
Buddy
Buddy4w ago
Conventions of using underscore in C# is only for backing fields, like this.
canton7
canton74w ago
There are a ton of different conventions
Buddy
Buddy4w ago
according to official Microsoft ones
canton7
canton74w ago
You should try reading MS's own code -- there's a huge mix in there too
Ab
Ab4w 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!!