个性化阅读
专注于IT技术分析

故事书-Godot中的JSON用法

JSON(JavaScript对象表示法)是一种轻量级的数据可互换格式。读写很简单。机器很容易解析和生成。它基于JavaScript编程语言(标准ECMA-262第3版, 1999年12月)的子集。JSON是一种完全独立于语言的文本格式, 但是它使用C语言家族的程序员所熟悉的约定, 并且还包括C, C ++, C#, Java, JavaScript, Perl, Python等。这些属性使JSON成为理想的数据交换语言。

故事书-Godot中的JSON

JSON建立在两种结构上:

  • 在各种语言中, 它都实现为对象, 记录, 结构, 字典, 哈希表, 键列表或关联数组。名称/值对的集合。
  • 值的有序列表。在最大的语言中, 这实现为数组, 向量列表和序列。

这些是通用数据结构。几乎所有现代编程语言都以一种形式和另一种形式支持它们。有道理的是, 也基于结构与编程语言交换的数据格式。

在JSON中, 它们采用以下形式:

对象是一组无序的值/名称对。对象以{(左括号)开始, 以}(右括号)结束。每个名称后跟有:(冒号), 而名称/值对则由(逗号)选择。

数组是值的有序集合。数组以[(左大括号)和](右大括号)开头。值之间用(逗号)分隔。

JSON的基础

[值1, 值2 , ?]

{“键1″:值, “键2″:值?}

步骤

  • 创建一个JSON文件
  • 打开文件(也读取, 解析并关闭文件)

我们现在有一个开放的Godot引擎。

首先, 转到res://, 然后在文件管理器中单击”打开”。

故事书-Godot中的JSON

然后, 该文件将打开。

故事书-Godot中的JSON

然后在这里我们使用ATOM IDE。

首先, 安装Atom IDE, 然后对其进行处理。

Atom是适用于macOS, Linux和Microsoft Windows的免费开放源代码文本和源代码编辑器, 支持由Node.js编写的插件以及由GitHub开发的嵌入式Git Control。 Atom是使用Web技术构建的桌面应用程序。

ATOM IDE改进了语言集成。 ATOM用于获取更智能的上下文感知自动完成功能, 代码导航功能(如大纲视图), 进行定义, 查找所有引用以及悬停到, 显示信息, 诊断(错误和警告_)和文档格式。

首先, 进入ATOM的官方网站(atom.io)进行下载。

故事书-Godot中的JSON

我们可以下载以单击”下载”按钮。

这是一个免费的开源想法, 我们可以使用Visual Studio Code。打开atom并保存该文件夹, 以免重新打开该文件夹。

故事书-Godot中的JSON

保存后, 我们必须回到Godot游戏引擎中的脚本选项卡。我们在脚本中创建的故事(在上一教程中已对此进行了评论), 在这里我们不必对其进行注释。然后将其粘贴到ATOM IDE中。

故事书-Godot中的JSON

然后再次将其从Godot的脚本选项卡中删除。

之后, 我们必须在此处从JSON添加一些内容。

例子:

extends Control
var player_words=[]
var current_story = {}

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

func _ready():
	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():
	var stories = get_from_json("StoryBook.json")
	randomize()
	current_story= stories[randi()%stories.size()]
	
	#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 get_from_json(filename):
	var file = File.new()
	file.open(filename, File.Read)
	var text= file.get_as_text()
	var data= parse_json(text)
	file.close()
	return data 

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()

输出如下:

故事书-Godot中的JSON
故事书-Godot中的JSON
故事书-Godot中的JSON
故事书-Godot中的JSON
故事书-Godot中的JSON

在下一个教程中, 我们将学习如何在Godot中导出项目。


赞(0)
未经允许不得转载:srcmini » 故事书-Godot中的JSON用法

评论 抢沙发

评论前必须登录!