Godot Version
4.3 Stable
Question
I’m making an open source turn based JRPG, I want any programmer to be able to very easily create new enemies with different behaviors by simply customizing export variables in the editor, There’s currently 3 Actions the enemies can do: Attack, Strong Attack, Do nothing.
I want it so that each enemy can have a specific chance to do each action, So for example there could be a “Lazy” enemy that has an 80% chance to do nothing and a 20% chance to actually attack, Another enemy could be “Aggressive” with a 75% chance to attack and a 25% to do a strong attack.
But I’m having trouble implementing this system, I’m attempting to use @export_range and have users just increase the chances of each action happening, But that didn’t work so easily:
@export_category("Chance for actions")
@export_range(0.01, 1.00) var attackingChance
@export_range(0.01, 1.00) var strongAttackingChance
if !enemyGettingReadyForStrongAttack:
var action: float = randf()
if action <= enemyData.attackingChance:
enemy_attack()
elif action <= enemyData.strongAttackingChance:
enemy_strong_attack_prep()
else:
enemy_nothing()
else:
enemy_strong_attack()
This current system works when I only have 2 actions those being an action that can the user can manipulate the chance of + a default action, So the user can set the attacking chance to 65% and if randf() returns 0.65 or below then the enemy will attack and if it doesn’t then it’ll just default to doing nothing.
But this won’t work when more than 2 actions are active, The if statement prioritizes the 1st if and skips the other elifs if the 1st if just turns out to be true, So If I have the attacking chance be 60% and strong attacking be 40% and randf() returns 0.61 to 1.00 then the enemy will default to doing nothing, But if randf() returns 0.01 to 0.60 then the if statement will just do the normal attack and never do the strong attack since the if statement has a certain order.
I’m really not sure how to code this system ! Please tell me any ideas you have, I’m ready to just rewrite the whole system if necessary.