Here are 3 integers and 1 bool.

int a;
        int b;
        int c;
        bool bHasChangedValue;

The boolean value bHasChangedValue depends on the value of b changing.

If I update b each frame

public void Update()
        {
            a = b;

            b += c;

            bHasChangedValue = a != b;
        }

bHasChangedValue becomes true if b has changed.

A problem occurs if I set the value of b outside the Update method though.

public void SetB(int value)
        {
            if (b == value)
            {
                return;
            }

            b = value;
            bHasChangedValue = true;
        }

Because b has changed, bHasChangedValue is set to true. When the update is called, if c is 0 then bHasChangedValue will be set to false, which isn't correct in this case.

How can I keep the bHasChangedValue as true for one Update if it was set outside Update?

Recommended Answers

All 10 Replies

You could replace your SetB method with a property to access b, that way you can guarantee that the flag gets set regardless of where you change b from:

bool bHasChanged = false;
int a;
int _b;
public int b
{
   get { return _b; }
   set 
   {
       bHasChanged = _b != value;
       _b = value;
   }
}
int c;

public void Update()
{
    a = b;
    b += c;
}

You continue to use "b" as normal but instead of the variable it is now the property you are calling. If the value set to _b changes then the flag gets set.

That idea got me thinking. Image if other parameters would also affect this boolean value besides b.

I thought about using a logical OR when setting the boolean value

bHasChangedValue |= a != b;

        public void SetB(int value)
        {
            if (b == value)
            {
                return;
            }

            b = value;
            bHasChangedValue |= true;
        }

The boolean would then be accessed via a property

public bool BHasChangedValue
        {
            get
            {
                bool currentValue = bHasChangedValue;
                bHasChangedValue = false;
                return currentValue;
            }
        }

The problem with this is that the boolean will be reset when accessed.

Can you think of a more elegant solution to this problem?

Im not sure what you are trying to acheive. When would you want the flag to be reset? What is the flag going to be used for?

If I understand what you are trying to do, you could reset the flag when b is accessed:

private int myB;
public Boolean bHasChangedValue;

public int B {
    get {
        bHasChangedValue = false;
        return myB;
    }

    set {
        if (b != value) {
            b = value;
            bHasChangedValue = true;
        }
    }
}

The flag is going to be used to determine whether an object has been scaled, rotated or translated this frame.

If true it will allow the world transform to be reconstructed.
If true it will also mean that an object will need to be reassigned a node in a bounding volume hierarchy.

EDIT:

Momerath

If it reset when accessed then it will return false if it is accessed more than once per frame. Even though it is still true for the rest of the frame. This was also my approach, but I am not happy with it.

Then you need to change the boolean when the frame ends.

Hmm, so at what point would you want to reset the flag? Momerath's code will reset the flag when you read the value of b, or your code works to reset the flag when you read its value. If neither of these are appropriate, can you discribe when you want the flag set back to false?

The flag would have to be reset at the start of each frame. The alternative would be to reset it after all the draw calls had been made.

I was hoping to avoid a ResetAllFlags method if possible.

If you have several flags that need to be reset you might considered using a BitArray.
This has as SetAll method that will set all to flags to true or false.
The only problem with this is having to keep track of which index corresponds to which flag.

If you want to use a collection of related flags then you could always look into an bit flag enumeration. You can set and check multiple values on a single enumeration using binary operators.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.