D5/Autoload/[文档]GameState 数据调用说明.md
2025-05-09 15:40:47 +08:00

7.4 KiB

GameState 数据调用说明

GameState 是一个 Autoload 单例,用于存储游戏运行时的所有动态数据。它通过 InitializationManager 在新游戏开始或加载存档时进行初始化。

数据结构

GameState 内部使用一个大的 Dictionary 来存储所有状态。数据主要分为两部分:

  1. 基础状态 (来自 Init_Base.json): 这些数据直接存储在 GameState 的顶层。
  2. 任务开发相关状态 (来自 task_development.json): 这些数据被组织在 GameStatetask_development 键下。

1. 基础状态 (顶层键)

这些键直接对应 Data/Init_Base.json 中的内容。

  • level (String): 当前关卡标识符 (例如: "level_0")。
  • time (Dictionary): 游戏内时间。
    • year (int)
    • month (int)
    • week (int)
    • day (int)
  • money (int): 玩家当前拥有的资金。
  • idea (int): 玩家当前拥有的创意点数。
  • fans (int): 玩家当前的粉丝数量。
  • staff_count (int): 当前员工数量。
  • system_status (Dictionary): 系统状态标志。
    • game_pause (bool): 游戏是否暂停。
    • is_on_task (bool): 是否正在执行某个任务。

访问示例:

# 获取当前资金
var current_money = GameState.get_value("money")
print("Money: ", current_money)

# 获取当前年份
var current_year = GameState.get_value("time.year")
print("Year: ", current_year)

# 检查游戏是否暂停
var is_paused = GameState.get_value("system_status.game_pause")
if is_paused:
	print("Game is paused.")

2. 任务开发状态 (task_development 键)

所有与游戏开发任务相关的数据都存储在这个字典下。其结构源自 Data/task_development.json

task_development 包含以下子字典:

  • platforms (Dictionary): 游戏平台信息。
  • gameplays (Dictionary): 游戏玩法信息。
  • themes (Dictionary): 游戏主题信息。
  • strategies (Dictionary): 开发策略信息。
  • product_focus_points (Dictionary): 产品侧重点信息。

2.1 task_development.platforms

存储所有平台的数据。键是平台名称 (String),值是包含平台属性的字典。

  • 结构: Dictionary[String, Dictionary]
  • 示例键: "台式 & 笔记本", "Switch", "PS", "XBOX"
  • 子字典属性:
    • enabled (bool): 该平台当前是否可用/解锁。
    • Icon (String): 用于显示平台图标的资源标识符。
    • Maker (String): 平台制造商。
    • Sales (int): 平台销量潜力或基数。
    • Market_share (int): 市场份额百分比。
    • Cost (int): 在该平台开发游戏的成本或授权费。

访问示例:

# 获取所有平台数据
var all_platforms = GameState.get_value("task_development.platforms")

# 获取 "Switch" 平台的成本
var switch_cost = GameState.get_value("task_development.platforms.Switch.Cost")
print("Switch Cost: ", switch_cost)

# 检查 "PS" 平台是否启用
var ps_enabled = GameState.get_value("task_development.platforms.PS.enabled")
if ps_enabled:
	print("PS platform is enabled.")
else:
	print("PS platform is not enabled yet.")

2.2 task_development.gameplays

存储所有游戏玩法的数据。键是玩法名称 (String),值是包含玩法属性的字典。

  • 结构: Dictionary[String, Dictionary]
  • 示例键: "角色扮演(RPG)", "动作(ACT)", "模拟经营(SIM)"
  • 子字典属性:
    • enabled (bool): 该玩法当前是否可用/解锁。
    • Experience (int): 玩家在该玩法上的经验值。
    • Popularity (String): 玩法的流行度 ("高", "中", "低")。
    • Cost (int): 研究或使用该玩法的成本。

访问示例:

# 获取 "模拟经营(SIM)" 玩法的经验值
var sim_exp = GameState.get_value("task_development.gameplays.模拟经营(SIM).Experience")
print("SIM Experience: ", sim_exp)

# 获取所有玩法的流行度
var all_gameplays = GameState.get_value("task_development.gameplays")
for gameplay_name in all_gameplays:
	var popularity = all_gameplays[gameplay_name].Popularity
	print(gameplay_name, " Popularity: ", popularity)

2.3 task_development.themes

存储所有游戏主题的数据。键是主题名称 (String),值是包含主题属性的字典。

  • 结构: Dictionary[String, Dictionary]
  • 示例键: "现代都市", "校园青春", "魔幻奇幻"
  • 子字典属性:
    • enabled (bool): 该主题当前是否可用/解锁。
    • Experience (int): 玩家在该主题上的经验值。
    • Popularity (String): 主题的流行度 ("高", "中", "低")。
    • Cost (int): 研究或使用该主题的成本。

访问示例:

# 获取 "魔幻奇幻" 主题的成本
var fantasy_cost = GameState.get_value("task_development.themes.魔幻奇幻.Cost")
print("Fantasy Theme Cost: ", fantasy_cost)

2.4 task_development.strategies

存储所有开发策略的数据。键是策略名称 (String),值是包含策略属性的字典。

  • 结构: Dictionary[String, Dictionary]
  • 示例键: "快速迭代", "精品打磨", "口碑营销"
  • 子字典属性:
    • enabled (bool): 该策略当前是否可用/解锁。
    • Description (String): 策略的描述。
    • Effect (String): 策略效果的简述。
    • Cost (int): 采用该策略的成本。

访问示例:

# 获取 "口碑营销" 策略的描述
var word_of_mouth_desc = GameState.get_value("task_development.strategies.口碑营销.Description")
print("Word of Mouth Strategy: ", word_of_mouth_desc)

2.5 task_development.product_focus_points

存储产品开发侧重点的数据。这是一个嵌套字典。

  • 结构: Dictionary (包含 "Gameplay", "Graphics", "Story", "Sound" 等键)
  • 子字典属性 (例如 "Gameplay"):
    • name (String): 侧重点名称 ("玩法")。
    • description (String): 描述。
    • weight (float): 权重 (影响开发点数分配等)。
    • current_level (int): 当前投入的等级或点数。
    • max_level (int): 最大等级或点数。

访问示例:

# 获取 "画面" 侧重点的当前等级
var graphics_level = GameState.get_value("task_development.product_focus_points.Graphics.current_level")
print("Graphics Focus Level: ", graphics_level)

# 获取所有侧重点的权重
var focus_points = GameState.get_value("task_development.product_focus_points")
for focus_key in focus_points:
	var weight = focus_points[focus_key].weight
	print(focus_key, " Weight: ", weight)

注意事项

  • 通过 GameState.get_value(key_path) 获取的数据通常是原始数据的副本或值类型,修改它们不会直接影响 GameState 内部存储。
  • 要修改 GameState 中的数据,应使用 GameState.set_value(key_path, new_value) 方法。
  • key_path 支持点号 (.) 分隔的路径来访问嵌套字典的值。
  • 在访问数据前,最好先通过 GameState.has_value(key_path) 检查路径是否存在,以避免潜在错误。
# 安全地增加资金
var current_money = GameState.get_value("money")
GameState.set_value("money", current_money + 1000)

# 安全地增加 "Switch" 平台的市场份额
var switch_path = "task_development.platforms.Switch.Market_share"
if GameState.has_value(switch_path):
	var current_share = GameState.get_value(switch_path)
	GameState.set_value(switch_path, current_share + 1)
else:
	printerr("Cannot find path: ", switch_path)