Creating Pygame to GDScript converter

Introduction:

Hi, I’m still new to Godot, as I mostly use Pygame. I’ve become somewhat familiar with Godot and find it easy to use with its own Python-like programming language.

I already have basic setup how to convert pygame to gdscript.

Questions:

1. Any ideas what version to use?
2. Should i preload GDScript every time or only preload it once and assign to a variable? (Example 1)
3. Should i use Image, Sprite or Viewport as Surface like in Pygame?
4. Would anyone like to have functions like exec and eval based on loading external .gd files?
→ other ideas please comment

Examples:

1.

var pi_ = preload("res://math.gd").pi
var Vector2_ = preload("res://math.gd").Vector2

or

math_ = preload("res://math.gd")
var pi_ = math_.pi
var Vector2_ = math_.Vector2

I don’t use PyGame, but I may be able to help with this:

From the preload documentation:

During run-time, the resource is loaded when the script is being parsed. This function effectively acts as a reference to that resource.

The file will only be loaded once regardless of how many times you use preload. Whether or not you assign it to a variable is up to you. Do you want to type preload("res://math.gd") everywhere, or do you want to just type math_?

2 Likes

Here is my code example:

a=1
def print_(name):
    print(name)
print_("")
class Math:
    pi = 3.14
    def __init__(self):
        pass
pi = Math.pi
math = Math()

convert to

main.gd
static var STATIC_VAR := {}
static func static_set_(name_, value):
	STATIC_VAR[name_] = value	
static func static_get_(name_):
	return STATIC_VAR[name_]	
static func static_del_(name_):
	STATIC_VAR.erase(name_)
func _init():
    static_set_("a", 1)
    print_("")
    static_set_("class_Math", preload("res://Math.gd"))
    static_set_("pi", static_get_("class_Math").pi)
    static_set_("math", static_get_("class_Math").new())
func print_(name):
    print(name)
Math.gd
extends Node
static var STATIC_VAR := {"pi":3.14}
static func static_set_(name_, value):
	STATIC_VAR[name_] = value	
static func static_get_(name_):
	return STATIC_VAR[name_]	
static func static_del_(name_):
	STATIC_VAR.erase(name_)
func _init():
    pass

A lot of what you’re doing here seems to be replicating existing functionality in Godot. Which is fine if that’s what you want, but (for example) your Math.gd could just be brought in as a global file by adding it to the globals list in the project settings, and that will also get you Math.pi, though there’s also already a global constant PI, and a lot of the math functions are already included.

1 Like