Howdy, Stranger!

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

[SugarCube 2]Calling a macro or JavaScript inside an

Hey there!

Problem description:
I'm trying to return a value to SugarCube 2 by calling a macro that I made.
Let's see the code:

JS:
var weekdays = new Array();
weekdays[0] = "Sunday";
weekdays[1] = "Monday";
weekdays[2] = "Tuesday";
weekdays[3] = "Wednesday";
weekdays[4] = "Thursday";
weekdays[5] = "Friday";
weekdays[6] = "Saturday";
var date = new Date(2017,2,1,17,30,0);

Macro.add(
{
    "todayis", function()
    {
        return weekdays[time.getUTCDay()];
    }
})
;

Passage:
/% Kiosk Passage: %/
<<linkreplace "Find out the current date by looking at the date of a daily newspaper>>
    <<if <<todayis>> is "Wednesday">>
                Oh my, how is this possible? It's Wednesday. 
                But I thought today is Friday?
                Does that mean my machine worked? 
                I traveled through time?
    <</if>>
<</linkreplace>>

Unfortunately this fails with an error: saying there's an extra << symbol that shouldn't be there.
Help please?

Comments

  • Anyways, if this has not came across. I want the value returned by JS to be compared in the <<if>> statement inside the passage. Is this possible in some way?
  • You cannot call a macro within the argument(s) of another macro, so you'll need to make it a function—you also screwed up the macro definition, so it wouldn't have worked regardless. Also, your array creation is needlessly complex, use an array literal.

    Try the following:
    /*
    	Keep all of these together within the same scope.
    */
    
    var weekdays = [
    	"Sunday",
    	"Monday",
    	"Tuesday",
    	"Wednesday",
    	"Thursday",
    	"Friday",
    	"Saturday"
    ];
    var gameDate = new Date(2017, 2, 1, 17, 30, 0);
    window.today = function() {
    	return weekdays[gameDate.getUTCDay()];
    };
    
    The today() function is assigned to the window object so that it will be globally available.

    And the passage:
    /% Kiosk Passage: %/
    <<linkreplace "Find out the current date by looking at the date of a daily newspaper>>
        <<if today() is "Wednesday">>
                    Oh my, how is this possible? It's Wednesday. 
                    But I thought today is Friday?
                    Does that mean my machine worked? 
                    I traveled through time?
        <</if>>
    <</linkreplace>>
    
  • edited February 2017
    Nice, thanks for the help man. I really appreciate it!
  • Something I missed before. You're missing a closing quote on the <<linkreplace>> text. The following:
    <<linkreplace "Find out the current date by looking at the date of a daily newspaper>>
    
    Should be:
    <<linkreplace "Find out the current date by looking at the date of a daily newspaper">>
    
  • True, thanks again! I find your answers very helpful.
Sign In or Register to comment.