Godot "Your First Game" Tutorial - I keep getting an errors.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Li282

So I literally just started learning how to use Godot. I have zero experience with scripting and have no idea how to fix the error I keep running into.

During the tutorial, I am at the part where you need to add “game_over” and “new_game”. This is the script I have:

func game_over():

func new_game():
     score = 0

The line “score = 0” gets highlighted in red and I get an error that reads this:
The identifier “score” isn’t declared in the current scope.

I decided to move on thinking maybe something I input later would fix it, but then when I come to the end to check to make sure everything works, I am told to add a line under the _ready function as typed below:

func _ready():

The line with “new_game()” gets highlighted in red and I get a similar error that reads this:

The method “new_game()” isn’t declared in the current class.

I typed everything in as shown and I am not sure what it is I am doing wrong nor how to declare anything. Help would be much appreciated. Thank you.

Here is the whole script for Main.gd and Player.gd where the problems occur.


extends Node

export (PackedScene) var Mob
var score

func _ready():

func _on_StartTimer_timeout():

func _on_ScoreTimer_timeout():
    score += 1

func _on_MobTimer_timeout():
    $MobPath/MobSpawnLocation.offset = randi()
    var mob = Mob.instance()
    var direction = $MobPath/MobSpawnLocation.rotation + PI / 2
    mob.position = $MobPath/MobSpawnLocation.position
    direction += rand_range(-PI / 4, PI / 4)
    mob.rotation = direction
    mob.linear_velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0)
    mob.linear_velocity = mob.linear_velocity.rotated(direction)


extends Area2D

signal hit

export var speed = 400
var screen_size

func _ready():
    screen_size = get_viewport_rect().size

func _process(delta):
    var velocity = Vector2()
    if Input.is_action_pressed("ui_right"):
            velocity.x += 1
    if Input.is_action_pressed("ui_left"):
            velocity.x -= 1
    if Input.is_action_pressed("ui_down"):
            velocity.y += 1
    if Input.is_action_pressed("ui_up"):
            velocity.y -= 1
    if velocity.length() > 0:
            velocity = velocity.normalized() * speed
    position += velocity * delta
    position.x = clamp(position.x, 0, screen_size.x)
    position.y = clamp(position.y, 0, screen_size.y)
    if velocity.x != 0:
            $AnimatedSprite.animation = "walk"
            $AnimatedSprite.flip_v = false
            $AnimatedSprite.flip_h = velocity.x < 0
    elif velocity.y != 0:
            $AnimatedSprite.animation = "up"
            $AnimatedSprite.flip_v = velocity.y > 0

    func _on_Player_body_entered(body):
        $CollisionShape2D.set_deferred("diable", true)

    func start(pos):
        position = pos
        $CollisionShape2D.disabled = false

    func game_over():

    func new_game():
        score = 0

Starting think maybe I put something in the wrong script or misread something.

Please post your whole script for “Main.gd”.

kidscancode | 2020-07-24 09:36

I edited my post to add the script for both main.gd and player.gd since that is where the problems occur.

Li282 | 2020-07-24 20:04

:bust_in_silhouette: Reply From: IvanVoirol

Hey, also made that tutorial recently.

I think “not declared” means you haven’t defined what score is. In this case it’s supposed to be a variable. The first lines of the concerning script should be :

extends Node

export (PackedScene) var Mob
var score

Notice var score, which is the first occurence of the score variable, which sets it. Have you written that part in your script?

I have that. Not sure what I did wrong. Thinking maybe I put the script in the wrong scene script.

I edited my original post to have the full script as requested.

Li282 | 2020-07-24 20:05

The new_game() and game_over() functions belong in Main.gd, not Player.gd. That’s why Main.gd says new_game() is not defined.

kidscancode | 2020-07-24 20:44

Here are the instructions:

"Next, select the Player node in the Scene dock, and access the Node dock on the sidebar. Make sure to have the Signals tab selected in the Node dock.

You should see a list of the signals for the Player node. Find and double-click the hit signal in the list (or right-click it and select “Connect…”). This will open the signal connection dialog. We want to make a new function named game_over, which will handle what needs to happen when a game ends. Type “game_over” in the “Receiver Method” box at the bottom of the signal connection dialog and click “Connect”. Add the following code to the new function, as well as a new_game function that will set everything up for a new game:"

When I do that, it puts the script in the player.gd. Did I select something wrong to make it put it in the Player.gd? Trying to understand what I read wrong.

Thank you for the help.

Li282 | 2020-07-24 20:58

Ope, nevermind. I figured out what happened. When I connected it, for some reason it has the player node highlighted and not main. So I basically connected to itself. I am an idiot.

Li282 | 2020-07-24 21:16