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