0 votes

i've been trying to follow a tutorial for making a clicker game and my code looks identical to the one shown in the video, and im trying to store the count variable to a file. (The full script in the video can be seen at ~26 minutes in.)
When I try to run it it closes immediately and tells me "Invalid get index 'count' (on base Nil) ," and it points out the following line specifically.

count = data.count

The following is the full script.

extends Node

var autoclicker = 0
export var count = 0

func _ready():
    var file = File.new()
    if file.file_exists("user://save.txt"):
        file.open("user://save.txt", File.READ)
        var data = parse_json(file.get_line())
        count = data.count

func _on_Make_a_Thing_pressed():
    count +=  1

func _process(delta):
    $Thngs.text = str(count)

func _on_SavingTimer_timeout():
    var data = {
            count = count
        }
    var file = File.new()
    file.open("user://save.txt", File.WRITE)
    file.store_line(to_json(data))
    file.close()

Any help is much appreciated!

Godot version Godot 3.4.4
in Engine by (12 points)

1 Answer

0 votes

Usually "Invalid get index" means that you've incorrectly called something - either because there's a slight misspelling or because the object you're calling does not exist.

"on base Nil" indicates that it can't find 'data' - can you confirm that everything in this line of code

var data = parse_json(file.get_line())

is properly entered, and consistent from previous usages?

You can try calling

print(data, count, file.get_line())

or

print("data is ", data, "count is ", count, "file.get_line() is ", file.get_line())

instead of the line of code "count = data.count", and then checking the console - what's coming up for data? what's coming up for count? file.get_line()? that might help you see where the problem might be.

by (166 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.