PDA

View Full Version : Working with then and else...



damned
September 16th, 2014, 01:33
Im having trouble extending my INIT rolls.
Together with Trenloe we scripted the action for the NPCs.
Now I need to take that code and check whether it was a PC or an NPC that rolled and then update a different database field depending on who did it...

This function checks where the INIT roll came from:

function getSkillRoll(rActor, vSkill)
local rRoll = {};
rRoll.sType = "init";
rRoll.aDice = { "d10","d10" };

local sInit = DB.getValue(vSkill, "Speed", "");
Debug.console("sInit:" .. sInit);
local sActorType, nodeActor = ActorManager.getTypeAndNode(rActor);
Debug.console("nodeActor: " .. nodeActor);
Debug.console("sActorType: " .. sActorType);
rRoll.sDesc = "[INIT] " .. DB.getValue(vSkill, "value", "");


return rRoll;
end


returning pc or npc for sActorType

In the case of npc I need to update the npc.init database field (which works) but I cant work out the right syntax to check if its a PC or a NPC rolling and in the case of PC update the char.init field instead...


function onRoll(rSource, rTarget, rRoll)
local rMessage = ActionsManager.createActionMessage(rSource, rRoll);

-- Get the total of the roll - this includes and modifiers
local sRollTotal = ActionsManager.total(rRoll);

-- Get the actor type and the database node of that actor (the record that made the roll).
local sActorType, nodeActor = ActorManager.getTypeAndNode(rSource);

-- If we have a valid actor database node:
if nodeActor then
--Get the npc init node and set the total value
local npcInitDBNode = nodeActor.getChild("npc.init");
npcInitDBNode.setValue(sRollTotal);
end

rMessage.text = rMessage.text .. " " .. sRollTotal;
Comm.deliverChatMessage(rMessage);
end

damned
September 16th, 2014, 01:37
funny thing is... after typing this out and looking in the db.xml I could see that my code was still writing npc.init back to character xml - so as a quick fix I changed my PC init db field to also be npc.init instead of char.init and that works... but I would still love some help on how to write me if/then/else statements :)

Andraax
September 16th, 2014, 02:17
if op == "+" then
r = a + b
elseif op == "-" then
r = a - b
elseif op == "*" then
r = a*b
elseif op == "/" then
r = a/b
else
error("invalid operation")
end

Moon Wizard
September 16th, 2014, 02:19
Typing from phone so limited formatting.

if nodeActor then
if sActorType == "pc" then
DB.setValue(nodeActor, "char.init", "number", sRollTotal);
else
DB.setValue(nodeActor, "npc.init", "number", sRollTotal);
end
end

Cheers,
JPG

Moon Wizard
September 16th, 2014, 02:20
In general, if defining new system, I suggest matching field locations in database between PC and NPC to save work later.

Cheers,
JPG

Trenloe
September 16th, 2014, 02:31
I told him it would be a good idea to reorganise the NPC database structure.

He doesn't listen to me though...

;)

damned
September 16th, 2014, 02:34
it is a brand new system... it makes much more sense leaving them the same.....

thanks andraax I couldnt get that working - it always kept telling me that first line needed to be: if op then - it didnt want any operators on the first line...

thanks moon_wizard - i think i was using the wrong variable on the nested if statements...

damned
September 16th, 2014, 02:41
I told him it would be a good idea to reorganise the NPC database structure.

He doesn't listen to me though...

;)

i listen to you - i just dont always understand what you are saying when you say it...
as you can see above in post 2 your words of wisdom filtered thru to the conscious layer and i made the change....

ive just tested JPGs suggestion which also works but I think i should go with the simplified data structure.

Trenloe
September 16th, 2014, 02:55
...your words of wisdom filtered thru to the conscious layer...
Wasn't sure you had one of those! ;)

damned
September 16th, 2014, 03:47
Wasn't sure you had one of those! ;)

I have a conscious side - its just not very good at programming ok...