반응형

게임 전체에서 간단히 사용할 수 있는 전역 클래스 멤버 변수를 만들어 보자.

 

전역 클래스 멤버 변수를 저장할 스크립트(Global.cs)를 생성한다. Template: Object: Empty

 

1
2
3
4
5
6
using Godot;
 
public partial class Global : Node
{
    public static int a = 10;
}
 

 

 

어떤 씬에도 소속되지 않은 스크립트(Global.cs)가 생성 된다.

 

노드를 하나 생성하고 전역 변수를 사용할 스크립트를 추가한다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using Godot;
 
public partial class Script : Sprite2D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        GD.Print(Global.a);        
    }
 
    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
    }
}
 

 

 

Project Settings - Autoload 에 Global.cs 스크립트를 추가한다.

Autoload에 추가되는 스크립트나 씬은 게임이 시작되면 자동으로 로드된다.

 

게임을 실행하면 Output 창에 Global.a 값이 출력된다.

 

게임이 실행 중인 상태에서 Remote 탭을 확인하면 Global 스크립트가 root 노드 아래 로드되어 있다.

 

※ 참고

Singletons (Autoload)

 

반응형
Posted by J-sean
: