Hello it is me again, I am working through the official Godot tutorial "Your first 2D game".

https://docs.godotengine.org/en/3.5/getting_started/first_2d_game/03.coding_the_player.html

I am having more success so far in comparison to the previous attempts but I have 3 questions.

I interpreted these instructions as writing it out below the extends area 2D.

As can be seen the code is acting funny. I am wondering what identifier I should use to fix this or if I am going in the wrong direction. Also the control elements of this code are different from what is written down. (see below)

I was unable to change that situation for what ever reason. Would this cause problems?

My last question involves this paragraph. "We start by setting the velocity to (0, 0) - by default, the player should not be moving. Then we check each input and add/subtract from the velocity to obtain a total direction. For example, if you hold right and down at the same time, the resulting velocity vector will be (1, 1). In this case, since we're adding a horizontal and a vertical movement, the player would move faster diagonally than if it just moved horizontally."

I interpreted that instruction as coding in func _velocity(0,0).

Is this correct or should I delete this code?

Thank you for aiding me on this coding journey.

  • Toxe replied to this.

    Remove the () around hit. A signal isn't a function. Also, next time you get an error message, please include it in your question.

    func _velocity(0.0) isn't valid GDscirpt. Not sure what that is supposed to do.

      Carmes The signal should be declared as signal hit, not signal(hit).

      Are there any other specific error messages you get?

        Toxe

        I get "error(3,7): Expected an identifier after "signal". "

        Zini Thank you for your aid. I have solved two of my problems.

        I have a new question I have imputed the script "position += velocity * delta" as mentioned on the official Godot web page.

        Exact quotes below:

        "position += velocity * delta
        position.x = clamp(position.x, 0, screen_size.x)
        position.y = clamp(position.y, 0, screen_size.y)"

        I got the error "error (33,1) Unexpected token: Identifier:position "

        I am not sure what to do with this information.

        Any help would be appreciated.

        You guys are awesome

          The code looks fine. Your problem is probably somewhere else.

          Carmes Did you fix line 30? That one reads func _velocity(0,0), which is wrong and might be the reason for the error.

            Carmes as mentioned by @Toxe, the error is in the function definition as it is missing a ':'...

            func _velocity(0, 0): # <- ':' here to end the function declaration
                position += velocity * delta
                # the rest of the code

            Note the formatting (e.g. spaces or tabs that align your code) are also important. Always worth taking a break and double-checking the code against the docs (to fix further script errors!).

            But I think what you really want to do is as stated in the docs and remove the func _velocity(0, 0):

            Add the following to the bottom of the _process function (make sure it's not indented under the else):
            position += velocity * delta
            position.x = clamp(position.x, 0, screen_size.x)
            position.y = clamp(position.y, 0, screen_size.y)

            • Zini replied to this.

              I figured out what went wrong with a decent chunk of the coding, it was indentation problems (noob move me). I also removed func _velocity(0,0).

              Currently there is an error in the code: "position.y = clamp(position.y, 0, screeen_size.y)".

              I have changed the indentation but nothing has happened.

              The error reads "identifier "screen_size" isn't declared in the current scope"

              the previous code for screen size reads: " screen_size = get_viewport_rect().size" as requested by the document.

              I am not sure how to remedy this situation.

              Any advice would be greatly appreciated.

              Thank you.

              I think it would be best if you post the complete code and error message.

              Just figured out the issue, From this experience I learned two things.

              1) I need to tinker around with indents more

              2) I need to get better at finding typos.

              Thank you for all your aid.

              Another error has manifested in line 32 elif velocity.y != 0:

              I have tinkered with the spelling and indentation but it is not working.

              the error is as follows: Error parsing expression, misplaced elif. I copied the instructions as stated within the web page:

              https://docs.godotengine.org/en/3.5/getting_started/first_2d_game/03.coding_the_player.html

              I am still not sure what to do with this information

              my code is below, I hope this aides in answering my question.

                Carmes The if-statement syntax is:

                var n = 42
                
                if n == 42:
                    print("yay!")
                elif n == 69:
                    print("oh là là")
                elif n == 0:
                    print("meh")
                else:
                    print("whatever")

                else can only be the last clause, elif comes before it.

                https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/gdscript_basics.html#if-else-elif

                I have updated the code by placing the elif above the else. (see code below). Error has not changed. (Error parsing expression, misplaced elif)

                I attempted to change elif to if to see if that would work, it did but created a series of errors undone through indents before getting another error (Error parsing expression, misplaced else). So undid that and now my code is as below. I am wondering two things:

                1. Is there anything else I can do to fix this error?
                2. Has anyone completed the tutorial and have the script in screen shot form so I may use it as reference to see where I am going wrong in following the instructions?

                Thank you for your aid in this process.

                • Toxe replied to this.

                  Carmes Has anyone completed the tutorial and have the script in screen shot form so I may use it as reference to see where I am going wrong in following the instructions?

                  Only for Godot 4 but I don't think that helps much.

                  • Line 31 is indented one level too much. Needs to be the same level as the if and else.
                  • Line 32 + 33 need to be indented one more level, like line 29.
                  • Line 42 is indented too much as well.