Forum Archive

Pythonista Games Patterned after Godot?

TutorialDoctor

I am trying out the scene module and doing a few tests. The new Node system reminds me of the Godot game engine

I have been trying to pattern my code after this construct. A test can be found here.

So, for now I am looking for analogies. I guess I might end up creating a small API of some sort and really need some tips on how the code should be structured if I want to pattern it after the Godot game engine.

Any tips would help.

P.S. I would like it if nodes could have names, so I might have to write my own class. Also, I would need a function to traverse the node tree. I love the get_node() function of Godot, but I would like to get any node no matter how deep it is in the hierarchy.

omz

@TutorialDoctor

P.S. I would like it if nodes could have names, so I might have to write my own class. Also, I would need a function to traverse the node tree. I love the get_node() function of Godot, but I would like to get any node no matter how deep it is in the hierarchy.

I don't really know anything about Godot, but you can easily assign names to nodes. some_node.name = 'foo' will work in Python, even though name is not a standard attribute of the Node class.

Writing a helper function to find nodes in a tree is also pretty easy. Here's a little example:

from __future__ import print_function
from scene import Scene, SpriteNode, run

# A simple scene that contains a background, 
# which contains a player, which contains a key...
# Just to have a nested node hierarchy for testing.

class MyScene (Scene):
    def __init__(self):
        a = SpriteNode('plf:BG_Colored_grass', parent=self)
        a.name = 'background'
        b = SpriteNode('plf:AlienBeige_swim2', parent=a)
        b.name = 'player'
        c = SpriteNode('plf:HudKey_yellow', parent=b)
        c.name = 'key'

def find_node(root, name):
    if hasattr(root, 'name') and root.name == name:
        return root
    for child in root.children:
        n = find_node(child, name)
        if n:
            return n

# Some tests for the find_node function:
s = MyScene()
print('Player:', find_node(s, 'player'))
print('Background:', find_node(s, 'background'))
print('Key:', find_node(s, 'key'))
print('Non-existing node:', find_node(s, 'foobar'))
TutorialDoctor

Okay, thanks Ole. I will look into it.