Data Script

The Data script (Scripts/Data.gd) can be used to store data related to the game state. Things like the player's name, event flags, etc. It is a global script, meaning it can be accessed from anywhere in the project by using 'Data.my_variable_name.'

Information should be stored in Data as variables:

var player_name: String = "Stranger";
var my_flag: bool = false;

NOTE: Every variable in the Data script must be of a type that is able to be converted to JSON by JSON.stringify(). This is because the contents of the Data script are converted into a Dictionary and stored as JSON when calling Narrare.save(). For more information on saving, see Narrare.

The Data script comes with a variable and methods related to storing and retrieving a player's inventory. These are defined in the DataBase class, which the Data script extends.

Variables

player_inventory

player_inventory: PackedStringArray;

The player's inventory. Contains a PackedStringArray of identifiers of Interactables which reference Interactables defined in the Interactables script. To add an Interactable to the player's inventory, use add_to_inventory. To remove an item, use remove_from_inventory. You can use get_inventory to access this property.

Note that Interactable in the player's inventory must be stored in the Interactables script, NOT in a Room's room_interactables property.

Functions

add_to_inventory

add_to_inventory(item_name: String) -> void

Adds an interactable name to the player's inventory.

remove_from_inventory

remove_from_inventory(item_name: String) -> bool

Removes an interactable by name from the player's inventory. Returns false if that interactable name wasn't found in the inventory, otherwise returns true.

get_inventory

get_inventory() -> PackedStringArray

Gets the player's inventory.

to_dict

to_dict() -> Dictionary

Converts this script's properties into a Dictionary. Called when using Narrare.save().

from_dict

from_dict(in_data_dict: Dictionary) -> void

Takes a Dictionary in_data_dict generated by to_dict and sets the contents of the Data script to equal the values in in_data_dict. Called when using Narrare.load().