Modiferty - Property Modification
Created by Hiroya Aramaki (Makihiro)
What is Modiferty ?
Modiferty is a great solution for making modifications to properties.
In games, there are often situations in which the status of characters, weapons, etc. temporarily change.
Modiferty can be used in the following situations.
- Want to modify the in-game character status temporally.
Table of Contents
Installation
Download any version from releases.
Releases: https://github.com/mackysoft/Modiferty/releases
Usage
Add "MackySoft.Modiferty" namespace into using area.
using MackySoft.Modiferty;
The following code implements a temporary increase the character attack power.
public class Character : MonoBehaviour {
public int health = 3;
public ModifiableInt attackPower = new ModifiableInt(baseValue: 1);
public void Attack (Character target){
target.health -= attackPower.Evaluate();
}
}
public class PowerUpItem : MonoBehaviour {
public AdditiveModifierInt additivePower = new AdditiveModifierInt(1);
public void Modify (Character target){
target.attackPower.Modifiers.Add(additivePower);
// Same as below.
// target.attackPower.AddModifier(additivePower);
}
}
Using ModifierList
If you want to modify the value without using ModifiableProperty, use a ModifierList.
ModifierList<int> m_DamageModifiers = new ModifierList<int>;
// Add something modifiers.
m_DamageModifiers.Add(modifier);
// Evaluate the damage.
int evaluatedDamage = m_DamageModifiers.Evaluate(damage);
ModifiableList is also used in the ModifiableProperty implementation.
Modifier Types
Basic operator modifiers are provided.
- Additive Modifier
- Subtractive Modifier
- Multiply Modifier
- Division Modifier
A variety of other unique modifiers are also available.
Set Modifier
The given value ignored and the specified value returned.
var setModifier = new SetModifierInt(0);
character.attackPower.AddModifier(setModifier);
// result is always 0.
int result = character.attackPower.Evaluate();
Create Modifier
The lambda formula allows you to improvise complex modifiers.
var createModifier = new CreateModifier<int>(value => {
int result;
// Do something process...
return result;
});
External Assets
Modiferty supports integration with some external assets.
UniRx
Install UniRx and define MODIFERTY_UNIRX
to enable integration with UniRx.
UniRx: https://github.com/neuecc/UniRx
The integration with UniRx mainly adds the following APIs to allow you to observe the values of Modiferty.
ReactiveModifierList<T>
ReactiveModifiableProperty<T>
using UnityEngine;
using UnityEngine.UI;
using MackySoft.Modiferty;
using UniRx;
public class Character : MonoBehaviour {
// Define attackPower as ReactiveModifiableProperty.
public ReactiveModifiableInt attackPower = new ReactiveModifiableInt(baseValue: 1);
. . . . .
}
public class CharacterAttackPowerUI : MonoBehaviour {
public Character character;
public Text attackPowerTsxt;
void Awake () {
// You can observe changes BaseValue and Modifiers.
character.attackPower.ObserveChanged().Subscribe(property => {
// Apply the attackPower change to the text.
attackPowerText.text = property.Evaluate();
});
}
}
Author Info
Hiroya Aramaki is a indie game developer in Japan.
- Blog: https://mackysoft.net/blog
- Twitter: https://twitter.com/makihiro_dev
License
This library is under the MIT License.