Access GC Script Components
How to access GC script components from your own scripts using C#
The documentation has moved to: https://mitschmr-studios.io/documentation/api-guides/accessgcscriptcomponents.html
This version will no longer be updated and maintained.
In this tutorial, I want to start with accessing a few script components of Game Creator. These are:
- Character Player
- Stats
- Actions
Before we start, there are some things to do.
- Install Unity
- Install IDE (I use Visual Studio 2019)
- Import Game Creator
- Import Stats and enable it in the Module Manager
- Enable Stats Examples in the Module Manager.
First, let's make a new folder in our project for storing the scripts. I'll name it
Scripts
. Navigate into the newly created folder and create a new C# script. As for the name, I go withUseGameCreatorComponents
. Open the script, and delete void Start()
and void Update()
, we won't use them. You also need to the following using
statements: using GameCreator.Characters;
using GameCreator.Core;
using GameCreator.Stats;
Note: Make sure that the name of the script matches the class name in the script!
Attention: Delete
Start()
, Update()
, LateUpdate()
with no content in it. Even if you don't use them, Unity processes them and generates overhead. In a game with thousands of objects and empty methods, this can cost performance!Let's add a Player to our scene. To do this, right-click in the hierarchy and select
Game Creator -> Characters -> Player
. Now, open the script with your chosen IDE. The script should look like the following: using GameCreator.Characters;
using GameCreator.Core;
using GameCreator.Stats;
using UnityEngine;
public class UseGameCreatorComponents : MonoBehaviour
{
}
There are at least two ways to access components on a gameobject. The first one is to get a public reference to the gameobject and then call
<yourVariable>.GetComponent<ComponentType>()
inside a method. The second one is to already specify the type you want to have for the public variable. Let's take a look: Method 1
Method 2
The commented line is also possible, if you don't want to specify the type of variable or are not sure what type you get. If you specifiy a variable type, the variable will throw an error if you want to assign a value of another type. For example, a float cannot get applied to an int variable.
// Method 1
public GameObject go;
void GetComponentsMethod1()
{
// var component = go.GetComponent<PlayerCharacter>();
PlayerCharacter component = go.GetComponent<PlayerCharacter>();
component.xxx;
}
Note: If you use
GetComponent<Type>()
in the Unity Editor, it will generate ~0.5 KB of garbage collection, because of the safety checks that get executed. This doesn't occur in the build version. // Method
public PlayerCharacter player;
void GetComponentsMethod2()
{
player.xxx;
}
Both ways are ok, but the second method is the cleaner way.
As you can see in the Inspector, a Player has a bunch of settings that can be modified. If you want to access them within C#, you need a reference to this component on the gameobject. This is exactly what we have done above. For example, let's read and set the value of
IsControllable
. public PlayerCharacter player;
void GetSetComponentCharacterPlayer()
{
bool isControllable = player.IsControllable();
player.characterLocomotion.isControllable = false;
}
player.IsControllable()
is a method with a return value of type bool. This returns the current value of the IsControllable checkbox. You can set the value of IsControllable if you access the characterLocomotion
field of the PlayerCharacter. There are also the fields for canRun
, canJump
,runSpeed
, faceDirection
and many more. For accessing the stats component, let's add it to our player in the inspector. Then, as said above, you have to ways to access the component by script.
Method 1
Method 2
// Method 1
public GameObject go;
void GetComponentsMethod1()
{
// var component = go.GetComponent<Stats>();
Stats component = go.GetComponent<Stats>();
component.xxx;
}
// Method
public Stats playerStats;
void GetComponentsMethod2()
{
playerStats.xxx;
}
Let's access the attribute
health
and the stat level
from the Stats examples and set it to something new. For this, you need the methods GetAttrValue()
, SetAttrValue()
, GetStat()
and SetStatBase()
. There are also many more methods to use. public Stats playerStats;
void GetSetComponentStats()
{
float health = playerStats.GetAttrValue("health");
playerStats.SetAttrValue("health", 30);
float level = playerStats.GetStat("level");
playerStats.SetStatBase("level", 5);
}
There are not many things you can do with an
Actions
component, but if you are curious enough, you can even get the title of the current executing action. For this tutorial, I'll only cover the topic on how to execute an action. As always, get a reference to the
Actions
component. Then you simply do <variable>.Execute();
.public Actions actions;
void ExecuteActions()
{
actions.Execute();
}
This
<variable>.Execute()
is also applicable on buttons , where a OnClick
event executes certain things, and the Call Methods
action. You reference the actions object and select Execute()
. 