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.