STAR TREK 2d20
  1. #1
    leozelig's Avatar
    Join Date
    Jun 2007
    Location
    Eastern USA
    Posts
    1,852
    Blog Entries
    1

    Decoding Strings for Variables

    I am trying to pull a die type from a simple string, but I don't understand the parsing code very well. For my NPCs, they will have an 'action die' string in the following format:

    1d20
    2d24
    etc.

    I am trying to ignore the first number, and just pull the "d##" for building the attack roll. The variable can be a string, but I will be converting it to a die. Does anyone know how to code that?

  2. #2
    leozelig's Avatar
    Join Date
    Jun 2007
    Location
    Eastern USA
    Posts
    1,852
    Blog Entries
    1
    I think I figured it out. I stole some code from the convertStringToDice function in the StringManager and carved out what I didn't need. The "actiondice" string will be something like "1d16" or "2d24", and I have chosen to ignore the die count for now.

    Code:
    	-- Get Action Die for NPCs
    	if sActorType == "npc" then
    		local sNPCActionDie = DB.getValue(nodeActor, "actiondice");
    		local aRulesetDice = Interface.getDice();
    		for v in sNPCActionDie:gmatch("([%da-zA-Z]+)") do
    			local sDieCount, sDieNotation, sDieType = v:match("^(%d*)([a-zA-Z])([%dF]+)");
    			if sDieType then
    				sDieNotation = sDieNotation:lower();
    				sDieType = sDieNotation .. sDieType;
    				if StringManager.contains (aRulesetDice, sDieType) or (sDieNotation == "d") then
    					local sDie = sDieType;
    
    
    					rRoll.aDice[1] = sDie;
    				end
    			end
    		end
    	end

  3. #3
    leozelig's Avatar
    Join Date
    Jun 2007
    Location
    Eastern USA
    Posts
    1,852
    Blog Entries
    1
    Actually, that should be simplified to:

    rRoll.aDice[1] = sDieType;

  4. #4
    damned's Avatar
    Join Date
    Mar 2011
    Location
    Australia
    Posts
    26,678
    Blog Entries
    1
    ...

  5. #5
    leozelig's Avatar
    Join Date
    Jun 2007
    Location
    Eastern USA
    Posts
    1,852
    Blog Entries
    1
    In case anyone else has a similar question in the future, here is the code I ended up with:

    Code:
    	-- Get action die for NPCs
    	if sActorType == "npc" or sActorType == "ct" then
    		local sNPCActionDie = DB.getValue(nodeActor, "actiondice");
    		local aRulesetDice = Interface.getDice();
    		local sDieCount, sDieNotation, sDieType = sNPCActionDie:match("^(%d*)([a-zA-Z])([%dF]+)");
    			if sDieType then
    				sDieNotation = sDieNotation:lower();
    				sDieType = sDieNotation .. sDieType;
    				if StringManager.contains (aRulesetDice, sDieType) or (sDieNotation == "d") then
    					rRoll.aDice[1] = sDieType;
    				end
    			end
    	end
    Here is a screenshot of the result:

    Attachment 14178

Thread Information

Users Browsing this Thread

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

Bookmarks

Posting Permissions

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

Log in

Log in