I am using godot v4.3 gdscript, I want to create a pure-text casual simulation dynasty game. I want to randomly create 1000 NPC (including the player) and save/load the data. The player can have offspring with other NPCs, after the player dies, the player can choose one offspring to be the new player. Other NPC can also marry with other NPC randomly, but not all NPC will get married.
Assistant
Creating a pure-text casual simulation dynasty game in Godot 4.3 using GDScript is a fun project! You want to:
Randomly generate 1000 NPCs (including player)
Simulate marriages (some NPCs marry randomly)
Offspring from player and NPCs
When player dies, choose one offspring to continue as new player
Save/load the entire game state
Suggested approach & structure
Because your game is heavily data-driven, focus on:
Data structures for NPCs
Simulation logic (marriages, offspring)
Saving/loading game data
Player switching
Step 1: Define NPC Structure
Create a script npc.gd for NPC data:
gdscript
# npc.gd
class_name NPC
extends Reference
var id: int
var name: String
var age: int = 0
var gender: String
var spouse_id: int = -1
var children_ids: Array[int] = []
var parent_ids: Array[int] = []
var alive: bool = true
func _init(id:int, name:String, gender:String):
self.id = id
self.name = name
self.gender = gender
id is unique NPC identifier.
spouse_id is the NPC's spouse (or -1 if none).
children_ids is list of children.
parent_ids is list of parents.
Step 2: Create and initialize 1000 NPCs
Put this in your main scene script or a manager node:
gdscript
var NPCCount = 1000
var npc_data: Dictionary = {}
var player_id: int
func _ready():
randomize()
generate_npcs()
simulate_marriages()
simulate_offspring()
player_id = 0 # Let's assign NPC 0 as player initially
print("Game initialized. Player is NPC id: %d" % player_id)
func generate_npcs():
var male_names = ["John", "Mike", "Adam", "Luke", "Tom"]
var female_names = ["Anna", "Lily", "Kate", "Mary", "Jane"]
for i in range(NPCCount):
var gender = randf() < 0.5 ? "male" : "female"
var name_list = gender == "male" ? male_names : female_names
var name = name_list[randi() % name_list.size()] + " " + str(i)
var npc = NPC.new(i, name, gender)
npc_data[i] = npc
This code creates 1000 NPCs with random gender and names.
Step 3: Simulate random marriages between NPCs
gdscript
func simulate_marriages():
var unmarried_males = []
var unmarried_females = []
for npc in npc_data.values():
if npc.spouse_id == -1 and npc.alive:
if npc.gender == "male":
unmarried_males.append(npc)
else:
unmarried_females.append(npc)
unmarried_males.shuffle()
unmarried_females.shuffle()
# Not all NPCs marry, so we randomly marry some percentage
var max_couples = int(min(unmarried_males.size(), unmarried_females.size()) * 0.6) # 60% marry
for i in range(max_couples):
var male = unmarried_males[i]
var female = unmarried_females[i]
male.spouse_id = female.id
female.spouse_id = male.id
This matches ~60% of unmarried males/females randomly.
Step 4: Simulate offspring (children)
We'll generate children between married NPCs, and also allow player to have offspring.
gdscript
func simulate_offspring():
var child_id = NPCCount
// For each married couple, probabilistically have children
for npc in npc_data.values():
if npc.spouse_id != -1 and npc.id < npc.spouse_id: # process each couple only once
var spouse = npc_data[npc.spouse_id]
# Random number (0-3 children)
var num_children = randi() % 4
for i in range(num_children):
var gender = randf() < 0.5 ? "male" : "female"
var name = gender == "male" ? "Son" : "Daughter"
var child = NPC.new(child_id, "%s_of_%d_%d" % [name, npc.id, spouse.id], gender)
child.age = 0
child.parent_ids.append(npc.id)
child.parent_ids.append(spouse.id)
npc.children_ids.append(child_id)
spouse.children_ids.append(child_id)
npc_data[child_id] = child
child_id += 1
NPCCount = child_id
This creates children for married NPCs.
Step 5: Player death & switch to offspring
Example function:
gdscript
func player_die():
var player = npc_data[player_id]
player.alive = false
var living_offspring = []
for child_id in player.children_ids:
var child = npc_data.get(child_id)
if child and child.alive:
living_offspring.append(child)
if living_offspring.size() == 0:
print("Player's lineage ended!")
# Could reset game or load last save
return
print("Choose a new player from offspring:")
for i in range(living_offspring.size()):
var child = living_offspring[i]
print("%d: %s (Age %d)" % [i, child.name, child.age])
# For testing, we just pick the first offspring as new player
player_id = living_offspring[0].id
print("New player is %s (id %d)" % [living_offspring[0].name, player_id])
Step 6: Save/Load NPC data
You can save your npc_data Dictionary easily using JSON or Godot's File API:
gdscript
func save_game(file_path: String):
var file = FileAccess.open(file_path, FileAccess.WRITE)
if not file:
print("Error opening file for save")
return
var save_dict = {}
save_dict["player_id"] = player_id
# Serialize NPC data as Array or Dictionary
var npc_array = []
for npc in npc_data.values():
# Convert each NPC object to Dictionary for JSON
npc_array.append({
"id": npc.id,
"name": npc.name,
"age": npc.age,
"gender": npc.gender,
"spouse_id": npc.spouse_id,
"children_ids": npc.children_ids,
"parent_ids": npc.parent_ids,
"alive": npc.alive,
})
save_dict["npcs"] = npc_array
var json = JSON.print(save_dict)
file.store_string(json)
file.close()
print("Game saved to %s" % file_path)
func load_game(file_path: String):
var file = FileAccess.open(file_path, FileAccess.READ)
if not file:
print("Error opening file for load")
return
var json = file.get_as_text()
var res = JSON.parse(json)
if res.error != OK:
print("Error parsing saved game data: %s" % res.error_string)
return
var save_dict = res.result
player_id = save_dict.get("player_id", 0)
var npcs_arr = save_dict.get("npcs", [])
npc_data.clear()
for npc_dict in npcs_arr:
var npc = NPC.new(npc_dict["id"], npc_dict["name"], npc_dict["gender"])
npc.age = npc_dict["age"]
npc.spouse_id = npc_dict["spouse_id"]
npc.children_ids = npc_dict["children_ids"]
npc.parent_ids = npc_dict["parent_ids"]
npc.alive = npc_dict["alive"]
npc_data[npc.id] = npc
print("Game loaded from %s" % file_path)
Summary
Define NPC as class to hold individual NPC info.
Generate 1000 random NPCs with names and genders.
Randomly marry some NPCs.
Create children from married NPCs.
When player dies, choose living offspring as new player.
Save/load NPC data and player id to JSON file
Additional tips
You can add functions to age NPCs, simulate events over time.
Add interaction for player during gameplay.
Use UI to show text info.
Extend NPC with traits and stats for more complexity.
If you want, I can help you put this in a Godot project with a simple text UI next! Would you like that?