Node Access
In this chapter you'll learn how to access the Node C++ Script is assigned to, interact with it and access other nodes in the scene.
Accessing Self
Unlike languages like C# and GDScript, Jenova C++ Script does not extend classes but acts as a top-level layer and due to this design you cannot access the Node
C++ Script is running on using the this
keyword.
Instead, Jenova provides a more performant way using the Caller
object.
struct Caller
{
// Script Caller
const godot::Object* self;
};
By using Caller
object you can simply access the Node
script instance is running on like this :
void OnReady(Caller* instance)
{
auto self = (Camera3D*)instance->self;
Output("OnReady Called on Node : %s", GetCStr(self->get_name()));
}
You can also use the Helper Template function GetSelf<T>()
for smaller code :
void OnReady(Caller* instance)
{
auto self = GetSelf<Camera3D>(instance);
Output("OnReady Called on Node : %s", GetCStr(self->get_name()));
}
How It Works
You may have noticed a difference in the callback function definition. Using Caller
is optional and should only be added if you need to access the Node
script is assigned to.
You can add Caller*
as first parameter to any function within Script Block, Then you can use get access to the Node
script is running on.
Here's some examples :
// Without Instance Passing
void OnProcess(double delta)
{
}
// With Instance Passing
void OnProcess(Caller* instance, double delta)
{
}
// Without Instance Passing
void OnButtonClick(String targetPage, double fadeSpeed)
{
}
// With Instance Passing
void OnButtonClick(Caller* instance, String targetPage, double fadeSpeed)
{
}
Using jenova::sdk::Caller
requires JenovaSDK, Do not forget to include JenovaSDK.h
header file.
Accessing Scene
Since there's no this
in Jenova C++ Scripts, JenovaSDK provides utilities to easily obtain SceneTree
and Node
objects within a scene.
Scene Tree
To obtain SceneTree
in your C++ Script you can simply use GetTree()
utility function from JenovaSDK :
// On Ready Callback
void OnReady(Caller* instance)
{
SceneTree* sceneTree = GetTree();
Output("Scene Frame : %lld", sceneTree->get_frame());
}
Don't forget to include Godot/classes/scene_tree.hpp
header file in your Script when using GetTree()
function.
Scene Nodes
Similar to Scene Tree, You can quickly obtain Nodes in the scene by their path using the JenovaSDK GetNodeByPath()
utility function or you can use the Helper Template function GetNode<T>()
for smaller code :
// On Ready Callback
void OnReady(Caller* instance)
{
auto playButton = GetNode<Button>("Main Menu/UI/Button");
playButton->set_text("Play!");
}
Finding Nodes
If you need to find a node with unique name you can easily obtain it using the JenovaSDK FindNodeByName()
utility function or you can use the Helper Template function FindNode<T>()
for smaller code :
// Get Main Player Node from Scene
CharacterBody3D* GetMainPlayer()
{
Node* worldNode = GetWorld();
return FindNode<CharacterBody3D>(worldNode, "Game-Main-Player");
}
Nested Extensions
While developing Nested Extensions in the Jenova Framework, You won't need these methods. Since Nested Extensions are class-driven you can use the this
keyword and directly access SceneTree
and Node
objects from the GDExtension API.
Besides the methods explained above, You can also directly access scene nodes by exposing a NodePath
property from your C++ Script similar to Unity Engine.