A complete introduction to scripting for games using the versatile Lua programming language and the Core game development platform. Add an array of game coding skills to your toolbox and take your game development to new heights!

Referencing Game Objects

Referencing Objects

Overview

Creating a game involves building the visual and audio parts of your game, using 3D objects and materials, and then controlling their behavior, usually through scripts.

In order to be able to write that behavior, you will need a way to talk about it in code, a reference.

In this lesson, you will learn the many different ways to get references to objects in Core, and which ones to use in different circumstances.

Hierarchy Objects vs. Asset References

When you talk about a Core Object, you could be referring to two different types of things, a specific object, or a type of object of which there can be multiple copies.

This is the same concept we studied in the Classes and Objects lesson when focusing on the distinction between an instance of a class and a prototype.

  • An object in the Hierarchy is an instance. It has a position in the world.
  • Things in Project Content or Core Content are prototypes. You can add instances of them to your project.

Spawning assets

If you want to create an object from script, you will need a reference to the prototype, which is the Asset Reference. With this, you can use the World.SpawnAsset() function to make a new instance of it appear in your game

Templates

If you have modeled your own object that you want to treat like a prototype, and be able to spawn copies of in game, you will need to make it a template. You can learn more about how to do this in the Core Documentation Template Reference, where you can learn how to make, change, and update templates.

Once a group becomes a template, you can create new copies of it from the Project Content window.

Networking

If you want a script to change an object, that object will need to be networked.

Enable Networking

For an object in the Hierarchy, enable networking by right-clicking and selecting Enable Networking.

For a template:

  1. Find the template in Project Content and drag a copy of it into the Hierarchy.
  2. Right-click the template instance and select Deinstance This Object.
  3. Right-click again and select Enable Networking
  4. Right-click one more time and select Update Template From This.

Custom Properties

The easiest way to get a reference to an object is to make it a custom property of the script.

Drag an Object onto a Script’s Custom Properties

Dragging an Object onto the Custom Properties panel

Hierarchy Objects vs. Asset References

One trick to tell if a Custom Property is referring to an object in the Hierarchy or an asset is in the generated variable code. If the object is followed by :WaitForObject(), then it is already part of the Hierarchy, and we are telling Core to wait for it to load before defining the variable.

Other Uses of Custom Properties

Any variable in your code can be made into a custom property. This will allow you to modify inputs to the program by changing them on the properties of an object

Parents and Children

The Hierarchy can be arranged so that different objects scripts and folders can be nested under other ones. Those nested objects are children, and the object they are nested under is the parent.

Nested Objects in the Hierarchy

Parents

If you nest a script under an object, that object is the script’s parent. Knowing that, you can easily reference it with

local objectToReference = script.parent

You could even get the parent of that object (like if it is part of a template) with script.parent.parent!

Children

Using an object or script’s children is more complicated than getting a parent, because while an object can have only one parent, it can have many children.

There are two useful function for this:

  • GetChildren() will give you a table of every child of the object called on it.
  • FindChildByName() will allow you to give the name that shows up for the object in the Hierarchy as a string, and get a reference to it in your script.

Descendants and Ancestors

You can also find parents of parents, Ancestors and children of children, Descendants using these CoreObject functions:

  • FindAncestorByName()
  • FindDescendantByName()

MUID’s

Every object in Core has a unique ID string that you can use to find the object with the World.FindObjectByID() function.

This method has the potential to create bugs later down the line. It makes it difficult to tell what type of thing you are dealing with, and will stop working if you try to make a new copy of the object referenced, or save it as part of a template. In general, you should avoid using MUID’s in your code, unless you cannot find an alternative.

Asset References and Hierarchy Object MUID’s

Assets Core Content and Project Content and instances of objects in the Hierarchy both have ID’s. It can be easy to confuse the two, since they will not have any clear distinguishing characteristics.

Scroll to Top