kiriri / gd2cs.py

Regex based Python script that converts arbitrary gdscript code to C#
GNU General Public License v3.0
87 stars 12 forks source link

Good resource, please improve it #4

Closed Crogarox closed 3 years ago

Crogarox commented 3 years ago

I tried this addon with the Godot demo. I shouldn't have expected a quick fix and the results are still useful, however, its still requires effort to further convert the converted python to c# code to be free of errors. For example in player.gd .

extends KinematicBody

const CAMERA_MOUSE_ROTATION_SPEED = 0.001
const CAMERA_CONTROLLER_ROTATION_SPEED = 3.0
const CAMERA_X_ROT_MIN = -40
const CAMERA_X_ROT_MAX = 30

const DIRECTION_INTERPOLATE_SPEED = 1
const MOTION_INTERPOLATE_SPEED = 10
const ROTATION_INTERPOLATE_SPEED = 10

const MIN_AIRBORNE_TIME = 0.1
const JUMP_SPEED = 5

var airborne_time = 100

var orientation = Transform()
var root_motion = Transform()
var motion = Vector2()
var velocity = Vector3()

var aiming = false
var camera_x_rot = 0.0

onready var initial_position = transform.origin
onready var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") * ProjectSettings.get_setting("physics/3d/default_gravity_vector")

onready var animation_tree = $AnimationTree
onready var player_model = $PlayerModel
onready var shoot_from = player_model.get_node(@"Robot_Skeleton/Skeleton/GunBone/ShootFrom")
onready var color_rect = $ColorRect
onready var crosshair = $Crosshair
onready var fire_cooldown = $FireCooldown

onready var camera_base = $CameraBase
onready var camera_animation = camera_base.get_node(@"Animation")
onready var camera_rot = camera_base.get_node(@"CameraRot")
onready var camera_spring_arm = camera_rot.get_node(@"SpringArm")
onready var camera_camera = camera_spring_arm.get_node(@"Camera")

onready var sound_effects = $SoundEffects
onready var sound_effect_jump = sound_effects.get_node(@"Jump")
onready var sound_effect_land = sound_effects.get_node(@"Land")
onready var sound_effect_shoot = sound_effects.get_node(@"Shoot")

turns into

public class player : KinematicBody
{

    public const float CAMERA_MOUSE_ROTATION_SPEED = 0.001f;
    public const float CAMERA_CONTROLLER_ROTATION_SPEED = 3.0f;
    public const int CAMERA_X_ROT_MIN = -40;
    public const int CAMERA_X_ROT_MAX = 30;

    public const int DIRECTION_INTERPOLATE_SPEED = 1;
    public const int MOTION_INTERPOLATE_SPEED = 10;
    public const int ROTATION_INTERPOLATE_SPEED = 10;

    public const float MIN_AIRBORNE_TIME = 0.1f;
    public const int JUMP_SPEED = 5;

    public int airborne_time = 100;

    public Transform orientation = new Transform();
    public Transform root_motion = new Transform();
    public Vector2 motion = new Vector2();
    public Vector3 velocity = new Vector3();

    public bool aiming = false;
    public float camera_x_rot = 0.0f;

    onready var initial_position = transform.origin;
    onready var gravity = ProjectSettings.GetSetting("physics/3d/default_gravity") * ProjectSettings.GetSetting("physics/3d/default_gravity_vector");

    onready var animation_tree = $AnimationTree
    onready var player_model = $PlayerModel
    onready var shoot_from = player_model.GetNode(@"Robot_Skeleton/Skeleton/GunBone/ShootFrom");
    onready var color_rect = $ColorRect
    onready var crosshair = $Crosshair
    onready var fire_cooldown = $FireCooldown

    onready var camera_base = $CameraBase
    onready var camera_animation = camera_base.GetNode(@"Animation");
    onready var camera_rot = camera_base.GetNode(@"CameraRot");
    onready var camera_spring_arm = camera_rot.GetNode(@"SpringArm");
    onready var camera_camera = camera_spring_arm.GetNode(@"Camera");

    onready var sound_effects = $SoundEffects
    onready var sound_effect_jump = sound_effects.GetNode(@"Jump");
    onready var sound_effect_land = sound_effects.GetNode(@"Land");
    onready var sound_effect_shoot = sound_effects.GetNode(@"Shoot");

still much appreciated for the tool.

kiriri commented 3 years ago

Please be more specific with your feature requests. I noticed 2 issues in the code. 1) Child nodes referred to in the form of "$X/Y" need to be converted to GetNode("X/Y") 2) onready

I've implemented (1) , but won't implement a fix for (2). As far as I know of, there isn't a direct equivalent to gdscript's onready in c#. I could move all the assignments to the _Ready() function, and create a _Ready function if it doesn't exist already, but that seems like too much of a "Design" question for an automatic converter to handle. I've added the keyword to the list of accepted keywords though, so the automatic type and access modifier detection should work now.

I'm happy to hear the results thus far have been useful to you.