Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick Access to the Scene's Tree Nodes #31

Open
perbone opened this issue Nov 11, 2020 · 2 comments
Open

Quick Access to the Scene's Tree Nodes #31

perbone opened this issue Nov 11, 2020 · 2 comments

Comments

@perbone
Copy link
Owner

perbone commented Nov 11, 2020

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

@perbone
Copy link
Owner Author

perbone commented May 1, 2022

So there is this new concept called "unique node" that facilitates access to a node by its name only, without needing the full path.

I don't know if I really liked this idea, but if I end up implementing it, here's how it will work with Lua.

There will be a local table called _U that will contain the nodes configured as unique, allowing quick access to these nodes without the need to inform the full path.

root
/root/Hero
/root/Hero/Fighter
/root/Hero/Fighter/MachineGun <== This node is unique
/root/Hero/HealthGauge
/root/Supplies
/root/Supplies/Ammunition
/root/FirstAidKit

local currentAmmunitionCount = _U.MachineGun.get_current_ammunition_count()

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

-- Perbone

@Poikilos
Copy link

Poikilos commented May 1, 2022

It is a Lua-like way to provide a Godot-like feature, so it seems pretty good. _U Is the localized string (translate) function in FiveM but that's pretty obscure so it probably doesn't matter unless someone tries to port FiveM to Godot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants