Trying to wrap my head around the various solutions


Hello everyone! I just finished the "Learn to Code From Zero" course. I'm working hard to grasp the concepts in this chapter but there's definitely a leap in difficulty. I've been using ChatGPT as a free mentor and Google alternative, and it suggested a solution with two modifier variables. While I understand that the course's provided solution is more modular and has clearer code intent, would there be any significant drawbacks to using this shorter solution, aside from the need to find and replace the two _modifier variables throughout the code?

extends Resource
class_name BattlerStats

# ... (other parts of your code)

var _fixed_modifiers := {} # For fixed value modifiers
var _multiplier_modifiers := {} # For multiplier-based modifiers

# ...

func add_modifier(stat_name: String, value: float) -> int:
    assert(stat_name in UPGRADABLE_STATS, "Trying to add a modifier to a nonexistent stat.")
    var id := _generate_unique_id(stat_name)
    if value != 0.0:  # You can adjust this condition as needed
        # Check if the value is not zero (no effect)
        if stat_name not in _multiplier_modifiers:
            _multiplier_modifiers[stat_name] = {}

        _multiplier_modifiers[stat_name][id] = value

    return id

# ...

func _recalculate_and_update(stat: String) -> void:
    var base_value: float = get("base_" + stat)
    # Apply multiplier-based modifiers
    if stat_name in _multiplier_modifiers:
        var multiplier_modifiers: Array = _multiplier_modifiers[stat].values()
        for modifier in multiplier_modifiers:
            base_value *= modifier
    # Apply fixed value modifiers
    var fixed_modifiers: Array = _fixed_modifiers[stat].values()
    for modifier in fixed_modifiers:
        base_value += modifier

    base_value = max(base_value, 0.0)
    set(stat, base_value)
  • Nathan Lovato replied

    Using two variables is completely fine!  It's good to use the data structure that feels most comfortable to you.