Godot Docs "Your first 2D game coding your player" questions
- Edited
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)
spaceyjase That is still not valid GDScript. 0, 0 is not a parameter list.
Zini I can't fix everything
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.
- Edited
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.
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:
- Is there anything else I can do to fix this error?
- 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.
- Edited
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
andelse
. - Line 32 + 33 need to be indented one more level, like line 29.
- Line 42 is indented too much as well.
- Edited
Yes I am and yes I am will do so again. Thank you.
I have found a yellow warning but no errors but the sprite would not appear on screen.
All the information can be seen in the above screenshots. Any advice would be greatly appreciated.
- Edited
Carmes The editor is saying the body
parameter that is passed into _on_Player_body_entered
isn't used (yet). It is just a warning and can likely be ignored for now.
A warning like this could mean the programmer has made a mistake; perhaps the intention was to use a variable and they didn't? Or maybe they thought they did but have misspelled something (for example) so should always be checked. If you want to suppress the warning, the editor indicates you can change it to _body
:
func _on_Player_body_entered(_body):
pass
And the warning will disappear. But it's likely you'll use it in the tutorial so don't worry too much if you understand why the warning has appeared.
Note: you have a pass
statement here that should also likely be removed before the statements following it (emit_signal
, etc). This probably won't fix the sprite issue (possibly something else, worth reviewing the manual again...) but itโs unnecessary.
Keep going!
I have just figured out, why I could not see the sprite, I however still cannot make the character move.
The full script is seen below, thank you for your aid.
I have hypothesized that the reason I am unable to move sprite is because there is no back round to move it in. I will keep going and come back to that problem later.
Could you try to move the following block up ?
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
Put it just before you are beginning to set the position
(before line 25 in your screenshot).
If velocity.x
(or y) is set to 1, then moving the sprite's position by velocity * delta
will move the sprite at the velocity of 1 pixel-per-second.
You have to set a higher value if you want to see anything moving on the screen.
In this tutorial, you need to multiply your velocity vector by the speed value before applying it to the position.