Hello. I have experience in pygame and decided transport projects from python to godot. Almost immediately in view of the absence of good gdscript docs I got a problem with classes. For example, I have the same class in python

    def some():
    	pass

	class PriorityStack:
                def __init__(self):
                    self.elements = []
            
                def put(self, item, priority):
            
                    if self.empty():
                        self.elements.append((item, priority))
                    else:
                        for el in self.elements:
                            if priority <= el[1]:
                                self.elements.insert(self.elements.index(el), (item, priority))
                                break
                        else:
                            self.elements.append((item, priority))
            
                def get(self):
                    return self.elements.pop(0)[0]
            
                def empty(self):
                    return len(self.elements) == 0
            
                def __len__(self):
                    return len(self.elements)

And is it posiible to rewrite this class in gdscript without creating new scene? I've read that godot's script is already classes, but how can I use them? Thanks that you read this. P.S First function for coverage whole code, because this redactor wouldn't accept class header and init function

In GDScript, each file is its own class and can be used as such. You can also have subclasses in each file, though if I recall correctly, the class will only be accessible to the main class in the file. I believe classes in GDScript are not strictly required to extend something, though it is generally recommended. Looking at the code above, I would suggest extending the Resource class, so it is reference counted and can be saved to a file.

Here's how I would do it, using the code above:

class_name PriorityStack
extends Resource

var elements = []	

func _init():
	elements = []

func put(item, priority:int):
	if empty():
		# no tuple support in GDScript, unfortunately. Have to use a list/array instead.
		elements.append([item, priority])
	else:
		for el in elements:
			if priority <= el[1]:
				elements.insert(elements.index(el), [item, priority])
				break
		# I'm not sure if this else will work in GDScript. Would need to test!
		else:
			elements.append([item, priority])

# Other code here...

Then you can use it like the following:

extends Node
var stack
func _ready():
	stack = PriorityStack.new()
	stack.put("Text", 1)
	stack.put("Better text", 2)
	stack.put("Meh text", 1)
	print(stack.elements)

I have not tested any of the code above, but hopefully this helps explain how to use classes in GDScript! :smile:

Thanks for your answer, it really will help me in godot developing. And "for else" don't work in gdscript, I checked)

an extra indentation might work though if an if-else there would work.

2 years later