📜  Godot中的Storybook故事对象JSON

📅  最后修改于: 2021-01-02 09:53:08             🧑  作者: Mango

故事书-故事对象(JSON)

我们正在创建不太混乱的LonnyLips脚本。然后,首先,创建一个StoryBook节点,然后将每个故事作为StoryBook的单独子节点,并使每个故事使用相同的脚本,而我们希望新脚本为每个故事保存数据。

然后我们创建一个节点。单击加号( + )来添加节点。

在这里,我们可以在下面看到该节点已创建。

节点的属性。

然后将“节点”名称重命名为StoryBook。

之后,创建一个名为story0的节点,该节点是StoryBook的子级。然后创建其名为Story0的子节点。

右键单击节点Story0 ,然后单击附加的脚本以打开文本。

然后从路径中删除0后创建它。

这是脚本选项卡。

编码:

再创建一个名为story1的节点,它是story0的副本。

要制作任何节点的重复节点,请右键单击该节点,然后转到重复或表达控制权以进行其下一个系列。或者,我们可以单击关键字ctrl + D,这是重复项的快捷方式。

Story0中的代码:

extends Node
var prompts=["a happy name", "a noun", "adverb", "adjective"]
var story = "It takes many %s deeds to build a %s reputation, and %s bad one to %s it."

复制节点1之后,然后在代码中添加导出函数,如下所示。

在代码中添加导出函数后,这将在属性中发生:

我们也可以在检查器属性中添加故事。然后从如下所示的检查器属性中相应地更改其故事。


我们还可以如下添加数组数组类型

但是现在,我们不这样做:

如果要在脚本中添加一个数组,我们将进行编码,然后它将自动显示在属性中

在这里,我们使用键入的G.DScript。

我们可以看到同时按下ctrl的任何关键字的描述和特定关键字。

码:

或者我们可以用字符串代替“。”

然后,我们将访问lonnylips.gd并将所有故事注释到脚本中,如下所示。

lonnylips.gd的示例:

extends Control
var player_words=[]
#var template = [
#{
#    "prompts":["a name", "a noun", "adverb","adjective"],
 #   "story" : "It takes many %s deeds to build a %s reputation, and %s bad one to %s it."
  #  },
    #{
    #    "story":"There once was %s called %s who searched far and wide for the mythical %s noun of %s.",
    #    "prompts": ["a noun","a name", "an adjective", "another name"], 
    #},
    #{
    #    "story":"Once upon a time a %s ate a %s felt very %s. It was a %s day for all good %s.",
    #    "prompts": ["a name","a thing", "a feeling", "another feeling", "some things"], 
    #},
    #{
    #    "story":"There once was %s called %s that lived as %s as a %s." ,
    #    "prompts": ["a thing","a name", "a descrition", "a thing"], 
    #},
    #{
    #    "story":"There once was %s called %s who searched far and wide for the mythical %s noun of %s.",
    #    "prompts": ["a noun","a name", "an adjective", "another name"], 
    #},
    #{
    #    "story":"a Poem.\n\n1 I was a %s as can be, \n then we could call me %s, \n and i would talk to %s ",
    #    "prompts": ["a noun (a thing)","an adjective(a description word)", "a person's name"], 
    #},
    #{
    #    "story":"Dear %s,I hope this letter finds we well. I have spent the past three weeks in %s researching the %s of donkies for my new book. I miss you hesitantly, and whenever i see a %s I think of u. ",
    #    "prompts": ["a person's name","the name of a place", "the plural of a noun", "another name"] 
    #},
    #{
    #    "story":"Once upon a time, a %s hero called %s was sent to %s, tio defeat a %s %s. He did so, return to defeat %s",
    #    "prompts": ["an adjective","a person's name", "a place", "an adjective","a noun","another place"],
    #},
    #{
    #    "story":"The ultimate pizza recipe. \n\n Moix one packet of %s with three spoonfuls of flour. Knead it as it %s then put the %s and %s on it, after that open the oven and %s it.",
    #    "prompts": ["a noun","an adjective", "another noun", "yet another noun", "adjective"] 
    #}
    #]
var current_story

onready var PlayerText= $VBoxContainer/HBoxContainer/PlayerText
onready var DisplayText= $VBoxContainer/DisplayText

func _ready():
    randomize()
#    current_story=template[randi()% template.size()]
    set_current_story()
    DisplayText.text="Welcome all of u in the world of game and have a wondeful time!"
    check_player_words_length()
    PlayerText.grab_focus()
func set_current_story():
    randomize()
    var stories = $StoryBook.get_child_count() # is used for how many children u have as here are 4 chilren 0, 1, 2, 3.
    var selected_story=randi()%stories
    current_story.prompts= $StoryBook.get_child(selected_story).prompts
    current_story.story= $StoryBook.get_child(selected_story).story
#    current_story=template[randi()% template.size()]

func _on_PlayerText_text_entered(new_text):
    add_to_player_words()

func _on_TextureButton_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_length()

func is_story_done():
    return player_words.size() == current_story.prompts.size()

func check_player_words_length():
    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()
    $VBoxContainer/HBoxContainer/Label.text="Again!"
    tell_story()

之后,我们将故事复制到故事0 (故事1)的属性中。

故事0

故事1

我们可以添加更多类似以上的故事。

在这里,我们还要创建两个以上Story2,story3之类的故事,并在其中编写说明。

在这里,创建了四个故事。在这里也填写故事排列。

更新代码以检查员形式编写故事时:

extends Control
var player_words=[]
var current_story
onready var PlayerText= $VBoxContainer/HBoxContainer/PlayerText
onready var DisplayText= $VBoxContainer/DisplayText
func _ready():
    randomize()
    set_current_story()
    DisplayText.text="Welcome all of u in the world of game and have a wondeful time!"
    check_player_words_length()
    PlayerText.grab_focus()

func set_current_story():
    randomize()
    var stories = $StoryBook.get_child_count() # is used for how many children u have as here are 4 chilren 0, 1, 2, 3.
    var selected_story=randi()%stories
    current_story.prompts= $StoryBook.get_child(selected_story).prompts
    current_story.story= $StoryBook.get_child(selected_story).story

func _on_PlayerText_text_entered(new_text):
    add_to_player_words()

func _on_TextureButton_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_length()

func is_story_done():
    return player_words.size() == current_story.prompts.size()

func check_player_words_length():
    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()
    $VBoxContainer/HBoxContainer/Label.text="Again!"
    tell_story()

输出:





在下一个教程中,我们将通过JSON了解StoryBook。