PDA

View Full Version : Getting a "databasenode" from a databasenode and path string?



Varsuuk
March 16th, 2019, 05:00
If I get the "characterID" node (ie, like the "character root" of sorts) as an argument, what is the best way to access the NODE that is described by appending a path to that root?
I have always gotten the VALUE of the node via DB.getValue(nodeChar, path, default) calls. But that returns a value not a databasenode.

Here is one way I hit and missed to resolve:


local node = DB.findNode(nodeChar.getPath() .. ".abilities.strength.score");


Any better way if the above is not the recommended way?
(doing it this way for now and will recode if other solutions recommended)

damned
March 16th, 2019, 06:17
try getDatabaseNode()

Varsuuk
March 16th, 2019, 06:29
Yeah, I've used that and it should be the obvious one normally - but as you might be able to infer, the node given to me as an argument is already the "main character node" you get with getDatabasaeNode. I am trying to travel from that node to "ThatNode.abilities.strength.**score**"

How do I do so with least amount of lookups or temporary node creation (if that's a thing, I'm C++ some Java not Lua...) to get where I am going.
My first attempt also was to create a path from path of dynode + relative offset to where I want to go. Then parlay that into a reference to the node I always wanted to begin with.

But seemed circuitous and figured there is another clearer way.

celestian
March 16th, 2019, 07:09
If nodeChar is valid then:

DB.getValue(nodeChar,"abilities.strength.score",0);

Seems easier? That returns the value you are trying to get (the strength score) or 0 if it's not set.

Varsuuk
March 16th, 2019, 07:28
I'm actually trying to get the NODE itself (pointing to score in that string) not the value of the node.
The line I posted works, but strikes me as "not quite elegant"

I originally looked for something like (pseudo api): databasenode = myNode.childAt("path.to.child")

Off to sleep (sorry if wasn't clear)

celestian
March 16th, 2019, 07:38
Not really clear on your use case. Normally I'd use getDatabaseNode() from within the scope of the abilities windowclass (or a handler) and process whatever you're wanting. Otherwise I'd use the previously mention method to get the value.

Ikael
March 16th, 2019, 10:40
local nodeStr = nodeChar.getChild("abilities.strength.score")


Or if you want to make sure that the node exists and you know that the script it invoked by user who is owner of the nodeChar


local nodeStr = nodeChar.createChild("abilities.strength.score", "number")


If the node already exists, this will function as getChild otherwise it will create the node and return it. If the script invoker is not owner, it will return nil.

Varsuuk
March 16th, 2019, 16:37
OK... getChild(path) WHY did that not pop in my mind. I'd say I should stop coding at midnight to 2am except that is why I stopped working on my project - when I limited myself to non-existent free time in "normal hours"
So that works to do what I was doing via the first post that my gut told me was unelegant - glad my "clunky-sense" is still working.


Celestian, I already had the node that getDatabaseNode would give and your getValue would work to get the value of the node that I wanted to "point to" (I don't have words for describing lua concepts well yet) but I wanted to point to it to later "do things" with it ;) In the end, I realized I was better off with the parent that you referred to via getDatabaseNode but I was and ever am curious and I wanted to know if there was another way to do it. That it was so obvious as getChild makes me shake my head at my not seeing it. Responders not getting what I meant I understand as it is common when I write questions, I'm not good at head to paper (as it were.)

I actually look a bunch at your great code and saw that "node.getChild(....)" to go up levels, which at first made me... huh? until I realized there was no getParent() for multilevels up but there was for down or rather "delta" movement which includes up.

(Rereading what I wrote... yeah, not clear - this is why I am trying to show code more when I ask.)