Functions don't register external local variables

0 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Problem Description

    The capx is simple enough, but basically, it seems that functions don't occur in order with the event sheet when local variables are concerned. I'm not 100% sure that it's actually a bug, but it does seem a little unexpected. Hopefully I didn't waste too much of your time if this isn't a bug, it should be fairly easy to workaround using function parameters.

    Description of Capx

    Sets the text object to a text value

    Observed Result

    No text appears

    Expected Result

    Text appears

    Affected Browsers

    • Chrome: (YES)
    • FireFox: (YES)
    • Internet Explorer: (YES)

    Operating System and Service Pack

    Win7 64bit

    Construct 2 Version ID

    r173

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Swap actions on event number 2 to 4

    Make sure to check "Static" on this local variable.

  • Joannesalfa well yeah that will work, but this is a simplified capx to show an issue with functions.

  • This isn't a Function issue, it's a Trigger issue.

    Manual:

    [quote:1k46yh8e]Since triggers run upon an event happening, they aren't checked in top-to-bottom order like other events. This means the ordering of triggers relative to other events is not important (except relative to other triggers of the same type, since triggers still fire top-to-bottom).

    A trigger doesn't have any context outside of itself. Just because you place it in a certain location doesn't mean it has context in that location.

    Move your 'Text set' to 'On start of layout' - also ignored, since it's a trigger.

  • Right, OK, yeah, I thought as much. Seemed weird to me when I posted this report but that makes sense.

  • This is actually by design. When calling a function, it actually creates a new copy of each non-static local variable to work with at that level of nesting. This is important so recursive functions can use local variables like you can in real programming language: each level of nesting has its own unique local variables which have their own independent values, so each level of logic can work independently.

    If you don't want that to happen, the workaround is easy - just make it static.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)