views:

149

answers:

2

It's me again, about my Mega Man game. I switched over to a component system so that objects could be data driven. It's going ok but I've hit a problem.

My objects have states, specified with an input file. Those states have triggers to transition them to other states. The conditions for a state change are also in the input file, and are parsed into a lambda expression. Now I need to deep copy my objects, and I need the lambdas to refer to the members of the copy and not the members of the original. The originals are loaded from the file when the program loads, but can be copied at any time after (think projectiles being fired).

Here's a greatly simplified bit of code:

class StateComponent
{
    // when the whole entity is cloned, it will get a clone of
    // DependentComponent as well as a clone of this StateComponent.
    private OtherComponent DependentComponent;

    // there is a function to register dependencies. The entity that owns
    // me also owns DependentComponent, and registered it with me.

    public StateComponent Clone()
    {
        // what should I do here to make the lambda deep copied?
    }

    public void LoadFromXml(XElement node)
    {
        State state = new State();
        LambdaExpression lambda = DynamicExpression.ParseLambda(from xml stuff)
        Delegate condition = lambda.Compile();
        Action effect = LoadTriggerEffect();
        state.AddTrigger(condition, effect);

        // add state to my list of states
    }

    private Action LoadTriggerEffect()
    {
        Action action = new Action(() => { });
        if ( some stuff from the input file )
            action += () => { DependentComponent.Foo(); DependentComponent.Bar = 5; }

        return action;
    }
}

There's more to it than that, the triggers actually cause a state change, and then the new state's initializer calls that Action, but I simplified it here.

So the problem is that when I deep copy this component, or try to anyway, I don't know how to make it so that the lambdas reference the copy's instance of DependentComponent, rather than that of the original. I have already made sure that the deep copy of the entity is getting a new DependentComponent, but the lambda is just referencing the original. Is the delegate basically locked to particular instances once created? Would I have to create a new one? I don't want to have to load the whole entity from the file again.

A: 

Why not make it an argument of the lambda?

Action<OtherComponent> action = new Action<OtherComponent>((null) => { });
if ( some stuff from the input file )
    action += x => { x.Foo(); x.Bar = 5; }

If you need more then just one dependent component, you can also pass the this pointer, if you want to interchange the lambdas between objects of different classes, use an interface ...

Stefan Steinegger
I actually figured this out and did it no less than 1 minute after posting. But I'm still interested in whether or not there is a general solution to this problem.
Tesserex
A: 

Expression trees are immutable, so if it has object references in it, they will be to the original objects. To deep-copy it, you'd need some kind of visitor with substitution; I have some similar code somewhere, but it is a lot of work. Of course, if it doesn't have object-specific references in it, you can just use it "as is" quite safely.

Assuming you mean the LambdaExpression lambda field; I'm not familiar with how you are parsing it, so I can't comment on how easy it would be to do, but a common option here is to parameterize the lambda; pass in the target object as an argument, and you can re-use the lambda with multiple different objects at runtime (as long as they are of the appropriate type).

Marc Gravell
I think he wants actually to change it. While the original lambda is accessing a local property, he wants that the copied lambda is accessing the property of the new object.
Stefan Steinegger
Exactly; so I say: have the lambda access the property of the parameter (`Expression.Parameter`), and pass `this` into the *same* (immutable and unchanged) lambda in both places.
Marc Gravell
Ok, sorry, didn't read that far ... :-)
Stefan Steinegger