Getter & Setter Functions
Utilize Getter and Setter functions to interact with variables in other objects.

Utilize Getter and Setter functions to interact with variables in other objects.

Ideally, variables should not be accessed by external objects directly, as this can cause many issues depending on the purpose and implementation of the variable. This is especially true if functions need to be executed based on the changed variable.
Instead, the variables should be interfaced by established getter and setter functions, which massively helps with future extensibility and debugging.
A getter functions sole purpose is to retrieve the variables value. The value that is returned is immutable; meaning that it cannot be changed. This is an important distinction as the variable should instead be changed by the setter function.
An additional perk to retrieving the variables value from a getter function instead of the variable itself is that additional logic can be applied to that retrieval process.

/** Returns the widgets current set index. */
UFUNCTION(BlueprintPure)
int32 GetCurrentIndex();A setter functions sole purpose is to set the variables value along with trigger any additional functionality that should go along with the changed value. For example, any validation checks, debugging tests, etc.

/**
* Sets the current index of the widget.
* When updated, the child widget at that index will be visible.
* @param NewIndex - The index to switch to.
*/
UFUNCTION(BlueprintCallable)
void SetCurrentIndex(int32 NewIndex);Setting the variable to private is a pretty important aspect as you don’t want developers, including yourself, bypassing the established getter and setter functions.
To set the variable to private in blueprints, select the variable and check the private option.

To set the variable to private in C++, the UPROPERTY needs to be under the protected or private access specifiers.
If under the protected access specifier, then the variable can be changed directly in child classes. While if under the private access specifier, it can only be changed directly in the class the variable was established in.
protected:
/** The current set index of the widget. */
UPROPERTY(BlueprintReadWrite)
int32 CurrentIndex;Getter and setter functions save a lot of development time and hassle. Plus they simplify interfacing with variables from other objects.