Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Can you use random events to change tags.

I was wondering if you would be able to say have a random number and then if the number was say, 1 then add tag happy.
I tried using the following code but it came up with this error: "Error: <<if>>: assignment operator found within <<if>> clause (perhaps you meant to use an equality operator: ==, ===, eq, is), invalid: $random="1" add tag happy"
<<print random(1,2)>>
<<if $random="1" add tag happy>><</if>>

Comments

  • First. You should always state the story format you're using and its full version, because advice will tend to vary based on that information. Based on the syntax you're attempting to use and the error message, it's clear you're using some version of SugarCube, though known which version—the full version mind you—is still important.

    Also. Please use the code tag—it's C on the editor bar—when posting code or markup.

    stephen99 wrote: »
    I was wondering if you would be able to say have a random number and then if the number was say, 1 then add tag happy.
    If you simply want to track a state, like being happy, then you should use a story variable.

    If you're referring specifically to passage tags, then no. Passage tags should be considered immutable. That said, you may add classes easily enough.

    For the moment, however, I'll assume you want to do the former.

    stephen99 wrote: »
    I tried using the following code but it came up with this error: "Error: <<if>>: assignment operator found within <<if>> clause (perhaps you meant to use an equality operator: ==, ===, eq, is), invalid: $random="1" add tag happy"
    <<print random(1,2)>>
    <<if $random="1" add tag happy>><</if>>
    There's so much wrong with your code that anything I say would likely sound like pedantry, so we'll skip right to code examples—though you should probably look over SugarCube's documentation, I'm assuming v2 for now; parts relevant to this discussion: variables, <<set>> macro, <<if>> macro, random() function.

    The first thing you should do is initialize the story variable $happy in the StoryInit special passage—that's simply a passage by that name; see: Special Names for more information. For example, the following sets $happy initially to the boolean value false:
    <<set $happy to false>>
    


    Later on, the following example checks to see if the value returned by random(1, 2) is the number 1 and, if so, sets $happy to the boolean value true:
    <<if random(1, 2) is 1>><<set $happy to true>><</if>>
    
    That could actually be simplified to the following—though perhaps that's a bit advanced for this discussion:
    <<set $happy to random(1, 2) is 1>>
    


    You may check the value of $happy in one of the following ways:
    /* Check for both happy and not happy. */
    <<if $happy>>
    	happy
    <<else>>
    	not happy
    <</if>>
    
    /* Check only for happy. */
    <<if $happy>>
    	happy
    <</if>>
    
    /* Check only for not happy. */
    <<if not $happy>>
    	not happy
    <</if>>
    
  • Thanks heaps for your help what I really wanted to do was change the background colour and font size depending on the random variable, I thought that would be easier with tags, but obviously, that can't be done...
    I'm sorry if that was unclear.
  • edited June 2017
    stephen99 wrote: »
    Thanks heaps for your help what I really wanted to do was change the background colour and font size depending on the random variable, I thought that would be easier with tags, but obviously, that can't be done...
    I'm sorry if that was unclear.

    You can still use classes for this. Tags add classes you can use for styling as a part of what they do, but shouldn't be used dynamically. Classes can be used dynamically.
    <<set $happy to true>>
    
    <<if $happy>>
        <<addclass 'body' 'happy'>>
    <</if>>
    

    Then in CSS:
    body.happy { /* styles */ }
    
Sign In or Register to comment.