Thursday, May 23, 2019

References Vs Pointers

Pointers

  • Points to a memory address
  • Can be reassigned
  • Can be null or nullptr
  • Access its contents by using a * in front of the pointer I.E. *ActorPtr
  • Access the address like so ActorPtr
  • You can change the address of the pointer like so ActorPtr = &Actor
  • You can change the value of the pointer like so *ActorPtr = Actor

References

  • Stores a memory address
  • Cannot be reassigned
  • Cannot be null and must be initialised
  • You can access the contents of a reference like so ActorRef
  • You can access the address of a reference like so &ActorRef
  • Since a reference cannot be reassigned, you cannot change its memory address
  • You can change the value of the reference like so ActorRef = Actor


The & And * Symbols In Context

When using a pointer and there is a * in front of it, the * allows you to access the content of the pointer, this is also known as dereferencing. An example of this is CopyOfActor = *ActorPtr. When using a variable/reference and a & is in front of it, you are accessing the address of the reference. For example, ActorAddress = &Actor.

However when both symbols have different meaning when used with a type to declare a variable. With the * symbol you are declaring a pointer to some type, for example, UActor* ActorPtr. This means you have created a pointer that will point to a UActor. With the & symbol you are declaring a reference to some type, for example, UActor &ActorRef. This mean you have creaated a refence to a UActor.


Example Demonstration Code

Visit - https://onlinegdb.com/ByP5XeNa4 and run the code.


Pointers Or References?

  • The golden rule is to use references unless you can't
  • References are newer and safer
  • Pointers provide back-compatibility
  • Pointers are more powerful
  • Pointers are more dangerous

"With Great Power Comes Great Responsibility" - Uncle Ben


Thursday, May 9, 2019

UE4: Properties

Coming from a Unity background, you can have private variables in a class but by assigning various properties like [SerializedField] or [Multiline] we can modify them in the inspector in some way (see post here). The Unreal Engine has a similar method that uses the UPROPERTY() macro, which can be used to do similar things.

For example, the UPROPERTY(VisibleAnywhere) allows the variable to show up in the inspector but is not modifiable, whereas the UPROPERTY(EditAnywhere) allows the variable to be visible in the inspector but also can be modified.

To use the UPROPERTY() macro in a class you must use the macro above the variable in question and make sure that there are no white spaces between UPROPERTY and the brackets. An Example is below.

class SomeClass
{
public:
    SomeClass();

private:
UPROPERTY(EditAnywhere)
FRotator openRotation = FRotator(0.f, 90.f, 0.f);
}

There is more detail on each of the specifiers which can be found here.

Unity C#: Properties

Unity allows developers to use property attributes to modify how a class instance looks within the inspector. Here are some of them and how they work.

[Header("Some Header")]
Header allows you to title a set of variables so if you had a string for the characters name and a string for their description you can place a header attribute above them to signal to anyone that those variables are specifically for a character's description, then use another header attribute for the character's movement stats.

[Tooltip("Some Tip")]
Tooltip allows you to display a tip within the inspector to help explain any variable viewed in the inspector. This is especially useful if the naming of the variable is confusing or could possibly be confused with another variable.

[ContextMenu("Some Menu Name")]
Context Menu allows you to call a function defined within the class from the inspector and can be used to check the functionality of a function without going into play mode in the editor. This attribute is placed above the function that would be called. An example of this would be a TakeDamage() function that in game would be called if the player/character gets hit. Adding a context menu attribute allows that TakeDamage function to be visible in the inspector and allows you to check that the function works as expected due to being able to see the characters health decreasing when called.

[Multiline]
The multiline attribute modifies the text field of a string to be a text area instead, providing more room to write and as the name suggests use multiple lines. This attribute is useful for things such as character/level descriptions.

[Range(min, max)]
The range attribute creates a slider for the variable attached to and requires a min value and a max value. An example use of this would be for a damage per second variable, where you can adjust that value using the slider instead of typing a value in.

[ContextMenuItem("Some Menu Item Name", "Some Function Name")]
Context Menu Item works similar to the context menu attribute but is specific to a variable aka the one below said attribute. This will then call a specified function when clicked on within the inspector. An example of this would be a characters health, which has a context menu item that calls the TakeDamage function from earlier. Another could be added that can then be used to call a GainHealth function, which gives the character more health.

[CreateAssetMenu(fileName = "Some File Name", menuName = "Some Menu Name", order = Some Number]
Create Asset Menu is particularly useful when creating scriptable object classes. This attribute allows you to create a scriptable object file within the project view. This attribute goes above the class definition.
For Example:
[CreateAssetMenu(fileName = "Gun Stats", menuName = "ScriptableObject/Guns/Stats", order = 1]
public class GunStats : ScriptableObject
{
  public float reloadRate;
}

[Space]
The space attribute added a gap between two variables being displayed within the unity inspector.