Comments (5)
instead of "hooks" (which sounds to me like 'event system'), it would be possible to make variables contain a list of "value preprocessors".
when a variable is set to a new value, the new value is modified by those preprocessors first.
an example implementation to demonstrate this:
public abstract class ValuePreProcessor<T>{
public abstract T Process(T value);
}
public abstract class FloatValuePreProcessor : ValuePreProcessor<float>{}
public abstract class ScriptableVariable<T> : ScriptableObject{
private T value;
public T Value{
get{ return value; }
set{
var val = value;
for(int i = 0; i < preProcessors.Length; ++i){
val = preProcessors.Process(val);
}
this.value = val;
}
}
public abstract ValuePreProcessor<T>[] PreProcessors{ get; set; };
}
public class FloatVariable : ScriptableVariable<float>{
public FloatValuePreProcessor[] preProcessors;
protected override ValuePreProcessor<float>[] PreProcessors {
get => preProcessors;
set => preProcessors = (FloatValuePreProcessor[])value;
}
}
public class ClampPreProcess : FloatValuePreProcessor{
public FloatReference minValue;
public FloatReference maxValue;
public override float Process(float value){
return Mathf.Clamp(value, minValue.Value, maxValue.Value);
}
}
as seen in this example, the ClampPreProcessor uses FloatReferences, which means those could be FloatVariables too. which allows for an pretty high modularity.
only problem i can see so far: if the preprocessor is changed, it does not change anything on the values where the preprocessor is currently used
from unity-atoms.
I think a simpler implementation of this for numeric values would be to add new fields on each closed numeric scriptable variable type for asserting that the value should be constrained, the min/max of the range, and then SetValue
would use these internally when setting the value to clamp it to the desired range. It would require a small amount of additional code per type, but this would be mostly a copy-paste with maybe some slight variation. Adding another OOP level to SetValue
for processing simple value types seems like overkill.
from unity-atoms.
I really like @soraphis solution - it's flexible and extendible. ValuePreProcessor should be an array of GameFunctions though like this:
public abstract class ScriptableObjectVariable<T, E1, E2, GF> : ScriptableObjectVariableBase<T>,
IWithOldValue<T>,
ISerializationCallbackReceiver
where E1 : GameEvent<T>
where E2 : GameEvent<T, T>
where GF : GameFunction<T, T>
{
...
[SerializeField]
private GF[] _preProcessors;
...
}
from unity-atoms.
I'm updating the issue since it's quite old.
My thoughts going forward
I still think that the preprocessor solution would be really nice, although I think the name should be _valueTransformers
instead of _preProcessors
. I think that the implementation should look something like this:
public abstract class AtomVariable<T, E1, E2, F> : AtomBaseVariable<T>,
ISerializationCallbackReceiver
where E1 : AtomEvent<T>
where E2 : AtomEvent<T, T>
where F : AtomFunction<T, T>
{
[SerializeField]
private F[] _valueTransformers;
}
We should then loop through the transformers before we apply the value.
I would be really appreciate someone making a PR on this. Make sure to read the guidelines before starting + assign yourself (or ask me) to the issue. Also make sure to update the templates for the generator and also regenerate all the existing Atoms.
from unity-atoms.
Added in #93
from unity-atoms.
Related Issues (20)
- [BUG] npm run generate:docs throws error HOT 3
- [FEATURE] MonoHooks should be able to call instancer-events HOT 2
- [Documentation] Asset based Generators + asmdefs
- [BUG] Replay buffer size is not visible in inspector HOT 2
- StringReference property drawer not working HOT 4
- [BUG] FSM not compatible with disabled domain reload HOT 11
- [BUG] Vector2Refrence inspector doesn't work HOT 1
- [BUG] Trying to create atoms for an interface creates flow of editor errors HOT 5
- [BUG] Atom search is rendering with strange padding and red text HOT 1
- [BUG] Generator asset fails to generate some `UnityEngine` types. HOT 2
- [Documentation] Add section about Pre Change Transformers HOT 5
- [FEATURE] Tag Package, Events HOT 1
- [FEATURE] Upgrading Documentation to Docusaurus 2.x and adding a search plugin
- [BUG] NullReferenceException in HandlePlayModeStateChange HOT 1
- [BUG] AtomValueList keeps runtime modifications HOT 1
- [BUG] AtomValueList not compatible with disabled domain reload HOT 1
- [FEATURE] Improve Inspector legibility
- [BUG] FiniteStateMachine not firing events anymore HOT 2
- A number of issues with AtomLists/AtomCollections HOT 2
- [FEATURE] More public get-properties for fields HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unity-atoms.