Cleaning a Code in gdscript 3.1

gdt_s03_cleaning_up_the_player_code

#1

Hello, Wonderfull Godot Teachers (and colleagues)!

First and foremost - Thank you for your Job! I’m a absolute beginner in coding, and as beginner I will say it is a very good introduction to starting it. I think I started to getting it finally (I’m begginner but it not mean I was never trying to learn coding).
I know course is meant for pre 3.1 version, but it is just around the corner, so I tried to give it a go. And it was all nice and easy, till this moment. I’m almost sure, that there is something I should make different to make code working on 3.1…

I was looking in documentation, and on this forum. @ShadeWolfDK made his own working version, but it’s bit different, (he is using signals) so it’s no help for me (at least for now)

so all I can get now from Godot is:

func update(motion)

  • / Parse Error: Function signature doesn’t match the parent. Parent signature is: ‘void update()’.

When I delete “motion” i get different error.

func update()
Parse Error: Identifier ‘motion’ is not declared in the current scope.

I hope it is something trivial, and will not need to come back to 3.0.6.
And i just copied source from github, so it is not a some kind of stupid typo from my side.

Thank you again and waiting for an answer!

Adam


#2

Does your script inherit from another one ?
I dont really remember about this section, but if your parent scripts have the same function, but not with the same parameter, it could throw an error like this one.

Example… You have a Player.gd who extends from Character… If your Character has a update() function, your player.gd needs to have the same fonction, so it will not take the motion parameter.


#3

@GrossePistache Thanks for an answer. Yes, script inheriting form another one, that’s a part of a course when it is introduced. What you suggest sounds reasonable, but didn’t give me a result.

after a while

So today I will carefully watch this lesson again :blush:

after even more while

Ok… I step by step rewatched a lesson. And I’m 99% sure, that there is no typo in my code. (But I admit, that as I can very fast see “resonsibility” in last Juan Linietsky post I’m not that fast to find typos in code for now. In fact I’m very slow in it) So i decide to not fight with it - I just have it all in one script, but I made individual functions for animation and motion, so it is more less what was a point of a lesson.

it goes like this:

extends KinematicBody2D

var motion = Vector2()
const SPEED = 180
const GRAVITY = 1000
const UP = Vector2(0,-1)
const JUMP_SPEED = -400

func _physics_process(delta):
	update_motion(delta)
	
func _process(delta):
	update_animation(motion)

func update_motion(delta):
	fall(delta)
	run()
	jump()
	move_and_slide(motion, UP)
	
func fall(delta):
	if is_on_floor():
		motion.y = 0
	else:
		motion.y += GRAVITY * delta

func run():
	if Input.is_action_pressed("ui_right") and not Input.is_action_pressed("ui_left"):
		motion.x = SPEED

	elif Input.is_action_pressed("ui_left") and not Input.is_action_pressed("ui_right"):
		motion.x = -SPEED
	
	else: 
		motion.x = 0
	
func jump():
	if Input.is_action_pressed("ui_up") and is_on_floor():
		motion.y = JUMP_SPEED

func update_animation(motion):
	if motion.y < 0:
		$AnimatedSprite.play("idle")
	elif motion.x < 0:
		$AnimatedSprite.play("move")
		$AnimatedSprite.flip_h = false
	elif motion.x > 0:
		$AnimatedSprite.play("move")
		$AnimatedSprite.flip_h = true
	else:
		$AnimatedSprite.play("idle")