Page 1 of 2 12 Last
  1. #1
    Kelrugem's Avatar
    Join Date
    Sep 2018
    Location
    Geneva, Switzerland, and Lyon, France
    Posts
    717

    Adding new die to existing dice collection

    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:

    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).
    Last edited by Kelrugem; June 16th, 2019 at 04:24.

  2. #2
    Kelrugem's Avatar
    Join Date
    Sep 2018
    Location
    Geneva, Switzerland, and Lyon, France
    Posts
    717
    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.

    Code:
    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

  3. #3
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Denver, Colorado, USA (for a bit)
    Posts
    23,892
    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.
    Last edited by Trenloe; June 17th, 2019 at 16:31. Reason: Clarity

    FG Con 15 Fantasy Grounds Online RPG Convention - Nov. 8-10, 2019
    Register at www.fg-con.com for all the latest info.

    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  4. #4
    Kelrugem's Avatar
    Join Date
    Sep 2018
    Location
    Geneva, Switzerland, and Lyon, France
    Posts
    717
    Quote Originally Posted by Trenloe View Post
    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

  5. #5
    Kelrugem's Avatar
    Join Date
    Sep 2018
    Location
    Geneva, Switzerland, and Lyon, France
    Posts
    717
    Ok, now I was trying

    Code:
    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

  6. #6
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Denver, Colorado, USA (for a bit)
    Posts
    23,892
    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

    FG Con 15 Fantasy Grounds Online RPG Convention - Nov. 8-10, 2019
    Register at www.fg-con.com for all the latest info.

    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  7. #7
    celestian's Avatar
    Join Date
    Jan 2017
    Location
    DFW, Texas
    Posts
    2,006
    Quote Originally Posted by Kelrugem View Post
    Ok, now I was trying

    Code:
    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
    Try

    Code:
      local dDice = {'d100'};
      table.insert(dDice,'d10');
      Debug.console("some.lua","diceFunction","dDice",dDice);
    ---
    Coding the Official AD&D Ruleset
    My Twitch Channel for AD&D and FG related streams (See schedule for live days)
    My YouTube for FG related Tutorials and AD&D Actual Plays

  8. #8
    Kelrugem's Avatar
    Join Date
    Sep 2018
    Location
    Geneva, Switzerland, and Lyon, France
    Posts
    717
    Quote Originally Posted by Trenloe View Post
    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 Oh, I was trying so much and I also found that page from your link but I never saw the difference... So table.insert is a function returning already the cahnged bDice 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 Now I wonder how I couldn't see that because table.insert is also used that way in the existing code

  9. #9
    Kelrugem's Avatar
    Join Date
    Sep 2018
    Location
    Geneva, Switzerland, and Lyon, France
    Posts
    717
    Quote Originally Posted by celestian View Post
    Try

    Code:
      local dDice = {'d100'};
      table.insert(dDice,'d10');
      Debug.console("some.lua","diceFunction","dDice",dDice);
    Thank you Yes, that works much better now

  10. #10
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Denver, Colorado, USA (for a bit)
    Posts
    23,892
    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.

    FG Con 15 Fantasy Grounds Online RPG Convention - Nov. 8-10, 2019
    Register at www.fg-con.com for all the latest info.

    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Log in

Log in