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

  • perchance@lemmy.worldM
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    11 days ago

    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 from mob. The reason for that is because you’ve made s equal to [mob.selectOne] instead of mob.selectOne. The square brackets around it effectively make it “dynamically fetched” each time you access s.

    (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 [s = mob.selectOne, ""] 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 a mob item and put it in the s variable before the output list is executed (which uses the s 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!)

    • BluePower
      link
      fedilink
      English
      arrow-up
      1
      ·
      13 days ago

      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.

  • BluePower
    link
    fedilink
    English
    arrow-up
    2
    ·
    14 days ago

    You can use the shorthand exp ? if_true : if_false notation chained together instead of the if ... 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]]