Component Runtimes
Introduction
Components in a Gum project can be instantiated in custom code. Custom components can be created in code for a number of reasons:
To create a variable number of items, such as items in an inventory component
To create popups which may appear at any point in the app
Code Example - Instantiating a Component
Before instantiating a component from a Gum project, you must first have your Gum project loaded. For more information see the Loading a Gum Project page.
Once you have instantiated your project, you can create a component as shown in the following code:
The Components property contains a list of all components, so you can also access components by name or other property. For example, the First method can be used to find a component by name as shown in the following code:
Note that the name passed to the First method should match the name given in Gum. For example, in this case the code searches for a component named ColoredRectangleComponent.
If a component is in a folder, then use the qualified name relative to the Components folder. For example, the following component's name at runtime is "Buttons/StandardButton"
The ToGraphicalUiElement method can automatically add the component to the root for rendering, or alternatively it can be added to an existing container. If adding to an existing container, then the ToGraphicalUiElement's addToManagers parameter should be false as shown in the following code:
For more information about how to add a newly-created component runtime to managers or to a container, see the next section.
Adding a Component Runtime to a Parent
A newly-created component must be added directly or indirectly to managers. The following are the possible ways to add to managers:
Adding to a Container
The newly instantiated component can be added to a container in the screen. This container can be an actual ContainerRuntime instance, or to a control such as a ListBox.
To destroy the component, remove it from its parent, or set its parent to null:
Adding to PopupRoot/ModalRoot
The newly instantiated component can be added either the PopupRoot or ModalRoot. PopupRoot is used for popups which should appear on top of other controls, but which should not steal input. ModalRoot is used for popups which should steal focus from all other controls.
The following code shows how to create a Toast instance to display a message to a user. The code assumes your code has a valid Toast component.
The following code shows how to create a MessageBox instance and add it to the ModalRoot so it receives exclusive input.
To destroy the component, remove it from its parent, or set its parent to null:
Adding to Managers
Components can be added directly to managers. Usually items are only added directly to managers in the following situations:
If they are the root runtime. Usually this is a Screen, but it can be a component if your project is not using screens.
For testing/debugging.
If your game has multiple roots, you can add instances to a list of GraphicalUiElements which are passed to Update. This is considered an advanced scenario.
To destroy the component, call RemoveFromManagers:
Troubleshooting Component Creation
If your component is not visible, this may be a file issue. By default Gum project loading will not throw exceptions on missing files, and it will attempt to re-create missing file components. For more information, see the Troubleshooting section in the Loading .gumx page.
SetProperty
The SetProperty method can be used to set properties on components which are not natively part of GraphicalUiElement. This is useful in the following situations:
Setting a property which may not exist on all GraphicalUiElements, such as the Text property on a GraphicalUiElement for a Text standard element
Setting a property which has been exposed
If a GraphicalUiElement is a Text instance, the Text property can be assigned through SetProperty as shown in the following code:
If a component has an exposed variable, then this variable can be assigned through SetProperty. For example, consider the following component which exposes its Text variable:
This can be assigned through the SetProperty. Be sure to use the name exactly as it appears in Gum:
Apply State
States can be applied in code by string (unqualified name), or by accessing the state on the backing ComponentSave. Note, this can also be performed on screens and standards.
Last updated
Was this helpful?