PDA

View Full Version : Adding new die to existing dice collection



Kelrugem
June 15th, 2019, 23:45
Hi :)

I now learned how to code a trigger for dice rolls with their chat message etc., in my case a d100 triggered by an effect. However, the problem is that the number of d100 is arbitrary in my code (so, it can be that it has to be 2d100 instead of just d100) and that depends on the situation, like a DMG effect can add an additional die to the damage in 3.5e. I do not know how to add additional d100 to the existing one, I tried table.insert like I saw it in the coding of the DMG effect but that does somehow not work. To break down my code I try at some point in my code:



local bDice = {};
bDice = table.insert(bDice, {"d100, d10"});


But the outcome is only a nil value. I thought that I maybe insert the additional d100 wrong but replacing {"d100, d10"} with a "d6" (as string, I guess(?)) does not work, too, so this does somehow not work for other types of dice.

Do I use the insert function somehow wrong? I tried to do it similar to other parts in existing code but something is going wrong here for me :)

Best,

Kelrugem

EDIT: Oh, of course, bDice will be the variable defining the type of die of my roll, so it will be in some "rRoll" and rRoll.aDice = bDice (following the guideline in the comments at the very top of manager_actions.lua in CoreRPG).

Kelrugem
June 17th, 2019, 06:58
To explain a bit more: When I look at bDice before table.insert (by using Debug.console(bDice)) then it returns the defined value, but after table.insert it is empty. So, e.g.



local bDice = {"d100", "d10"};
bDice = table.insert(bDice, {"d100, d10"});


results into bDice ={} at the end, so after table.insert its entries are completely erased for some reason which I do not understand :)

Trenloe
June 17th, 2019, 14:22
Looks like you’re trying to insert a table into a table, whereas (in theory) this should work as far as LUA is concerned, it won't give the correct format for FG. In FG code I haven't seen multiple dice added at once. Try inserting the d100 and d10 separately as two table.insert commands.

Kelrugem
June 17th, 2019, 18:26
Looks like you’re trying to insert a table into a table, whereas (in theory) this should work as far as LUA is concerned, it won't give the correct format for FG. In FG code I haven't seen multiple dice added at once. Try inserting the d100 and d10 separately as two table.insert commands.

Thank you, I will try that :)

Kelrugem
June 17th, 2019, 19:28
Ok, now I was trying


bDice = table.insert(bDice, "d100");
bDice = table.insert(bDice, "d10");

But then I get the error message


Script Error: [string "scripts/manager_action_damage.lua"]:517: bad argument #1 to 'insert' (table expected, got nil)

And line 517 is exactly the line of the second table.insert; so, it seems that bDice is again completely erased after the first table.insert. I also tried "d6" or numbers but I always get this error, I do not know why my bDice is always being erased :D

Trenloe
June 17th, 2019, 19:32
That's because you're overwriting bDice in the first line with the result of the table.insert function, which I don't think returns anything. This is the first issue you had in your original post - sorry I didn't spot that too, I was more looking at inserting a table into a table being an issue.

Define bDice as an empty table first, then just use table.insert...

Details here: https://www.lua.org/pil/19.2.html

celestian
June 17th, 2019, 19:37
Ok, now I was trying


bDice = table.insert(bDice, "d100");
bDice = table.insert(bDice, "d10");

But then I get the error message



And line 517 is exactly the line of the second table.insert; so, it seems that bDice is again completely erased after the first table.insert. I also tried "d6" or numbers but I always get this error, I do not know why my bDice is always being erased :D

Try



local dDice = {'d100'};
table.insert(dDice,'d10');
Debug.console("some.lua","diceFunction","dDice",dDice);

Kelrugem
June 17th, 2019, 19:38
That's because you're overwriting bDice in the first line with the result of the table.insert function, which I don't think returns anything.

Define bDice as an empty table first, then just use table.insert...

Details here: https://www.lua.org/pil/19.2.html

aaaaaah, okay, now I see it :D Oh, I was trying so much and I also found that page from your link but I never saw the difference... :D So table.insert is a function returning already the cahnged bDice :D For some reasons I thought I have to designate/name that return still for calling it later (therefore the "bDice=..." in front)

Thank you very much :) I should have asked earlier, could have saved me a lot of time :D Now I wonder how I couldn't see that because table.insert is also used that way in the existing code :D

Kelrugem
June 17th, 2019, 19:38
Try



local dDice = {'d100'};
table.insert(dDice,'d10');
Debug.console("some.lua","diceFunction","dDice",dDice);


Thank you :) Yes, that works much better now :D

Trenloe
June 17th, 2019, 19:46
If you always want bDice to be a d100 and a d10 then you can just statically set the table as you did in the first line of post #2: local bDice = {"d100", "d10"}; Then you can add any further dice to this as required using one table.insert for each die to be added.

Kelrugem
June 17th, 2019, 19:54
If you always want bDice to be a d100 and a d10 then you can just statically set the table as you did in the first line of post #2: local bDice = {"d100", "d10"}; Then you can add any further dice to this as required using one table.insert for each die to be added.

Thank you very much again :) Yes, now everything works as expected :)

(The background is that I want to have phycial dice for fortification in 3.5e/Pathfinder, that is some percentage die you roll against incoming critical or precision damage to negate that when the outcome of the d100 is small enough. So depending on the incoming damage you need no d100 or 1d100 (when there is critical or precision) or 2d100 (when there is both, critical and precision damage die). Thus, I wrote a code to look at each damage die to see if fortification applies; if fortification applies then it should add a d100. Therefore bDice is normally empty at the beginning such that the situation with no d100 is also possible :) )