반응형

C#에서 파이썬 스크립트를 실행해 보자.

 

1
2
3
4
5
import sys
 
print(sys.argv[0])
if len(sys.argv) > 1:
    print(sys.argv[1])
 

 

우선 위와 같은 파이썬 스크립트(script.py)를 하나 작성하고 저장한다

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Diagnostics;
class CSTest
{
    public static void Main()
    {
        Process process = new Process();
        process.StartInfo.FileName = "python.exe";
        process.StartInfo.Arguments = "script.py abc";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.CreateNoWindow = true;
 
        process.Start();
        process.WaitForExit();
 
        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();
 
        Console.WriteLine("■ Output:");
        Console.WriteLine(output);
        Console.WriteLine("■ Error:");
        Console.WriteLine(error);
    }
}
 

 

C# 코드를 작성하고 빌드한다.

 

실행하면 파이썬 스크립트의 결과가 출력된다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Diagnostics;
 
class CSTest
{
    public static void Main()
    {
        Process process = new Process();
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.CreateNoWindow = true;
 
        process.Start();
        process.StandardInput.WriteLine("dir/w");
        process.StandardInput.WriteLine("time");
        process.StandardInput.Flush();
        process.StandardInput.Close();
        process.WaitForExit();
 
        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();
 
        Console.WriteLine("■ Output:");
        Console.WriteLine(output);
        Console.WriteLine("■ Error:");
        Console.WriteLine(error);
    }
}
 

 

파이썬 스크립트를 가상환경에서 실행해야 하거나 복잡한 명령을 차례대로 실행해야 하는 경우 위 코드처럼 cmd를 먼저 실행하고 다른 명령을 순서대로 실행한다.

 

 

반응형
Posted by J-sean
:
반응형

다른 스크립트에 선언된 변수에 접근해 보자.

 

첫 번째 캐릭터를 생성하고 스크립트를 추가한다.

 

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

 

 

두 번째 캐릭터를 생성하고 스크립트를 추가한다.

 

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

 

 

두 캐릭터를 자식 노드로 갖는 씬을 생성한다. 두 캐릭터는 부모 자식 관계가 아닌 형제 관계로 설정한다.

 

게임을 실행하면 Output 창에 Character1의 멤버 변수 a가 출력된다.

 

반응형
Posted by J-sean
:
반응형

스크립트 작성 시 SerializeField와 Range 어트리뷰트를 함께 사용할 수 있다.

 

스크립트를 작성한다. 두 가지 방법으로 작성 할 수 있다.

 

a, b 멤버 변수를 Range에 정해진 범위 내에서 조절 가능하다.

 

반응형
Posted by J-sean
: