Skip to content

Quick Access to the Scene's Tree Nodes #31

Open
@perbone

Description

@perbone

The idea is to design how the scene's nodes can be accessed through aliases, reducing the amount of code needed for reaching out a descendant node, or the root of the tree, or even a sibling node, since this type of operation is widely used for the game's logic.

Keeping the Lua way of programming, every scene will have two local tables used to access the tree’s nodes. They are the _R table and the _S table and they allow us to access the root node of the tree and the current node been processed by the engine, respectively.

The root node is so named like this because it is the oldest ancestor of each node in the tree. So its table alias _R will give us access to the entire scene tree by simply referring to the name of the child node and, like wise, the child node's own children, descending from the top most parent node to the deepest child node.

The local _S table is a lot alike the _R table. The main difference is its conciseness that allows us quick access to all current node's children. That is because the scene node is in front of the root node (or below it, depending how you think about such tree), and its main purpose is to interact quickly with its children. Therefore, using the _S table to access sibling or ancestral nodes does not bring much of an advantage for the developer, in which case the _R table should be used instead.

Following below is an excerpt for some of the the designs I have so far, showing how those tables can be applied on real game logic code.

The following examples presuppose this sample tree and has the Hero node as the current node running the Lua script so that the _S table is an alias for the Hero node.

/root
/root/Hero
/root/Hero/Fighter
/root/Hero/Fighter/MachineGun
/root/Hero/HealthGauge
/root/Supplies
/root/Supplies/Ammunition
/root/FirstAidKit

local class = require 'lua.class'       -- Import the system class library
local Sprite = require 'godot.Sprite'   -- Make sure to import the base class

local Main = class.extends(Sprite)      -- Create the user subclass

function Main:ready()
  _S.Fighter.MachineGun.load( _R.Supplies.Ammunition.get() ) 
  -- The above line of code is equivalent to:
  get_node( 'Fighter/MachineGun' ).load( get_node( '/root/Supplies/Ammunition' ).get() )

  self.Fighter.MachineGun.unlock()      -- _S table can be interchanged with the 'self' keyword
end

function Main:process(delta)
  if ( _S.HealthGauge.level() < 20 ) then
    apply_first_aid_kit( _R.FirstAidKit )
    -- The above line of code is equivalent to:
    apply_first_aid_kit( get_node( '../FirstAidKit' ) )
  end
end

function Main:apply_first_aid_kit(kit)
  -- do some life saving with the kit
  -- ...
end

return Main

Please share your thoughts on this idea or show us a new idea that you come up about this topic.

-- Perbone

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions