Yes you’r right, I did it to test if I can call the get_from_json func directly.
My json file lies under: /home/jan/Godot/LoonyLips/stories.json
Exactly the place where I put the gfx folder in.
Here is my whole skript:
extends Node2D
var player_words=[] #words given by player
var current_story
func _ready():
$Blackboard/Storytext.text = “Willkommen zu diesem Spiel.\nBitte gib die angefragen Woerter ein.\nUmlaute werden leider nicht erkannt. \n \nGib mir bitte “+ current_story.prompt[player_words.size()] +”.”
set_random_story()
func set_random_story():
var stories = get_from_json(“stories.json”)
randomize()
current_story = stories [randi() % stories.size()]
func get_from_json(filename):
var file = File.new()
file.open(filename, File.Read)
var text = file.get_as_text()
var data = parse_json(text)
return data
file.close()
func _on_TextureButton_pressed():
if story_done():
get_tree().reload_current_scene()
else:
var new_text = $Blackboard/Textbox.get_text()
_on_Textbox_text_entered(new_text)
func _on_Textbox_text_entered(new_text):
player_words.append(new_text)
$Blackboard/Textbox.text=""
check_player_word_length()
func story_done ():
return player_words.size() == current_story.prompt.size()
func prompt_player():
$Blackboard/Storytext.text =“Gib mir bitte “+current_story.prompt[player_words.size()] +”.”
func check_player_word_length():
if story_done():
tell_story()
else:
prompt_player()
func tell_story():
$Blackboard/Storytext.text = current_story.story %player_words
end_game()
func end_game():
$Blackboard/Textbox.queue_free()
$Blackboard/TextureButton/ButtonLabel.text =“Noch einmal”
Here is my stories.json:
[ {
“prompt” :[
"einen Namen ",
"einen Gegenstand ",
"einen weiteren Gegenstand ",
“ein Gefuehl”
],
“story” :
“Eines Tages nahm %s %s um damit %s kaputt zu machen. Doch es fuehlte sich %s an.”
},
{
“prompt” :[
“eine Automarke”,
“eine Beleidigung”,
"einen Namen "
],
“story” :
“In einem %s faehrt ein %s. Ach nein es ist doch nur %s.”
},
{
“prompt” :[
"eine Eigenschaft ",
"einen Ort ",
"eine zweite Eigenschaft ",
"einen Gegenstand ",
"einen Namen "
],
“story”:
“Auf einer %s %s liegt ein %s %s. Nein das ist ja %s”
}
]