0

I want to create a dynamic conditional statement.

class Condition
{
    targetObject;   //Class or Struct or anythings..
    targetMemberVariable;  //the Member in targetObject

    conditionFilter;
    //It depends on the type of targetMemberVariable.
    //targetMemberVariable is Float type,
    //then conditionFilter automatically becomes a Float type. 
    //No need to change it in "Inspector"

    comparisonValue;
    //This is based on the type of conditionFilter.
    //If it is a float type, the "Inspector" takes a float as its value.

    Public bool CheckCondition(object targetObject)
    {
        //It checks with the above information and returns a true value.
        return true or false;
    }
}

I want to get the Unity Editor or C # library keywords.

The intention is to create something like the above.

  1. Interactive elements in the game are likely to change.
  2. In-game elements can be expanded or added.
  3. I do not want to modify conditional statements for each change or extension.

example

ex1 ex2 I remember seeing the library used in dynamic compilation in C # or after adding classes during execution.

Of course I know that dynamic is not universal. I want to do something like the validation of StarCraft Galaxy Editor.

Here is a game example

[Bleeding shot]
Give 10 damage to enemies.
If the enemy's Health is below 20, it adds 10 damage.

unit,health,amount,<=(below)
true-> adds 10 damage.
false -> none.


[Hooking]
If the enemy is enemy, it gives 5 damage and pulls to the front.
If the target is ally, pull to the front and reduce cooldown to 5 seconds.

unit,Tag,Enemy(enum),==
true-> 5 damage and pulls to the front.
false-> pull to the front and reduce cooldown to 5 seconds.

[Healing the Moon]
Heal the target by 10.
If the current time is night, heal 10 more.

GameTimer,DayNight(Enum),Night,==
true->heal 10 more.
angdroid
  • 81
  • 6

1 Answers1

3

Making this generic would probably help since the type being tested varies. This way your CheckCondition method is type safe.

You could start with just the interface. Down the road you might want a condition that's more complex than just comparing a particular member to some threshold.

public interface ICondition<T>
{
    bool CheckCondition(T subject);
}

I suspect that in most cases you'll find it easier to just check the condition explicitly than to store member information in the class an use reflection.

For example,

public class SomeClass
{
    public int Value { get; set; }
}

public class MinimumValueCondition : ICondition<SomeClass>
{
    public bool CheckCondition(SomeClass subject)
    {
        return subject.Value > 5;
    }
}

It's really easy to read, no reflection required. If some data required to evaluate the condition is variable, you could pass it in.

public class MinimumValueCondition : ICondition<SomeClass>
{
    private readonly int _minimumValue;

    public MinimumValueCondition(int minimumValue)
    {
        _minimumValue = minimumValue;
    }

    public bool CheckCondition(SomeClass subject)
    {
        return subject.Value >= _minimumValue;
    }
}

You could also write a condition that takes a function as an argument.

public class Condition<T> : ICondition<T>
{
    private readonly Func<T, bool> _conditionFunction;

    public Condition(Func<T, bool> conditionFunction)
    {
        _conditionFunction = conditionFunction;
    }

    public bool CheckCondition(T subject)
    {
        return _conditionFunction(subject);
    }
}

That way you could create a Condition from an expression or method.

var notZeroCondition = new Condition<SomeClass>(subject => subject.Value != 0);
var someOtherCondition = new Condition<SomeClass>(SomeEvaluationMethod);
Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
  • can i get this information? >>> 'SomeClass.GetPropertiesList' arrayList or anyList of information about Members. – angdroid Jul 07 '17 at 18:30
  • Are you asking for details about `SomeClass`? It's just a class I made up for the example. Or are you asking something else - I don't understand. – Scott Hannen Jul 07 '17 at 18:31
  • for example: Class Unit has [int] Health, [int] Atk, [bool] isAttack. in Code 'Unit.GetGetPropertiesList()'. then i can get informationsCollection in Array. – angdroid Jul 07 '17 at 18:35
  • Sorry, I'm still not following - is that part of this question or a different question about how to list the properties of a type? – Scott Hannen Jul 07 '17 at 18:37
  • Thank you very much for the answer. I got the keyword without any questions while the questions were coming and going. – angdroid Jul 07 '17 at 18:40
  • https://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class – angdroid Jul 07 '17 at 18:40
  • I wanted to make a conditional comparison without using a kind of switch statement or if statement. Like a generic. – angdroid Jul 07 '17 at 18:42