Learn JavaScript in Construct, part 2: language basics

You're viewing a single comment in a conversation. View all the comments
  • 3 Comments

  • Order by
  • My project contains only an 'On start of layout' event block with "let b = "Hello";" in it but when I run the game, the variable 'b' returns as undefined until I manually declare it in the console.

    Is this normal behaviour or am I missing something? This is the case with any variable I make - for some reason I'm not able to declare on runtime through an event block.

    • To be more specific, the console returns:

      "Uncaught ReferenceError: b is not defined

      at <anonymous>:1:1"

        • [-] [+]
        • 1
        • Ashley's avatar
        • Ashley
        • Construct Team Founder
        • 1 points
        • (0 children)

        That's normal - the console basically works in global scope, and it can't access variables declared in other functions (and script blocks in event sheets are actually inside functions), just like how you can't write code in one function that accesses a local variable inside a different function.