Function behaves strange?

0 favourites
From the Asset Store
Strange factory themed set of tiles for your platformer game
  • Hey there!

    I want to turn my login events into a function because I want to add multiple ways of signing in. For example by button click, by pressing enter etc. and I just want to use a single function for that.

    Here is what the perfectly fine working login events look like:

    So now if I turn that into that function:

    The whole red marked part at the end is skipped. Even though I checked via console that the local var signed_in is indeed set to 1. Or if I put in wrong login information to force an error message (from mongodb) then error_message != "" will be ignored too.

    This is how the login button event with the function looks like:

    Why is that? I even tried using function parameters instead of local variables. And tried setting it to asynchronous, with and without a wait for previous actions.

    In order to circumvent the part that is skipped I'm implementing the skipped part inside the javascript snippet. The login part works just fine. But for the error messages I'm confused as to how I can access the key-value-pairs of my JSON object within javascript.

    I tried a lot of variations of

    runtime.objects.Language.Get("error_messages." & error_message)

    runtime.objects.Language.getItem("error_messages." & error_message)

    runtime.objects.Language[2]

    runtime.objects.Language["error_messages." & error_message]

    runtime.objects.Language["error_messages." + error_message]

    runtime.objects.Language[String("error_messages." + error_message)]

    runtime.objects.Language[String("error_messages." + String(error_message))]

    etc...

    This is what the modified login script looks like

    Please help with either getting the functions last part not getting skipped or with the part how I can retrieve the JSON object from script event. Thank you! :)

  • Even though I checked via console that the local var signed_in is indeed set to 1.

    The only possible explanation why the marked events are skipped is that the variables checks return false.

    I'm not sure if you can use "Wait for previous action" to wait for the script to finish executing. My guess is that it just waits one tick and continues running other events, while logged_in value is still 0.

    I would suggest adding Browser Log after "Wait for previous action" action to check the value of logged_in variable.

  • I would suggest adding Browser Log after "Wait for previous action" action to check the value of logged_in variable.

    I will test that tomorrow. But what's odd is that it is working outside of this function.

    Edit:

    Just tested it and it seems like that inside of the function the wait for previous actions to complete is ignored. But it is not ignored if I use the exact same event structure in the button directly.

    Javascript await + wait for previous actions will wait until the script is finished. But for some reason it will not do this in a function.

  • Maybe the script sometimes runs a bit faster and can actually finish in one tick. You can easily test this by replacing "Wait for previous action" with "Wait 1 second".

  • You can easily test this by replacing "Wait for previous action" with "Wait 1 second".

    That still returns 0. :-o

  • Ok, I was wrong, "Wait for previous action" actually does wait for the script to finish if there is "await" in the script. But I tested and it works the same regardless of whether it's inside a function or not.

  • But I tested and it works the same regardless of whether it's inside a function or not.

    Does this mean that there is something wrong with how I ordered the events?

  • I don't know.. My method of debugging issues like this is to add console output to all events and into the script - to see the order in which they are executing and how the value of the variable changes.

  • You are right. I will thoroughly debug later and give an update.

  • Try setting "logged in" and "error message" as a static variable maybe? Otherwise they will reset to default value after a tick, e.g. after the wait. But I'm not sure why it works when not in a function.

    You could also split the function up some more, and instead of setting local vars you call a "login success" or "login error" function (runtime.callfunction) with whatever parameters you need.

  • Try setting "logged in" and "error message" as a static variable maybe? Otherwise they will reset to default value after a tick

    Local variables in functions are essentially static and are not reset until the function is finished.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • How did I not know that until now? :V Well in that case I don't know either. Splitting it up as I described probably works around whatever issue that is but I'd be curious to know what the issue actually is.

  • Nilom

    I can actually reproduce this issue - the value set to the logged_in variable in the script is not saved for some reason. And WackyToaster is right - setting the variable as static fixes it!

    So even though variables in functions preserve their values after "waits", seems like they still work differently from static variables.

  • Heh, interesting. The value IS static as dop described unless it's set inside a promise. Question is if that's a bug or a feature. It's fairly straightforward to workaround at least.

    It's also odd that it does work outside the function. The promise probably resets/exits the function and triggers the variable reset or something.

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