Overview of Game Character Attribute Management Framework
Say Goodbye to Attribute Calculation Nightmares: Analyzing the Game Character Attribute Management Framework
Still scratching your head over the jumble of Buffs, equipment, skills, and auras in game character attribute calculations? A simple speed potion is easy enough, but what about a potion that doubles the effect? Or "small flying shoes" that forcibly lock speed? Suddenly, simple addition, subtraction, multiplication, and division aren't enough. The code logic starts getting tangled, and dealing with the order of various stacking and expiry conditions makes you want to explode?
This document will guide you to face this "tough nut" problem in game development head-on. It starts with a simple "speed potion" example, showing you step by step how the "simple and crude" approach of directly modifying attribute values can easily collapse when faced with slightly more complex needs (such as doubling potion effects, stacking different effects, and fixed value overrides). You'll find yourself in a judgment hell of handling various scenarios like "applied first, takes effect later," "applied later, takes effect first," and the code becomes bloated and prone to errors.
Core Idea: "Value Comes from State"!
The core idea proposed by the author sounds a bit mysterious but is very crucial: "Value comes from state"! This means that the final movement speed, attack power, and other such "values" of a character should not be directly stored and modified by various effects; instead, they should be "calculated" in real-time based on all the "states" currently on the character (such as which Buffs are active, what equipment is worn, and what terrain they are on) through a set of rules.
Attribute Management Framework
So how exactly is this implemented? The article introduces an attribute management framework, mainly composed of the following parts:
Property Manager
Each character or entity that needs attributes has one, responsible for centrally managing all its attributes.
Property
Represents a specific attribute, such as "movement speed value" or "movement speed multiplier." It is divided into two types:
Base Property
Has an initial value (for example, read from a configuration table), and this initial value should not be arbitrarily modified by external logic.
Compute Property
Its value is not fixed but is dynamically calculated based on a custom formula, depending on one or more other properties (for example: Final Movement Speed = Base Movement Speed * Movement Speed Multiplier).
Property Modifier
This is the only thing that can affect the final calculation result of an attribute! [source: 82] Various Buffs, Debuffs, equipment effects, and skill effects are ultimately manifested as adding or removing Modifiers to the corresponding attributes. Modifiers have several typical types:
Additive: Directly increases or decreases the value (e.g., +10 Attack Power).
Multiplicative: Adjusts the value by a percentage or multiplier (e.g., * 1.5 Speed).
Clamp: Restricts the value within a certain range (e.g., HP regeneration cannot exceed maximum HP).
Override: Ignores other calculations and forcibly sets the attribute to a specific value (e.g., Small Flying Shoes force speed to 200). [source: 83, 84, 85]
Execution Order: The Key!
The system strictly follows the order of Additive → Multiplicative → Clamp → Override to apply these modifiers, ensuring clear calculation logic and controllable results. [source: 83]
In this way, no matter how many complex effects are acting simultaneously, they can be clearly decomposed into independent Modifiers, and the system can calculate the final attribute value uniformly according to the established rules. The article also mentions using a "Dirty Flag" mechanism to optimize performance, avoiding recalculating the entire complex dependency chain every time an attribute is accessed, and only recalculating when relevant states (Modifiers) change.
Summary
In summary, this method aims to provide a clearly structured, easily extensible, and error-resistant framework to help you elegantly "tame" those frustrating complex character attribute systems in games and say goodbye to spaghetti code!
Click to read the original article
Blog Submission Contact Information
Submission Email: blog@yunheculture.com
Official Website: Yunhe Culture Official Website