I’m trying to create a character generator, and I’m currently stuck on an issue. I’m trying to assign s to a single value picked from the list of possible species, so I can then use that value later for the species displayed and for some species-specific traits. I feel like this should be possible, but I’m also not super knowledgeable in perchance
The issue is this line in your lists editor:
s = [mob.selectOne]
This means that every time you use
s
it’ll select a new random item frommob
. The reason for that is because you’ve mades
equal to[mob.selectOne]
instead ofmob.selectOne
. The square brackets around it effectively make it “dynamically fetched” each time you accesss
.(Aside: Also note that the lists editor isn’t “executed” each time you click your generate button. It’s not a list of instructions which get executed line-by-line. The square blocks in the HTML editor on the other hand do get executed line-by-line each time you click the generate button. Think of the code in your list editor as a “static” structure which can be used by the square blocks in your HTML editor.)
So you need to add
[
in a place where it’ll get “executed” with every generate button click, so a new ]s
is generated with each click, but it stays the same when executing all of the other square blocks that come after it.So you could add this line before
[output]
in your HTML editor:[s = mob.selectOne, ""]
Each time your “Generate” button is clicked, the
update()
function is called, and that causes all the square blocks in the HTML code to be executed from top to bottom. So it ensures that we’ve randomly selected amob
item and put it in thes
variable before theoutput
list is executed (which uses thes
variable).Here’s a fixed example: https://perchance.org/ylx7fmwpql#edit
(This issue has unfortunately been the source of a lot of problems for newbies learning Perchance, so you’re not alone in being confused by this!)
Thank you so much!! Good to know I’m not the only one though lol
That’s a great answer! I’ve never thought of these solutions in a while, maybe even add a warning for that so other newbies could know easily.
You can use the shorthand
exp ? if_true : if_false
notation chained together instead of theif ... else
notations. It’s specially used for conditional inline value returns.[s == "Wolf" ? wolf_gene : s == "Cat" ? cat_gene : s == "Fox" ? fox_gene : ""]
You can also do like this as well:
[({"Wolf": wolf_gene, "Cat": cat_gene, "Fox": fox_gene})[s]]