#What is this C sharp Logic?

49 messages · Page 1 of 1 (latest)

dapper schooner
#

Look at the type def for CharacterBody2D
Now how can I use Velocity in _Ready but not _PhysicsProcess?

hollow jay
#

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

hollow jay
#

Because that's how c# works

dapper schooner
#

this code worked back in gd3

#

lemme check my github

hollow jay
#

It doesn't seem like it should have

dapper schooner
#

make sure this is the same code

dapper schooner
#

still doesnt work

hollow jay
#

No, you can't modify the x like that at all

#

You'd have to modify the entire vector at once

dapper schooner
#

what happened with vector

#

why did it become like thi

#

*this

hollow jay
#

Pretty sure it's always been this way

dapper schooner
#

I have old dev env on there

dapper schooner
#

no errors in my old gd

hollow jay
#

Dunno then, maybe it wasn't always like this

dapper schooner
#

that when I define a variable

#

called velocity

#

and same type

#

it doesnt glitch out

hollow jay
#

Maybe this is only the case with properties

dapper schooner
#

thats the thing

dapper schooner
hollow jay
#

No, this is not a Godot issue

dapper schooner
hollow jay
#

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)

dapper schooner
#

hmm\

#

Imma get and set every frame

hollow jay
#

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

dapper schooner
# hollow jay This is just due to the way that properties work. They have getters and setters ...

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();
    }```
hollow jay
#

probably

dapper schooner
#

is it good code

hollow jay
#

seems fine

dapper schooner
#

hmm

#

sprite isnt moving

#

lets see something