Howdy, Stranger!

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

Debugging a widget in SugarCube 2

edited August 2016 in Help! with 1.x
I'm trying to debug a widget I made. There an undefined in it somewhere. The debug mode doesn't add anything to the error displayed:
Error: <<myWidget>>: cannot execute widget: Cannot convert undefined or null to object

Is there a trick to getting the widget to parse and only stop on the error? Besides commenting out line per line. It's just a blob of text with naked variables.

Comments

  • edited August 2016
    We'd probably need to see the widget contents to help?

    First glance it sounds like you're trying to do something with a variable that hasn't been initialised.
  • I'm looking for a way to debug it myself, the message doesn't help. I've found the error but I would like to know if there was a way better than commenting out stuff until I find out whats wrong.
  • One of the oldest methods of debugging is to output messages to either the screen or console.
    <<print "got here 1">>
    Some code
    <<print "got here 2">>
    
    or
    
    <<script>>console.log("got here 1")<</script>>
    Some code
    <<script>>console.log("got here 2")<</script>>
    

    Depending on what you are doing you can even include information about the state of the environment.

    1. In passages or a widget:
    <<set $var to 10>>
    <<print "var: " + $var>>
    <<set $var += 15>>
    <<print "var: " + $var>>
    
    or
    
    <<set $var to 10>>
    <<print "<<script>>console.log('var: " + $var + "')<</script>>">>
    <<set $var += 15>>
    <<print "<<script>>console.log('var: " + $var + "')<</script>>">>
    

    2. In script tagged passages:
    var age = 10;
    console.log("age: " + age);
    
  • edited August 2016
    So there isn't anything similar to "debugger;" for SugarCube macros?

    If I turn my widget into a passage, change args to a global variable, and go to that passage, it goes through it until it fails. That's pretty convenient but requires a bit of reworking, noticably turning the widget into a passage.

    Maybe I could try plopping <<run debugger; >> all over the widget to see between witch runs it crashes.
  • I am assuming when you say "debugger;" you mean the Javascript debugger statement, which seems to be ignored by the SugarCube Javascript engine code that interprets and converts the contents of a passage into the corresponding HTML/Javascript elements.
Sign In or Register to comment.