Total RP 3: Extended

Total RP 3: Extended

903k Downloads

Pass variables in workflows links

Solanya opened this issue ยท 2 comments

commented

Seleves on Discord:

I just experimented beefing up document links with HTTP-esque request parameters:
{linkonClick&param1=abc&param2=defclick me}
Clicking that link would feed the onClick workflow with 2 workflow vars.
This would allow consolidating similar workflows into one (e.g. buyItemX1, buyItemX5, buyItemX10 in a shop-style document; or clicking tiles on a board)
In order to make this work I modified onLinkClicked locally in \totalRP3_Extended\document\document.lua:

local function onLinkClicked(self, url)
  if documentFrame.ID and documentFrame.class then
    local document = documentFrame.class;
    local parentArgs = documentFrame.parentArgs;
    
    --[[ original code
    if document.SC and document.SC[url] then
      TRP3_API.script.executeClassScript(url, document.SC, {
        object = parentArgs.object
      }, documentFrame.ID);
    else
       TRP3_API.Ellyb.Popups:OpenURL(url, loc.UI_LINK_WARNING);
    end]]--
    
    local isWorkflowLink = false
    local cArgs = {}
    
    if document.SC and url then
      if document.SC[url] then
        isWorkflowLink = true
      else
        local parts = url:gmatch("[^&]+") -- split by &
        url = parts()
        if document.SC[url] then -- trigger only, if part 1 is a workflow
          for x in parts do
            local _, _, key, value = x:find("([^=]+)=(.+)")
            if key and value then
              cArgs[key] = value
            end
          end
          isWorkflowLink = true
        end
      end
    end
    
    if isWorkflowLink then
      TRP3_API.script.executeClassScript(url, document.SC, {
        object = parentArgs.object,
        custom = cArgs
      }, documentFrame.ID);
    else
      TRP3_API.Ellyb.Popups:OpenURL(url, loc.UI_LINK_WARNING);
    end
    
  end
end

Would rather have a syntax like onClick(abc, def) or onClick(param1=abc, param2=def) for better readability, so need to adjust patterns for this.

commented

Only issue I can think of is how likely would it be that someone wants to actually supply a string with a comma as an argument, such as:

{link*onClick(param1=I really, really love bananas!, param2=:fatcat:)*Click for Cookies!}

Might need to require quotes around the value or escaping commas - which leads to possible nightmare cases like this that needs a lot of care to parse:

{link*onClick(param1="I am a "happy" person today... (really!)", param2=":fatcat:")*Click for More Cookies!}

One solution that - given my zero knowledge of Extended might not even be possible - would be that if instead of passing the value as a literal string to the target workflow, the link instead pass the value assigned to a variable of the same name from whatever object hosted the link to begin with; for example given:

{link*onClick(param1=var1, param2=var2)*Click for no cookies.}

Would it be at all possible for the hypothetical onClick workflow to be executed with param1 set to the contents of a variable named var1 that belongs to the object or document that the link was clicked from in any way?

commented

No, because we wouldn't need to set a value in a variable in such a case.
I don't particularly mind the edge case, as there is still the current method of making multiple workflows to handle it, and it seems much more unlikely than just having numbers/IDs provided.