Hello, I am new in Godot and i have a problem in my GDScript, because appear the error "Invalid call. Nonexistent function 'size' in base 'Nil'.
I will be very grateful if someone help about this error.
`extends Control
var player_words = []
var current_story = {}

@onready var PlayerText = $VBoxContainer/HBoxContainer/LineEdit
@onready var Displaytext= $VBoxContainer/Label
@onready var Butlabel=$VBoxContainer/HBoxContainer/Label2
func _ready():
pick_current_story()

Displaytext.text="Bienvenidoooo "
check_player_words_lenght()
PlayerText.grab_focus()

func pick_current_story():
var stories = get_from_json("StoryBook.json")
randomize()
current_story = stories[randi() % stories.size()]

func get_from_json(filename):
var file : FileAccess = FileAccess.open("res://" + filename, FileAccess.READ) # Opens file
var content = JSON.parse_string(file.get_as_text())
file.flush() # Good practice to close the file when we are done with it
return content # Return the JSON content

func _on_line_edit_text_submitted(new_text):
add_to_player_words()
func _on_texture_button_pressed():
if is_story_done():
get_tree().reload_current_scene()
else:
add_to_player_words()
func add_to_player_words():
player_words.append(PlayerText.text)
Displaytext.text=""
PlayerText.clear()
check_player_words_lenght()
func is_story_done():
return player_words.size()==current_story.prompts.size()
func check_player_words_lenght():
if is_story_done():
end_game()
else:
prompt_player()

func tell_story():
Displaytext.text=current_story.story % player_words

func prompt_player():
Displaytext.text+= "May I Have"+ current_story.prompts[player_words.size()] + " please"
func end_game():
PlayerText.queue_free()
Butlabel.text="Again"
tell_story()

`

  • xyz replied to this.

    AlejandroOZ23 The object on which you're trying to call the size() method is undefined i.e. it's a null reference. Check at which line the error occurred and see on which object you're calling that method. Make sure that the object reference gets properly initialized in preceding code.