Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Need Help with a Random NPC Generator (ScriptCards, ZeroFrame, Recursive Tables)

Hi - I have been strugging with this script for days, and I think I've taken this to the limits of my ability. Looking for an API wizard to help me break through.


The idea is I want to create a Random NPC Generator. A Random Race and Gender will be rolled, and then, based on the Race and Gender, a Random name corresponding to that Race and Gender would be rolled.


Using Scriptcards, the script I came up with looks like this:

!script{{
  --#title| NPC Information
  --=randomRace| [T#NPC-Race]
  --=randomGender| [T#NPC-Gender]
  --=randomPicture| [T#Silium-Tusk]
  --=randomName| [T#[$randomRace.tableEntryText]-[$randomGender.tableEntryText]] 
  --+| [c][img width = 200][$randomPicture.tableEntryImgURL][/img][/c]
  --+Name| [$randomName.tableEntryText]
  --+Race|  [$randomRace.tableEntryText]
  --+Gender| [$randomGender.tableEntryText]
}}

It works except for one big problem. Many of these Tables have recursive rolls and the output fails to show the recursive roll text (as if I didn't have the Recursive Table API installed). A great example is when I roll a name for Tiefling-Male or Tiefling-Female. There is a 50% chance that the NPC will have one of the Tiefling Virtue names instead of a Tiefling name ascribed to their Gender, and then it roles on the Tiefling-Virtue roll table.

My research online said that ZeroFrame might be able to help. But I've hit some snags.

I got this to work:
!script {{ {&global ([RaceResult] [\][\]1t[NPC-Race] \]\].value)} {&global ([GenderResult] [\][\]1t[NPC-Gender] \]\].value)}
  --#title|NPC Information
  --+Name| [\][\]1t[Tiefling-Male] \]\].value
}}

In the above script, if the Tiefling-Male rolltable results in a Tiefling-Virtue name, the Tiefling-Virtue rolltable result displays correctly.

I've also gotten this to work:

!script {{ {&global ([RaceResult] [\][\]1t[NPC-Race] \]\].value)} {&global ([GenderResult] [\][\]1t[NPC-Gender] \]\].value)}
  --#title|NPC Information
  --+Race|RaceResult
  --+Gender|GenderResult
}}

Additionally, I've gotten this to work:

!script {{ {&global ([LeaveBlank] LeaveBlank)}
  --#title|NPC Information
  --&randomRace|[\][\]1t[NPC-Race] \]\].value
  --&randomGender|[\][\]1t[NPC-Gender] \]\].value
  --+Race|[&randomRace]
  --+Gender|[&randomGender]
}}

But now I can't figure out how to get the Race and Gender results to affect the Name Result, just like I was able to accomplish before using ZeroFrame.

This doesn't work:

 --+Name|[\][\]1t[[&randomRace]-[&randomGender] \]\].value

This doesn't work either:

!script {{ {&global ([randomRace] [\][\]1t[NPC-Race] \]\].value)} {&global ([randomGender] [\][\]1t[NPC-Gender] \]\].value)}
--#title|NPC Information --+Name|[\][\]1t[randomRace-randomGender] \]\].value --+Race|randomRace --+Gender|randomGender }}


I'm stuck and I can't figure this out for the life of me. Anyone know what I can do or if what I'm trying to accomplish is possible?

June 07 (1 year ago)

Edited June 07 (1 year ago)
timmaugh
Forum Champion
API Scripter

Hey, Paul....

You're running into a problem of timing. All of the metascripts run prior to ScriptCards getting a chance at the message... They don't work at the same time or concurrent with ScriptCards. That means you can't use metascript functionality that relies on ScriptCards functionality in the same message (if you start a new message from ScriptCards, that message will also get a metascript loop, so during that loop the metascrips will be able to work on the message, and the message will be in whatever state ScriptCards left it -- resolving it's variables, etc -- but that isn't what you have going).

By the time ScriptCards processes those variables, the metascripts are long done. Alternatively, if the metascripts do work to the message and the command becomes un-deferred (a backslash is removed), then the roll will likely be detected prior to ScriptCards having the chance to process the variables, so the roll will break.

If you want to use the ZeroFrame .value syntax, you're going to have to do it all during the Metascript portion of the processing. It looks like you are using ScriptCards to do your rolling against the tables, so those would have to be standard Roll20 retrievals. I don't know if that limits the actions ScriptCards let's you take with the roll, later, or if that would matter... Right now it looks like it wouldn't matter.

Looking at your last example, it's pretty close... Your global variables are getting assigned to rolls, but those rolls are deferred. Typically that would mean that the global tag needs to be deferred, too (otherwise you're filling in unresolved roll syntax everywhere, and everywhere you do that you'll get a different result). What is really getting you, though, is that because the roll in randomGender and randomRace isn't resolved, the .value syntax is still there when the Name roll tries to resolve, and that will break things.

The solution is that the rolls in the global variables randomGender and randomRace don't need to be deferred. They are just normal rolls that we will extract the value from. We don't need to wait on any metascript shenanigans, so there's no reason to defer.

Since they are not deferred, the .value syntax will resolve in the first metascript loop and get assigned to the variables. Since that happens in the first loop, we need one deferral on the name roll (which is what we have). Now THAT roll will resolve.

I'm away from my computer, but this should work:

!script {{ {&global ([randomRace] [[1t[NPC-Race] ]].value)} {&global ([randomGender] [[1t[NPC-Gender] ]].value)}
  --#title|NPC Information
  --+Name|[\][\]1t[randomRace-randomGender] \]\].value
  --+Race|randomRace
  --+Gender|randomGender
}}

Give that a try and see if it works. Post back with any questions!

Oh my God. I am going to cry tears of joy. Thank you so much timmaugh! It works!!! This is such a relief.

Serendepitous that you were the one to respond, because I only got as far as I did on my own by reading your help in this thread where someone tried to make a foraging scriptcard.


The one thing I will note is the API is randomly crashing after every 2-5 times I run this script. But I have a feeling this might be happening because I don't yet have my Human-Male, Human-Female, Half-Elf-Male, and Half-Elf-Female tables set up yet. There results are not coming up when I run the script (like name is blank and race and gender still come up)


I'll report back if adding tables for those Races and Genders resolves the problem.

Here to confirm that adding those rolltables seems to have resolved the API crashing!


I will keep people posted on what the final product looks like!

June 08 (1 year ago)
timmaugh
Forum Champion
API Scripter

Excellent!

(And I'm the author of the Metascript Toolbox, so hit me up with any questions you run into!)