Code Monkey home page Code Monkey logo

Comments (5)

soraphis avatar soraphis commented on May 31, 2024

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.

jeffcampbellmakesgames avatar jeffcampbellmakesgames commented on May 31, 2024

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.

AdamRamberg avatar AdamRamberg commented on May 31, 2024

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.

AdamRamberg avatar AdamRamberg commented on May 31, 2024

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.

AdamRamberg avatar AdamRamberg commented on May 31, 2024

Added in #93

from unity-atoms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.