Howdy, Stranger!

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

sugarcube setter link seems to be unsetting my variables.

I'm using twine 2 - online/browser based
Sugarcube 1

So I'll set
$self_worth
at the start of my story. I display that and other variables' values in the sidebar with StoryMenu
Later on in the story I'll use a setter link to change that variable.
like so..
* goes very well, [[and she confronts Dean|break up or not][$self_worth -= 5]]

in the next passage, the $self_worth value no longer displays in the sidebar.
When I try to manipulate the $self_worth variable to see whats going on...
<<set self_worth -= 5>>

I get this error:
Error: <<set>>: bad evaluation: self_worth is not defined.

Why is the setter link unsetting my variable, what am I doing wrong?

Comments

  • You forgot the story variable sigil ($).

    Instead of:
    <<set self_worth -= 5>>
    
    You should have done:
    <<set $self_worth -= 5>>
    

    You should likely ensure that you aren't doing that elsewhere.
  • Ah I typed it in here wrong. Here's the actual code.
    * goes very well, [[and she confronts Dean|break up or not][$self_worth -= 5]]
    
  • edited February 2016
    Ah I typed it in here wrong. Here's the actual code.
    * goes very well, [[and she confronts Dean|break up or not][$self_worth -= 5]]
    
    That part is exactly the same as in the opening post—the variable has its sigil in both.

    It's the <<set>> macro you gaffed on (as shown below). Whether just here or also in your code, I couldn't say. Though, going by the error message, I'd say in your code as well (error: "bad evaluation: self_worth is not defined"; note the missing sigil).
    When I try to manipulate the $self_worth variable to see whats going on...
    <<set self_worth -= 5>>
    
    I get this error:
    Error: <<set>>: bad evaluation: self_worth is not defined.
  • edited February 2016
    Ah right you are!

    That was just a test though. So my error message is gone, but if I try to print that variable nothing prints to the screen, which leads me to believe I'm unsetting it somehow.
  • The setter looks normal. It should subtract 5 from $self_worth. So, I doubt that's your problem. Are you initializing it anywhere?
  • Yeah at the start of the story:

    <<set
    $agency = 40,
    $self_worth = 50,
    $selflessness = 30,
    $janelle_rapport = 50
    >>
  • Conversely if I change the link, to be a non-setter link
    * goes very well, [[and she confronts Dean|break up or not]]
    
    it works fine.

    Let's say at this point in the story the self worth stat is at 55, if I use the above code the stat remains at 55, if I use the setter code, it isn't even set to 0, nothing prints at all =/
  • @cocoaflannel: I have attached an example Archive file that demonstrates showing $self_worth in the side-bar as well as updating it's value using a setter link.

    Use Twine 2's Import From File option to add the example to your Storyies list.
  • What happens if you use:
    * goes very well, [[and she confronts Dean|break up or not][$self_worth to $self_worth - 5]]
    
  • myKael - same problem =/

    I'm going to try using the above archive file though. I think I might post my own archive file later.
  • Ah I think I've figured out my problem.
    So in an earlier passage I have this code.
    <h3>some basic outcomes of the scene</h3>\
    <b>on housemates</b>\
    <<silently>>
    <<set $sw_change_1 = $self_worth - 3>>
    <<set $sw_change_2 = $self_worth - 5>>
    <<set $sw_change_3 = $self_worth + 5>>
    <</silently>>\
    * thinks her housemates don't think much of her <<radiobutton "$self_worth" "$sw_change_1" checked>>
    * thinks her housemates hate her <<radiobutton "$self_worth" "$sw_change_2">>
    * thinks her housemates approve of her <<radiobutton "$self_worth" "$sw_change_3">>
    

    If this code is not present the setter link won't unset the self worth variable.

    I think I'm just going to avoid doing the above (this isn't a good means of doing what I want to do) from now on, so we could close the issue, but if anyone knows why the above code is problematic please let me know.
  • Your <<radiobutton>> invocations are erroneous. You should only be quoting the receiver variable (since we want its name, not its value). By quoting the $sw_change_* variables as you are, instead of passing in their value, you're passing in their name as a string (i.e. "$sw_change_1" passes in the string "$sw_change_1", while $sw_change_1 passes in whatever number $self_worth - 3 yields).

    For example, instead of this:
    * thinks her housemates don't think much of her <<radiobutton "$self_worth" "$sw_change_1" checked>>
    * thinks her housemates hate her <<radiobutton "$self_worth" "$sw_change_2">>
    * thinks her housemates approve of her <<radiobutton "$self_worth" "$sw_change_3">>
    
    You should be doing the following:
    * thinks her housemates don't think much of her <<radiobutton "$self_worth" $sw_change_1 checked>>
    * thinks her housemates hate her <<radiobutton "$self_worth" $sw_change_2>>
    * thinks her housemates approve of her <<radiobutton "$self_worth" $sw_change_3>>
    

    As a general rule: Never quote variables passed as arguments to a macro unless the macro's documentation specifically tells you to, and only the specific variables you are told to. Specifically, the only macros which require this are the various input macros (e.g. <<checkbox>>, <<radiobutton>>, <<textbox>>, etc), and then you only quote the receiver variable.


    The reason the value of $self_worth was disappearing from the UI bar is that by the time your setter link is clicked the $self_worth variable was set to a string (one of: "$sw_change_1", "$sw_change_2", "$sw_change_3"). So, the expression ($self_worth -= 5) was actually try to do something like, $self_worth = "$sw_change_1" - 5, which is nonsensical and yields NaN (Not a Number). NaN, along with a few other special values, doesn't have a printed form (i.e. nothing will print).
  • edited February 2016
    edited: I really wish this forum software would notify you if someone else has posted a reply between the time you start reading/answering a persons question and the time you click on the Post Comment button. Having to remember to hit refresh before clicking on Post Comment to check is getting old.

    @cocoaflannel:
    Assuming you have previously assigned a numeric value to the $self_worth before hand because an undefined variable does not equal zero, then your example works.
    <<set $self_worth to 10>>
    

    note: You don't need to wrap the $sw_change_1, $sw_change_2, $sw_change_3 variables in quotes when you pass their value to the <<radiobutton>> macro, this only needs to be done to the first parameter.
    * thinks her housemates don't think much of her <<radiobutton "$self_worth" $sw_change_1 checked>>
    * thinks her housemates hate her <<radiobutton "$self_worth" $sw_change_2>>
    * thinks her housemates approve of her <<radiobutton "$self_worth" $sw_change_3>>
    
  • Thank you madexile, that is exactly what was causing the problem. Much obliged.
Sign In or Register to comment.