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: