Doubts about functions

0 favourites
  • 7 posts
  • I don’t understand how this interaction between functions works.

    I’ve isolated this interaction from a much complex system of functions but the way it works on the original is:

    I add items to an array with a function but it can only add 1 kind of item at a time. So when I want to add multiple types of items I call for multiple functions with different parameters. At the end of this function calls an “Update” function that creates the objects in game to display on the correspondent slot. And it worked great until I started polishing the number of actions per tick, etc.

    Then I realised that functions executed from a trigger start at the same time or that’s what I think… So they end up overlapping conditions and duplicating the number of objects to create.

    I realise I can fix this by adding a Wait action between “Add” functions. But I don’t quite understand triggers, functions, and wait when they all work together.

    For example sometimes a function that’s at the end of the action list after a condition, triggers before other actions even tho it’s placed at the very end, I usually fix this by using wait. But this generate other kind of problems that I haven’t tested yet but it appears that using wait on some functions erase the parameters so following event on that function won’t be able to use them.

    (but I’m not sure that is what it’s happening.)

    So my question is: What’s the best way to go about using this functions all at once? Should I place wait actions after every function call? Or Is there a better way?

    Here’s the capx and a screenshot:

    dropbox.com/s/4668ibs6wdgs3kt/Debugging_Update_Problem.capx

  • So when W is pressed, you call function "add" 3 times. In "add" function you call "update", so it goes add->update->add->update->add->update. These 6 function calls are performed one after another, but all in one tick.

    Inside the "update" function you check if Item with ID exists, if not, you create it. Object instance is not created immediately, you need to wait till the next tick before you can pick it by its instance variable. That's why your code works differently when you add "Wait 0" before function calls - it allows time for Item to be created.

    It's possible to fix this without "Wait 0" - for example, you can set some flag in the array when Item is created, but I don't really understand your code, so can't help with this.

    Also, if you need to see the order in which events are executed, add Browser Log action to your functions. Press F12, and you will see log messages in console. It's a powerful debugging method, you can output variable values, array contents, function parameters etc. and see how they are changing.

  • What do you mean you don't understand my code is it that bad?

    you better not see the original functions then xd

    I'll have to have a look that browser log since you cant use breaks on triggers or loops I've been updating a textbox placed where I think the problem is.

    I may try the array flag but it does have to be inside of the array right? it can't be an instance variable because it'll be the same as with the item?

    Here's another case I have this debug menu where I can select multiple items and set an amount once the amount is set calls a function with a loop.

    which calls the "add" function every time one of the counters variable is more than 0. This add function calls the update function as in the previous example, and it does the same conditions, so in this case is there anyway other than the array flag to delay the trigger of the next condition in the loop? I read people use every X seconds but since this is a trigger function I cant use that, right?.

    Maybe I should just use more local variables and arrays. If they update instantly instead of having to deal with this delays.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's not a bad code, I just didn't really try very hard to figure out what it's supposed to do :)

    As I understand, the issue is with picking instances of Item object immediately after creating them. You can only pick a newly created instance in the same tick by UID. If it's not possible, you can use wait 0 or find some other workaround. Maybe use a different array to keep track of created items or something.

  • I’m reposting this because the Forum didn’t display the image and the post didn’t appear s the last post on the topic. Sorry

    Unfortunately it seems that the issue is not with picking instances. I further simplified the code but it acts the same:

    I'm really lost right now I don't understand anything. What am I missing?

    https://www.dropbox.com/s/i65uh9hjuwask9n/Debugging_Update_Problem_Small.capx?dl=0

    Ashley Is there anything special going on here?

    TLR: why does the final action performs 3 times even though the functions are called twice.

  • Your Update function is called twice, but the second time there are two "1" values in the array, that's why you see total 3 "hellos" in the log.

    Add more log outputs and you'll be able to see this:

  • Yes, I missed that one I'd been looking at it for so long that while trying to simplify it I overlooked that. But the original problem was with the instance variables.

    I've redone the way the items update, so once every single add functions has finished it updates only once instead of once per add function. I think it's also better practice performance wise. I also learnt about the browser log so thanks for not giving up on me xd.

    I read about wait on "loops" using loopindex so I'm playing with that now.

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