PDA

View Full Version : How to pull a record inside a function



Druthlen
June 28th, 2019, 06:04
Once you are inside a function how do you pull a record?

Here in this function I have nodeTrait which is user data for some racial traits. The text inside the nodeTrait is 3 disciplines
The while statements add the 3 disciplines to a list. Then the for statement runs until its at the end of the list. So far so good.
My problem is on the addDisciplines function I get no record exist. Because I am not calling the record correctly. How would I call a record from inside a function like this?
DB.getchildren? DB.setvalue?

I was thinking something like

addDiscipline(nodeChar, "referencediscipline", DB.setvalue(nodeChar, sRecord=refrence.disciplines.v);

But I don't know the proper syntax for it.

function handleClanDisciplines(nodeChar, sClass, nodeTrait)
local sText = DB.getText(nodeTrait, "text");
local aWords = StringManager.parseWords(sText);

local sDisciplines = {};
local ListDisciplines = DataCommon.disciplines;

local sFormat = Interface.getString("char_message_clanadd");
local sMsg = string.format(sFormat, "test", DB.getValue(nodeChar, "name", ""));

local i = 1;
local number = 0;
while aWords[i] do
if StringManager.isPhrase(aWords, i, { "three", "dots", "on" }) then
local j = i + 3;
while aWords[j] do
local k = 1;
while ListDisciplines[k] do
if aWords[j]:match(ListDisciplines[k]) then
table.insert(sDisciplines, aWords[j]);
number = number + 1;
break;
end
k = k + 1;
end

if not StringManager.isWord(aWords[j], "and" or "or") and not flag == 3 then
break;
end
j = j + 1;
end
break;
end
i = i + 1;
end

if #sDisciplines == 0 then
return false;
end

for _,v in ipairs(sDisciplines) do
addDiscipline(nodeChar, "referencediscipline", v);
end
return true;
end

damned
June 28th, 2019, 11:06
Something like:



local nodeChar = window.getDatabaseNode();
local myDiscipline = nodeChar.getChild("discipline").getValue();


edited typo/copypasto

Druthlen
June 28th, 2019, 19:01
Something like:



local nodeChar = cli_rollsWindowDBNode;
local myDiscipline = nodeChar.getChild("discipline").getValue();



When I run it that way nodeChar returns as a nil value.



function addDiscipline(nodeChar, sClass, sDiscipline)
local nodeChar = cli_rollsWindowDBNode;
local myDiscipline = nodeChar.getChild("disciplines").getValue();



local nodeSource = resolveRefNode(sRecord);
if not nodeSource then
return;
end



local nodeList = nodeChar.createChild("disciplines");
if not nodeList then
return;
end

local sDisciplineName = DB.getValue(nodeSource, "name", "");
local sDisciplineNameLower = StringManager.trim(sDisciplineName):lower();

local sFormat = Interface.getString("char_message_disciplineadd");
local sMsg = string.format(sFormat, sDisciplineName, DB.getValue(nodeChar, "name", ""));
ChatManager.SystemMessage(sMsg);

-- Try and match an existing class entry, or create a new one
local sRecordSansModule = StringManager.split(sRecord, "@")[1];
local nodeDiscipline = nil;
for _,v in pairs(nodeList.getChildren()) do
local _,sExistingdisciplinePath = DB.getValue(v, "shortcut", "", "");
if sExistingDisciplinePath == "" then
local sExistingDisciplineName = StringManager.trim(DB.getValue(v, "name", "")):lower();
if sExistingDisciplineName ~= "" and (sExistingDisciplineName == sDisciplineNameLower) then
nodeDiscipline = v;
break;
end
else
local sExistingDisciplinePathSansModule = StringManager.split(sExistingDisciplinePath, "@")[1];
if sExistingDisciplinePathSansModule == sRecordSansModule then
nodeDiscipline = v;
break;
end
end
end
local nLevel = 0;
local bExistingDiscipline = false;
if nodeDiscipline then
bExistingDiscipline = true;
nLevel = DB.getValue(nodeDiscipline, "level", 1) + 1;
else
nodeDiscipline = nodeList.createChild();
end

if not bExistingDiscipline then
DB.setValue(nodeDiscipline, "name", "string", sDisciplineName);
end
DB.setValue(nodeDiscipline, "level", "number", nLevel);
DB.setValue(nodeDiscipline, "shortcut", "windowreference", sClass, sRecord);

local nTotalLevel = 0;
for _,vDiscipline in pairs(DB.getChildren(nodeChar, "disciplines")) do
nTotalLevel = nTotalLevel + DB.getValue(vDiscipline, "level", 0);
end
end

damned
June 29th, 2019, 08:49
sorry - typo above - what happens if you add this near the top?



local nodeChar = window.getDatabaseNode();
Debug.chat("nodeChar: ", nodeChar);

Druthlen
June 29th, 2019, 21:31
Ill try it and get back to you. For now this worked.


function addDiscipline(nodeChar, sClass, sDiscipline)
local sDisciplineNameLower = StringManager.trim(sDiscipline):lower();

local sRecord = "reference.disciplines." .. sDisciplineNameLower .. "@VtM 20th Anniversary Edition";
local nodeSource = resolveRefNode(sRecord);
if not nodeSource then
return;
end


local nodeList = nodeChar.createChild("disciplineslist");
if not nodeList then
return;
end

local sDisciplineName = DB.getValue(nodeSource, "name", "");
local sDisciplineNameLower = StringManager.trim(sDisciplineName):lower();


-- Try and match an existing discipline entry, or create a new one
local sRecordSansModule = StringManager.split(sRecord, "@")[1];
local nodeDiscipline = nil;
for _,v in pairs(nodeList.getChildren()) do
local _,sExistingdisciplinePath = DB.getValue(v, "shortcut", "", "");
if sExistingDisciplinePath == "" then
local sExistingDisciplineName = StringManager.trim(DB.getValue(v, "name", "")):lower();
if sExistingDisciplineName ~= "" and (sExistingDisciplineName == sDisciplineNameLower) then
nodeDiscipline = v;
break;
end
else
local sExistingDisciplinePathSansModule = StringManager.split(sExistingDisciplinePath, "@")[1];
if sExistingDisciplinePathSansModule == sRecordSansModule then
nodeDiscipline = v;
break;
end
end
end
local nLevel = 0;
local bExistingDiscipline = false;
if nodeDiscipline then
bExistingDiscipline = true;
nLevel = DB.getValue(nodeDiscipline, "level", 1) + 1;
else
nodeDiscipline = nodeList.createChild();
end

if not bExistingDiscipline then
DB.setValue(nodeDiscipline, "name", "string", sDiscipline);
end
local sFormat = Interface.getString("char_message_clanadd");
local sMsg = string.format(sFormat, DB.getValue(nodeDiscipline, "name", ""), DB.getValue(nodeChar, "name", ""));
ChatManager.SystemMessage(sMsg);
DB.setValue(nodeDiscipline, "level", "number", nLevel);
DB.setValue(nodeDiscipline, "shortcut", "windowreference", sClass, nodeSource.getNodeName());

local nTotalLevel = 0;
for _,vDiscipline in pairs(DB.getChildren(nodeChar, "disciplines")) do
nTotalLevel = nTotalLevel + DB.getValue(vDiscipline, "level", 0);
end
end