C
C#9mo ago
2spooky2play

✅ confused by this error

class Player(Vector2 position, Vector2 size) {
public Vector2 Position { get; } = position;
public Vector2 Velocity { get; private set; } = Vector2.Zero;
public Vector2 Size { get; } = size;

public Rectangle GetRect() {
return new(Position, Size);
}

public void Input() {
float dt = GetFrameTime();
Velocity.X = 0.0f;
}
}
class Player(Vector2 position, Vector2 size) {
public Vector2 Position { get; } = position;
public Vector2 Velocity { get; private set; } = Vector2.Zero;
public Vector2 Size { get; } = size;

public Rectangle GetRect() {
return new(Position, Size);
}

public void Input() {
float dt = GetFrameTime();
Velocity.X = 0.0f;
}
}
Cannot modify the return value of 'Player.Velocity' because it is not a variable in the Input method all i am doing is modifying the field of the struct, why is that a problem?
4 Replies
2spooky2play
2spooky2playOP9mo ago
ok read, up on it, its because it is a copy Velocity = new(0.0f, Velocity.Y); is there a better syntax for this?
Angius
Angius9mo ago
Velocity = Velocity with { X = 0.0f } maybe?
2spooky2play
2spooky2playOP9mo ago
ill keep that in my back pocket, thanks
Angius
Angius9mo ago
And, yeah, that's because Vector2 is a struct
Want results from more Discord servers?
Add your server