githubEdit

ForceUpdatedependencies

Introduction

The ForceUpdateDependencies forces the calling PositionedObject and all of its parents to update absolute values according to relative values. This method can be called if an update is needed immediately (instead of waiting for the next Draw call). For more information on attachments, check the IAttachable pagearrow-up-right.

Common Usage

In many cases, if you have a parent object and a child object, you will not need to manually manage this attachment by calling ForceUpdateDependencies. However, calling ForceUpdateDependencies may be necessary in certain cases. As explained in this articlearrow-up-right, setting the relative value of a child object does not immediately change its absolute value. Similarly, changing the absolute value of a parent also does not immediately update the absolute value of a child. These updates occur right before drawing occurs. Therefore, if you have created a new attachment or modified the values involved in an attachment and would like to see these updates immediately impact the absolute value of all children, then you need to call ForceUpdateDependencies.

Code Example - Modifying child relative values

The following created two Textarrow-up-right objects. Both are attached to a PositionedObject, but only one calls ForceUpdateDependencies. Then the DisplayText is set to show each Text'sarrow-up-right absolute Y value. Notice only the one which called ForceUpdateDependencies has a non-zero Y value. Add the following using statements:

using FlatRedBall.Graphics;

Add the following to Initialize after initializing FlatRedBall:

 Text firstText = TextManager.AddText("");
 Text secondText = TextManager.AddText("");

 PositionedObject parent = new PositionedObject();

 firstText.AttachTo(parent, false);
 secondText.AttachTo(parent, false);

 firstText.RelativeY = 2;
 secondText.RelativeY = -2;
 // At this point the relative values have not
 // yet been applied to change absolute values.
 // Calling ForceUpdateDependencies forces an update
 // resulting in the absolute Y value changing.
 firstText.ForceUpdateDependencies();

 // Now set the DisplayText to show if they've been changed
 firstText.DisplayText = "My Y in Initialize is: " + firstText.Y;
 secondText.DisplayText = "My Y in Initialize is: " + secondText.Y;
ForceUpdateDependencies.png

Code Example - Modifying parent absolute values

The example above showed a situation where a child's relative values were read out immediately after an attachment was created. This example shows a similar situation - where the parent object is moved. You can comment and uncomment out the ForceUpdateDependencies call to see the difference. Add the following using statements:

Add the following to Initialize after initializing FlatRedBall:

ForceUpdateDependenciesParentReposition.png

Last updated

Was this helpful?