#What is this C sharp Logic?
49 messages · Page 1 of 1 (latest)
A Vector2 is a struct, and you can't use += on an individual piece of the struct. You need to modify the whole thing
So you can add a whole vector with +=, but not the individual x value
how come
Because that's how c# works
It doesn't seem like it should have
make sure this is the same code
No, you can't modify the x like that at all
You'd have to modify the entire vector at once
Pretty sure it's always been this way
lemme see my gitpod
I have old dev env on there
arent these vairables
no errors in my old gd
Dunno then, maybe it wasn't always like this
the thing is
that when I define a variable
called velocity
and same type
it doesnt glitch out
Maybe this is only the case with properties
but whats wrong with it
thats the thing
is this ok to report to issues
No, this is not a Godot issue
then what do I do
Fix it
Either make it a normal variable instead of a property, or don't modify the x like that
You can do something like Velocity += new Vector2(somex, 0)
This is just due to the way that properties work. They have getters and setters behind the scenes, so you can't modify the x value of a vector that was returned from a function call
It's like doing GetVelocity().x += 10
does this work?
public override void _PhysicsProcess(double ddelta)
{
Vector2 velocity = Velocity;
float delta = (float) ddelta;
if (Input.IsActionPressed("go_left"))
{
velocity.x -= Constants.PlayerAcceleration * delta;
}
if (Input.IsActionPressed("go_right"))
{
velocity.x += Constants.PlayerAcceleration * delta;
}
if (Input.IsActionPressed("jump"))
{
if (IsOnFloor())
{
velocity.y = Constants.PlayerJumpSpeed * -delta;
}
}
velocity.x *= Constants.PlayerSpeedDamp;
velocity.y += Constants.Gravity * delta;
Velocity = velocity;
MoveAndSlide();
}```
probably
is it good code
seems fine