Howdy, Stranger!

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

adding functions

Hi,

I've created a variable containing some functions in my intro page eg.
<<set $time={
    day=0,
    hour=7
}>>
<<set $misc={
    isMallOpen: function() {
        return ($time.hour lte 18);
    },
    haveSchool: function() {
        return ($time.day % 7 gt 0) && ($time.day % 7 lt 6) && ($time.hour lt 12);
    },
    canSleep: function() {
        return ($time.hour gt 15);
    },
    test: function() {
        return false;
    }
}>>\

with the plan to use them like this in other passages:
<<if $misc.canSleep>>[[Go to sleep]]<<endif>>

But, all the functions are returning true, or rather, all the if macros are treating $misc.<function> as true.

What am I doing wrong here? Or can I not create functions within objects like this?

Many thanks for your help.

Comments

  • Never mind. I found a way after much trial and error.

    I set up a series of widgets (using sugarcube 2) eg.
    <<widget "canSleep">>\
      <<if $time.hour gt 15>>\
        $args[0]
      <<endif>>\
    <</widget>>\
    

    and called like this
    <<canSleep "[[Go to sleep]]">>
    
  • bobbob wrote: »
    with the plan to use them like this in other passages:
    <<if $misc.canSleep>>[[Go to sleep]]<<endif>>
    
    But, all the functions are returning true, or rather, all the if macros are treating $misc.<function> as true.

    What am I doing wrong here? Or can I not create functions within objects like this?
    You're evaluating a reference to the method itself, which will always be truthy, rather than invoking it.

    You should have done something like the following:
    <<if $misc.canSleep()>>[[Go to sleep]]<</if>>
    
    Note the parenthesis at the end of the method name.
Sign In or Register to comment.