Howdy, Stranger!

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

Twine 1.42 Sugarcube 2.16 Widgets newbie question

edited April 2017 in Help! with 1.x
<<for _i=0; _i < $timetable.length; _i++>>\
	<<if  ($timetable[_i][0] eq $dayofweek+1)>>\
		<<print "You have a lecture on ">>
		<<for _m=0; _m < $courselist.length; _m++>>\
			<<if  ($courselist[_m][0] eq $timetable[_i][2]>>\
				<<print $courselist[_m][1]>>\
				<<set _m = $courselist.length>>\
			<<endif>>\
		<</for>>\
	<<endif>>\
<</for>>
<<for _i=0; _i < $timetable.length; _i++>>\
	<<if  ($timetable[_i][0] eq $dayofweek+1)>>\
		<<print "You have a lecture on ">>
		<<getcoursename $timetable[_i][2]>>
	<<endif>>\
<</for>>\

Passage: Main Widget
<<widget "getcoursename">>\
	<<for _m=0; _m < $courselist.length; _m++>>\
		<<if  ($courselist[_m][0] eq $args[0])>>\
			<<print $courselist[_m][1]>>\
			<<set _m = $courselist.length>>\
		<<endif>>\
	<</for>>\
<</widget>>\

To save your time, yes, the two sections of code are the same, except for changing $timetable[_i][2] with an $args[0] and put in the widget passage.

The first one worked. The second one did not. getcoursename always returns nothing.

Any clue?

Comments

  • In most cases when passing discrete arguments to a macro or widget, property access is considered to be using an expression—see: macro library documentation (at the top).

    You should be doing one of two things here:
    1. Assigning the value to a temporary variable. For example:
      <<set _course to $timetable[_i][2]>>
      \<<getcoursename _course>>
      
    2. Using a backtick expression. For example:
      <<getcoursename `$timetable[_i][2]`>>
      

    Other minor issues I noticed:
    1. The <<end…>> versions of ending tags are deprecated. Using them is disrecommended.
    2. To break out of a <<for>> loop, use its <<break>> rather than manually manipulating the loop index.
    For example, instead of this:
    	<<for _m=0; _m < $courselist.length; _m++>>\
    		<<if  ($courselist[_m][0] eq $args[0])>>\
    			<<print $courselist[_m][1]>>\
    			<<set _m = $courselist.length>>\
    		<<endif>>\
    	<</for>>\
    
    Do something like the following instead:
    	<<for _m = 0; _m < $courselist.length; _m++>>\
    		<<if $courselist[_m][0] eq $args[0]>>\
    			<<print $courselist[_m][1]>>\
    			<<break>>\
    		<</if>>\
    	<</for>>\
    
  • 1. temporary variable - probably a work around to the risky args[]?
    2. what is the use of a backtick expression? pointer related? when should I use it?
    3. endif - Got it. I used it to backward compatible with my old tws. Come to think of it, I dont really see the point.
    4. it is so confusing to remember which language has BREAK and which does not. okay, so sugarcube has BREAK, that is good. sugarcube does not have WHILE loop nor the DO loop, right?
  • pingguo wrote: »
    1. temporary variable - probably a work around to the risky args[]?
    Risky $args array? I have no idea what you mean.

    pingguo wrote: »
    2. what is the use of a backtick expression? pointer related? when should I use it?
    Neither SugarCube's macro syntax nor JavaScript have pointers, so it's definitely not pointer related.

    It's simply a way to pass the result of an expression as a discrete argument to a macro, which—outside of a few special cases—is problematic because the macro argument parser doesn't treat arguments as expressions by default and because it separates arguments with whitespace.


    Both of the options I outlined—the use of a temporary variable or a backtick expression—are to allow you to pass the result of the property access on $timetable as a discrete argument to the widget.

    pingguo wrote: »
    […] sugarcube does not have WHILE loop nor the DO loop, right?
    Not as macros, no. That said, SugarCube's <<for>> macro is multipurpose. You may invoke it with no expressions, a conditional expression only, or with a 3-part set (init, conditional, and post expressions). For example:
    → No expressions; loop until <<break>> is used
    <<for>>
    …
    <</for>>
    
    → Conditional expression only; analogous to 'while' loop in many languages
    <<for conditional_expression>>
    …
    <</for>>
    
    → 3-part set; analogous to 'for' loop in many languages
    <<for init_expression ; conditional_expression ; post_expression>>
    …
    <</for>>
    
  • pingguo wrote: »
    1. temporary variable - probably a work around to the risky args[]?
    Risky $args array? I have no idea what you mean.
    My bad. I read macros.html#macros-widget and it said, "This means that non-widget use of an $args variable is safe" and I probably wrongly wired it up with "it is not safe at some other time."
    Both of the options I outlined—the use of a temporary variable or a back-tick expression—are to allow you to pass the result of the property access on $timetable as a discrete argument to the widget.
    Mmmm. Okay. I do not recall something like back-tick expression in other programming languages. Is it Twine and/or Sugarcube only? No wait, an alien is popping up at the back of my mind. UNIX shell scripting?
    Not as macros, no. That said, SugarCube's <<for>> macro is multipurpose. You may invoke it with no expressions, a conditional expression only, or with a 3-part set (init, conditional, and post expressions).
    Programming languages are so different with each other and sometimes it comes down to habit. I can manage myself. It is just I feel solid with a clarification from an expert.

    Many thanks, TheMadExile. You are always the greatest help.
Sign In or Register to comment.