5E Product Walkthrough Playlist
Page 1 of 2 12 Last
  1. #1

    Trying to beef up Story Templates. Help needed

    If ANYONE here is a veteran Lua whiz, I could really use some big brain power. Moonwizard, you out there? *lights up the BatSignal*

    Here is a visual representation of what I am trying to do:
    codinglayout.jpg

    Now here's the long version of my plan, in case anyone does have the ability to help and would like more data:

    Within the software, we have a feature called Tables (where you can roll virtual dice and receive an outcome. These tables can have multiple columns forming their output.), and another feature called Story Templates (where you can write a story, and embed “callouts” to Tables.) When a “Generate Story” button is pressed within the Story Template, all the Table “callouts” you typed are replaced by the retrieved results from the Tables you were asking for. In practice, it would look like this in the Story Template before the button was pressed:

    • The merchant had [Eye Color] eyes, and [NPC Interesting Feature].


    Once the Generate Story button is pressed, a new record is created that replaces the Table callouts, it might look like this:

    • The merchant had blue eyes, and a pink scar on both palms.


    Furthermore, there is another layer to what is baked in out of the box, and these are called Reference Links. They let you recall something you have already retrieved from a given table, and would look like this at first:

    • The merchant had [Eye Color] eyes, and [NPC Interesting Feature]. His <Eye Color> eyes quickly searched the scene ahead.


    Once the Generate Story button is pressed, a new record is created that replaces the Table callouts, and ALSO retrieves data it already stored from the [Eye Color] Table Callout, in order to populate the <Eye Color> Reference Link with the same data. It might look like this:

    • The merchant had green eyes, and a nervous twitch. His green eyes quickly searched the scene ahead.


    I aim to provide a bit more functionality, in the form of a second type of Table Callout, and a second type of Reference Link. My callouts will look like {Table Callout} instead of [Table Callout], and my Reference Links will look like #Reference Link|Column Number# (where "Column Number" is a numerical digit) instead of <Reference Link>. This is intended to be additive, and not replace original functionality but augment it. (Though for prototype purposes, I have removed all original functionality from the code below. I'll add it back in when I get this working)

    When I make a Table callout using the new method, I want the output of the table's different columns to be placed into discrete positions within an array, so I can later retrieve them separately with the new Reference Link. (The original system doesn’t let you do this: any time you use a Reference Link, it verbal-diarrheas every single column result from the initial Table callout) Here's an example of how I want to cherry-pick the column results from Table callouts under my added system:

    Table A has 3 columns and 5 rows. When rolling for a result on Table A, no matter which of the 5 rows is rolled as the result, you will get output from all three columns of that row. So within the story template, let’s say we type:

    {Table A}

    #Table A|2#

    #Table A|3#

    The desired outcome would be that Table A’s output goes into an array, with each column occupying a different position. In the following two lines, we retrieve only what came from the second column of the original {Table A} callout, and on the third line we retrieve only what came from the third column.

    Conceptually, I know what I want to do, and I feel like I am close, but visualizing what I need to do next with my code is boggling me… Any suggestions would be greatly welcome. I'll attach the code so far, and post it below too.
    Attached Files Attached Files

  2. #2
    Code:
    local aLiteralReplacements = {};
    local aLiteralReplacements2 = {};
    local aTableNameTable = {} 
    local aColumnTable = {}
    local StoredTableName = ""
    local incrementor = 0 --I might not use this
    
    
    function onButtonPress()  --This runs when you press "Generate Story" in the Story Template
    	local node = window.getDatabaseNode();
    	
        aLiteralReplacements2 = {}
    	
    	sText2 = performTableLookups2(DB.getValue(node, "text", ""));  -- parse the text in the Story Template, find tables, roll on them, and store the results in an array
        sText2 = performStoredReplacements2(sText2); -- retrieve Reference Link data from the array
    	
    	nodeTarget = DB.createChild("encounter"); -- make a new window
    	DB.setValue(nodeTarget, "text", "formattedtext", sText2); -- put all our new goodies into the new window
        
    	Interface.openWindow("encounter", nodeTarget); -- open the new window for the user to see
    
    end
    
    
    -- Look for table roll expressions
    function performTableLookups2(sOriginal)
    	-- store the original value in a variable. We will replace matches as we go and search for the next matching
    	-- expression.
    	local sOutput = sOriginal;
    	
    	
    	-- Resolve table expressions
    	local nMult = 1;
    	local aLookupResults = {};
    	for nStartTag, sTag, nEndTag in sOutput:gmatch("()[%{]([^%}]+)%}()") do
            local sMult = sTag:match("^(%d+)x$");
            StoredTableName = sTag; -- added this to save the table name up top and return into the function below
    		if sMult then
    			nMult = math.max(tonumber(sMult), 1);
    			table.insert(aLookupResults, { nStart = nStartTag, nEnd = nEndTag, vResult = "" });
    		else
    			local sTable = sTag;
    			local nCol = 0;
    			
    			local sColumn = sTable:match("|(%d+)$");
    			if sColumn then
    				sTable = sTable:sub(1, -(#sColumn + 2));
    				nCol = tonumber(sColumn) or 0;
    			end
    			local nodeTable = TableManager.findTable(sTable);
    
    			local aMultLookupResults = {};
    			local aMultLookupLinks = {};
    			for nCount = 1,nMult do
    				local sLocalReplace = "";
    				local aLocalLinks = {};
    				if nodeTable then
    					bContinue = true;
    					
    					local aDice, nMod = TableManager.getTableDice(nodeTable);
    					local nRollResults = StringManager.evalDice(aDice, nMod);
    					local aTableResults = TableManager.getResults(nodeTable, nRollResults, nCol);  
    					
    					local aOutputResults = {};
    					for _,v in ipairs(aTableResults) do
    						if (v.sClass or "") ~= "" then
    							if v.sClass == "table" then
    								local sTableName = DB.getValue(DB.getPath(v.sRecord, "name"), "");
                                    if sTableName ~= "" then
                                        globalTableName = sTableName
    									sTableName = "[" .. sTableName .. "]";
    									local sMultTag, nEndMultTag = v.sText:match("%[(%d+x)%]()");
    									if nEndMultTag then
    										v.sText = v.sText:sub(1, nEndMultTag - 1) .. sTableName .. " " .. v.sText:sub(nEndMultTag);
    									else
    										v.sText = sTableName .. " " .. v.sText;
    									end
    								end
    								table.insert(aOutputResults, v.sText);
    							else
    								table.insert(aLocalLinks, { sClass = v.sClass, sRecord = v.sRecord, sText = v.sText });
    							end
    						else
    							table.insert(aOutputResults, v.sText);
    						end
    					end
    					
                        sLocalReplace = table.concat(aOutputResults, " "); 
    
                        -- BELOW HERE is where I am hitting a mental roadblock... HOW do I get each column of the table results to go into their own array location,
                        --AND THEN how to I ensure I can accurately retrieve that data later?  I DON'T KNOW....
                        local sTableName = globalTableName;
                        incrementor = incrementor + 1;
                        table.insert(aTableNameTable, { incrementor = sTableName });
                        table.insert(aColumnTable, { StoredTableName = aOutputResults }); -- added storedtablename as the KEY, with the other as the value
                                            
                            
    				else
    					sLocalReplace = sTag;
    				end
    				
    				-- Recurse to address any new math/table lookups
    				sLocalReplace = performTableLookups2(sLocalReplace);
    				
    				table.insert(aMultLookupResults, sLocalReplace);
    				for _,vLink in ipairs(aLocalLinks) do
    					table.insert(aMultLookupLinks, vLink);
    				end
    			end
    
    			local sReplace = table.concat(aMultLookupResults, " ");
    			if aLiteralReplacements2[sTable] then
    				table.insert(aLiteralReplacements2[sTable], sReplace);
    			else
    				aLiteralReplacements2[sTable] = { sReplace };
    			end
    
    			for _,vLink in ipairs(aMultLookupLinks) do
    				sReplace = sReplace .. "||" .. vLink.sClass .. "|" .. vLink.sRecord .. "|" .. vLink.sText .. "||";
    			end
    			
    			table.insert(aLookupResults, { nStart = nStartTag, nEnd = nEndTag, vResult = sReplace });
    			nMult = 1;
    		end
    		
    	end
    	for i = #aLookupResults,1,-1 do
    		sOutput = sOutput:sub(1, aLookupResults[i].nStart - 1) .. aLookupResults[i].vResult .. sOutput:sub(aLookupResults[i].nEnd);
    	end
    	
    	return sOutput;
    end
    
    
    function performStoredReplacements2(sOriginal)  --AND this function is missing something VERY important,
        -- which I can't figure out how to accomplish.  How do I get the Reference Link to pull specific column
        -- data via the use of the pipe "|" symbol followed by the column number?  EX: on a reference link of
        -- #Table A|4# I want only the data from column 4 of the previously rolled {Table A}.  How do I do this!?
        local sOutput = sOriginal;
        local TableName = StoredTableName;
        local nCol = 0
    
        local isFGU = UtilityManager.isClientFGU();
        
        for incrementor,v in pairs(aTableNameTable) do
            local sLiteral = "%#" .. TableName:gsub("([%-%+%.%?%*%(%)%[%]%^%$%%])", "%%%1") .."%#";
            local sColumn = sLiteral:match("|(%d+)$");
            if sColumn then
                TableName = TableName:sub(1, -(#sColumn + 2));
                local sLiteral = "%#" .. TableName:gsub("([%-%+%.%?%*%(%)%[%]%^%$%%])", "%%%1") .."%#";
                nCol = tonumber(sColumn) or 0;
            else
                nCol = 0;
            end
            
            for nCol,o in pairs(aColumnTable) do
    
                sOutput = sOutput:gsub(sLiteral, table.concat(o, " "));	
            end
    
        end
    	return sOutput;
    end

  3. #3
    I like what you're trying to do. But way beyond my feeble skills to assist.

    Good luck!
    Fantasy Grounds Unity Lives! Good job, Smiteworks!

  4. #4
    Thanks. Pulling my damn hair out trying to figure this out. It really should be so damn simple... yet my little brain is smoking.

  5. #5

  6. #6
    Quote Originally Posted by damned View Post
    Good luck JimSocks - keeping an eye on this!
    Thanks Damned, but this project has broken me. I have been working on this nearly every single day since I started. I have paid for and completed an online Lua course just for this purpose. And yet, I think I might be defeated. I just can't figure this out with my brain.

    I have located WHERE the needed code needs to go, but for the life of me even after trying what feels like three hundred different ways I can't get it to act like I envision. I am sure a more competent coder could figure this out in 10 minutes, probably even less, but for me even several weeks isn't enough... ...Feeling pretty broken down over here.

  7. #7
    Wait. Holy **** I think I have cracked it!

    It’s WORKING!!!! Muahahahahahahahaaaa!!!!!

    As soon as I stopped trying to repurpose bits of the existing code to do what I wanted, and began just writing my own from scratch, things started to click.

    I thought I could only succeed by standing on the shoulders of giants, trying to use their super-advanced code and shape it, but that was a fools errand. I needed to write it for myself, and when I did it was much easier to wrap my head around.

    So, it’s working. Now I need to learn how to make an extension that replaces stock FG code with my hot-rodded version. Here’s hoping that’s easier than THIS was!

  8. #8
    Man you guys, this is amazing. I have this 100% working as I envisioned at the outset, and it's just killer. (There is only ONE bug I have noticed that needs to be squashed now: It doesn't like quotes " in the text for some weird reason. Must be a fat-fingered pattern or something, I am sure I will find it soon.)

    I am on a roll- I might just look into passing parameters into story templates now!

  9. #9
    Nice! Glad you didn't give up. I want to see some of the things you do w/ this.
    Fantasy Grounds Unity Lives! Good job, Smiteworks!

  10. #10
    I’m excited too! I have been doing extensive testing of the code, and have found some bugs that I am still wrestling with.

    For starters: the weird quote issue I mentioned above wasn’t the only character that’d throw it for a loop. & > < and # were also causing spasms. I think I‘ve got that under control now, but it took me a whole day to figure out.

    NOW, I am realizing if there are sub-tables, my column storage solution just wants to store the name of those tables in the column data, instead of the results. This is a real brain-teaser- been working on it for a day and a half. I’m sure I’ll figure it out!

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
  •  
STAR TREK 2d20

Log in

Log in