Log in

View Full Version : How to validate "databasenode = { }"



celestian
May 30th, 2019, 21:36
I've a bit of code that I need to check the value of a node.



Runtime Notice: s'manager_effect.lua' | s'adnd_processEffect' | s'nodeEffect' | databasenode = { }


When the value of nodeEffect is "{ }" I need to do something... the problem is I can't figure out a good way to test nodeEffect and that it equals { }.

The best way I could figure out was to do this.



-- check if effect still exists. removed in fCustomOnEffectActorStartTurn() probably
-- we check the .getPath() through pcall and if it fails then just end this run.
if not pcall(function () nodeEffect.getPath() end) then
Debug.console("manager_effect_adnd.lua","adnd_processEffect","nodeEffect removed in-line",nodeEffect);
return;
end


It works by running the .getPath() on the nodeEffect and if it returns an error through pcall I do what I need to do.

Is there a better way to handle this or is this the best?

The reason this happens... is during the effects processing on a players turn, they might "die" from "DMGO" effects. If that happens, in the manager_action_damage code I remove the DMGO effect (this is part of death's door for AD&D) but when it comes back here from running the custom function the effect is gone... so I test for it and continue or end this effects loop as necessary.

I could just leave the effect on the dead player and let it keep ticking and make my life easier but this does what I want (allows me to remove the effect and then apply a dead effect on them). I'm considering going that way because I had to override a bit of CoreRPG code to do this...

Trenloe
May 30th, 2019, 21:49
I've a bit of code that I need to check the value of a node.



Runtime Notice: s'manager_effect.lua' | s'adnd_processEffect' | s'nodeEffect' | databasenode = { }


When the value of nodeEffect is "{ }" I need to do something... the problem is I can't figure out a good way to test nodeEffect and that it equals { }.
So, it's a LUA table. Maybe check for an empty table? A quick google gives this as a possible solution (the one with the green check mark): https://stackoverflow.com/questions/1252539/most-efficient-way-to-determine-if-a-lua-table-is-empty-contains-no-entries

Using if next(myTable) == nil then

celestian
May 30th, 2019, 21:54
So, it's a LUA table. Maybe check for an empty table?

I had actually tried that (tho I wasn't hopeful) and the error when trying that was....



Runtime Notice: Reloading ruleset
Script Error: [string "scripts/manager_effect_adnd.lua"]:564: bad argument #1 to 'next' (table expected, got userdata)


The type() of the variable is databasenode so I am pretty sure that's why it barfed.

Trenloe
May 30th, 2019, 22:00
The type() of the variable is databasenode so I am pretty sure that's why it barfed.
Ah, OK. I was misreading your debug. So it's an empty databasenode, but not nil.

What does if nodeEffect do? Does it return true or false?

celestian
May 30th, 2019, 22:05
Ah, OK. I was misreading your debug. So it's an empty databasenode, but not nil.

What does if nodeEffect do? Does it return true or false?

When its the listed debug output it returns true using "if nodeEffect" because it's not nil. Infact it always returns true because it's never "nil".

Trenloe
May 30th, 2019, 22:15
Yeah, then you're left to variations on what you're doing. How about DB.getType(nodeEffect) - what does that return?

celestian
May 30th, 2019, 22:18
Yeah, then you're left to variations on what you're doing. How about DB.getType(nodeEffect) - what does that return?

Returns "node" as the type but if it's { } it bombs, the same type I was trying to avoid.

Trenloe
May 30th, 2019, 22:20
Looks like your code is good enough then! :)

celestian
May 30th, 2019, 22:23
Looks like your code is good enough then! :)

Heh, was hoping someone had a better idea than that.

It would be useful if we had a DB.exists(nodeEffect) or similar function perhaps.

After all this I think I might just pull it all out and just leave the effect on the dead character. In the long run I think it'll be better that I don't override CoreRPG code (in this case EffectManager.processEffect()).

Moon Wizard
June 1st, 2019, 05:42
How are you getting a node with no path? Was it deleted during the script?

JPG

celestian
June 1st, 2019, 06:09
How are you getting a node with no path? Was it deleted during the script?

JPG

The processEffects() calls processEffect() for each effect. ProcessEffect runs the "DMGO" effect and because I removed the effect in the processing of the DMGO if the PC died in manager_action_damage.lua when the processEffect() continued trying to use the effect it would bomb.

I ended up not removing the effect because of all the hackery I had to do to make it work... I did get it working it just seemed a bit over the top and I really dont want to override CoreRPG unless I have to...

scleavelin
June 6th, 2019, 00:17
Sorry if this was already addressed, but can you not do:

if not next(databasenode) then
Debug.console('Table is Empty')
else
Debug.console('Table is not Empty')
end

celestian
June 6th, 2019, 00:19
I had actually tried that (tho I wasn't hopeful) and the error when trying that was....



Runtime Notice: Reloading ruleset
Script Error: [string "scripts/manager_effect_adnd.lua"]:564: bad argument #1 to 'next' (table expected, got userdata)


The type() of the variable is databasenode so I am pretty sure that's why it barfed.

...


Sorry if this was already addressed, but can you not do:

if not next(databasenode) then
Debug.console('Table is Empty')
else
Debug.console('Table is not Empty')
end

See above, doesn't work ;(

scleavelin
June 6th, 2019, 00:23
Ah, sorry I missed the part of the next call not seeing the databasenode as a table.

scleavelin
June 6th, 2019, 00:36
Can you try something like:

local node = DB.findNode('Somemissingnode');
local node2 = DB.findNode('currencies.id-00001.name')

local nodeType = type(node2);

print(node2)

if type(node) == 'databasenode' then
print('Node Found')
else
print('Node Not Found')
end

if type(node2) == 'databasenode' then
print('Node2 Found')
else
print('Node2 Not Found')
end

celestian
June 6th, 2019, 00:42
Can you try something like:

local node = DB.findNode('Somemissingnode');
local node2 = DB.findNode('currencies.id-00001.name')

local nodeType = type(node2);

print(node2)

if type(node) == 'databasenode' then
print('Node Found')
else
print('Node Not Found')
end

if type(node2) == 'databasenode' then
print('Node2 Found')
else
print('Node2 Not Found')
end

I cannot get the path string of the node without .getPath(). nodeEffect.getPath() would fail... the first post contained the only solution available to my knowledge.



-- check if effect still exists. removed in fCustomOnEffectActorStartTurn() probably
-- we check the .getPath() through pcall and if it fails then just end this run.
if not pcall(function () nodeEffect.getPath() end) then
Debug.console("manager_effect_adnd.lua","adnd_processEffect","nodeEffect removed in-line",nodeEffect);
return;
end

scleavelin
June 6th, 2019, 00:44
Ok. Just trying to get a hang of coding this stuff.
Trying to learn and all.

gnujak
June 20th, 2019, 00:36
I had a similar thing when deleting nodes if there was nothing in the node it would show that. What I did was check the if there were any children under the node

childCount = DB.getChildCount(nodeChar,"defenses.armour");
if (childCount > 0) then


if there was something there child count would be more than zero. if there was nothing there it would be zero. Maybe that will help?