Hi Everyone,
I’m hoping someone can help me see what is wrong with this code. This is a seed scene that randomly selects from a list to create different types of resource scenes.
That is working fine until I wanted to make it so it would add just 1 scene called ‘Castle’. So there is a bool for whether or not there has been a castle created. when the castle is create, that bool is being set to true (the #print shows it). However, when it goes back to pick and create the next scene, the bool is now false. So it is creating multiple castle scenes. Can anyone see what I am doing incorrectly here?
extends Area2D
enum ZoneType {CASTLE, WOOD, STONE, FOOD, VILLAGERS, WEAPONS, SOLDIERS, KEEPS}
var zone_type: ZoneType #= ZoneType.WOOD
var soldiers_in_zone: int = 0
var soldiers_moving_in: int = 0
var soldiers_moving_out: int = 0
var castle_soldiers: int = 50
var castle_created: bool = false
var friendly_color : Color = Color(0, 1, 0, 1) # Green
var neutral_color : Color = Color(1, 1, 0, 1) # Yellow
var enemy_color : Color = Color(1, 0, 0, 1) # Red
var Barracks = load("res://Assets/Barracks.png")
var Blacksmith = load("res://Assets/Blacksmith.png")
var Farm1 = load("res://Assets/Farm1.png")
var Farm2 = load("res://Assets/Farm2.png")
var Gatherer = load("res://Assets/Gatherer.png")
var Keep = load("res://Assets/Keep.png")
var Stone_Pile = load("res://Assets/Stone_Pile.png")
var Stone_Quary = load("res://Assets/Stone_Quarry.png")
var Timber_Mill = load("res://Assets/Timber_Mill.png")
var Townhall = load("res://Assets/Townhall.png")
var Training_Grounds = load("res://Assets/Training_Grounds.png")
var Village = load("res://Assets/Village.png")
var Wheat_Field = load("res://Assets/Wheat.png")
var Wood_Pile = load("res://Assets/Wood.png")
var Castle = load("res://Assets/Castle.png")
var zoneTypes = [ZoneType.CASTLE, ZoneType.WOOD, ZoneType.STONE, ZoneType.FOOD, ZoneType.VILLAGERS, ZoneType.WEAPONS, ZoneType.SOLDIERS, ZoneType.KEEPS]
# Maintain counters for each zone type
var zone_counters = {
ZoneType.WOOD: 0,
ZoneType.STONE: 0,
ZoneType.FOOD: 0,
ZoneType.VILLAGERS: 0,
ZoneType.WEAPONS: 0,
ZoneType.SOLDIERS: 0,
ZoneType.KEEPS: 0,
ZoneType.CASTLE: 1
}
var RNG = RandomNumberGenerator.new()
const ZONE_SIZE: int = 512
signal zone_clicked(zone)
func _ready():
# Ensure different random sequence each time the game runs
randomize()
# Ensure that there is at least one of each zone type
for type in zone_counters.keys():
zone_counters[type] = 1
# Pick a zone type based on the counters
zone_type = pick_random_zone_type()
# Ensure Castle is initialized once
if zone_type == ZoneType.CASTLE and castle_created == false:
castle_created = true
soldiers_in_zone = 50 # Set initial soldiers in the Castle
print("Castle created with 50 soldiers")
zone_counters[ZoneType.CASTLE] = 0 # Set the count to 0 to avoid re-creation
add_to_group("Zone_Click")
update_status()
set_zone_border()
update_zone_sprite()
func _process(delta):
$Soldiers_in_Zone.text = str(soldiers_in_zone-soldiers_moving_out)
###FIGURE OUT WHY CASTLE_CREATED IS NOT STAYING TRUE!!!
func pick_random_zone_type() -> ZoneType:
var zoneTypes = [ZoneType.CASTLE, ZoneType.WOOD, ZoneType.STONE, ZoneType.FOOD, ZoneType.VILLAGERS, ZoneType.WEAPONS, ZoneType.SOLDIERS, ZoneType.KEEPS]
print("Castle Status: ", castle_created)
# If Castle is already created, remove it from the zoneTypes array
if castle_created == true:
zoneTypes.erase(ZoneType.CASTLE)
print("Erased castle")
# Calculate the total count of all zones
var total_zones = 0
for count in zone_counters.values():
total_zones += count
# Calculate weights inversely proportional to the counts
var weights = []
for type in zoneTypes:
var weight = (total_zones - zone_counters[type]) + 1
weights.append(weight)
# Normalize the weights to get a probability distribution
var weight_sum = sum(weights)
var probabilities = []
for weight in weights:
probabilities.append(float(weight) / weight_sum)
# Pick a random zone type based on the probabilities
var random_value = randf()
var cumulative_probability = 0.0
for i in range(probabilities.size()):
cumulative_probability += probabilities[i]
if random_value <= cumulative_probability:
return zoneTypes[i]
var fallback_type = zoneTypes[randi() % zoneTypes.size()]
print("Fallback zone type: ", fallback_type)
return fallback_type
func sum(values):
var total = 0
for value in values:
total += value
return total
Thank you in advance.