# Stepping Simultaneously

## Recommended Posts

umaiuma    100
hello everyone/gamedev/community!
Apologies if it's rude to ask a question before introducing myself to the community or something >.<, please say so if it is!

Anyways, this might have been asked before, but i don't know how to refer to it T.T and so i've wasted plenty of time that would otherwise be productive searching!

But! Here's my predicament which i would hope someone here could shed light on!

so i've been (trying to) make a game in c++!
And, i have two classes, one called Object and one called Effect.

Now, effects affect objects, for example if the object was a character and the effect was to deduct health, the character would have his health deducted next step, and the effect will be "completed" next step. (currently each step occurs every 1/fps seconds).

However, i thought that this was undesirable, since if you have three effects: two which deduct the character's current health by 5% and one which deducts the health by 20 (and lets just say the character has 100 health at the very start of the step), it would matter in which order the effects act. if the two % deductions happened first followed by the 20 health deduction, the result in the end would be different to the 20 health deduction happening first! What would be more desirable here would be to deduct 5% of the original 100 twice, as well as the 20 health, leaving 100-100*0.05-100*0.05-20=70 health by the end of the step.
Or, another example would be, say a character has a effect which dispels all negative effects. The character also has an effect which dispels all positive effects. If the former acted first, the character would be left with the former, whereas if the latter acted first, the character would be left with the latter effect!
In other words, i want the end result to be independent of the order in which the Effect's step() function is called!

i thought the solution was for each object to create a "cloned reference" object. The calculations/effects would use the "cloned reference object" but the target on which the effect would be applied would be the actual object! But when i actually tried to implement this, i couldn't think of a way to only write one function in object which does this. My attempt went something along these lines:

class Object {
Object* reference;
list<Effect> effects;
public:
virtual Object* getReference() {return reference};
Object() {reference = new Object(this)};
void step() {
*iterate through the effects' step() functions, which would apply the effect on *this, but calculate according to *reference *

memcpy(reference,this,sizeof(*this));
}
}

But! since object is meant to be a superclass, inherited by such things as Character classes! The returned value from sizeof(*this) won't correspond to the actual size of a character >.<. I'm unsure of how to apply sizeof to something returned from the "typeid" function as well. I also doubt replacing memcpy with "delete reference; reference = new Object(this);" would allow the reference to keep the subclasses' values.

So, if anyone has any suggestions of alternative ways to do this, or other forms of code to do what i tried (preferably the latter, since i'm curious as to how u would go about doing this!) i'd be very grateful! since i really don't want to write a new function for every effect i make!

thanks everybodies!

##### Share on other sites
haegarr    7372
Possibility 1: Collecting the changes and apply the result as last step

[code]
delta = 0
foreach effect
delta += effect->magnitude( current );

current += delta;
[/code]

Possibility 2: Remembering current in temporary variable

[code]
temp = current
foreach effect
current += effect->magnitude( temp );
[/code]

Possibility 3: Two passes using the effect as temporary storage

[code]
foreach effect
effect->computeMagnitude( current );

foreach effect
current += effect->getMagnitude( );
[/code]

##### Share on other sites
umaiuma    100
ah, so we basically have to buffer the changes then apply them later, thanks ^^