Functions calling functions.

0 favourites
  • 11 posts
  • Ok badly written post, I'll try again.

    Function1 creates sprite1. It then calls function 2.

    Function2 creates sprite2 and pins it to sprite1. In order for function2 to be able to select sprite1, I pass sprite1's UID as a parameter. So when function2 is called, it attempts to select sprite1 from the passed UID, but it doesn't work.

    Even if I give sprite1 a variable and choose it directly in function2, it still doesn't work. It's as though sprite1 hasn't even been created at the moment I reach function2.

    Do I need to leave the first function in order for the newly created sprite to be recognised by further function?

  • Why do you need separate functions?

  • I simplified it a bit, but in my case, I'm creating 4 large boxes and each has about 20 text objects which are pinned to the boxes - it's basically an information panel. They're all similar, but not the same, so I want to do it in functions rather than create every object seperately.

    Anyway, let's assume this is the best method, why isn't it working? Why does the first sprite not register when I jump to the 2nd function?

  • I can't think of any reason, other than the uids are getting mixed up some how.

    You might try using global variables.

    Otherwise try making an example capx and posting it.

    One other thing to think about is using the on creation triggers along with a variable comparison.

    We might ask Ashley to add an expression to get a named function's parameter.

  • It's "the thing" which is really frustrating about C2 at the beginning. You can't pick the Sprite1 by UID untill the whole "event chain" is finished (function2 is a part of this event chain, because it was called by the function1). The thing is Sprite1 is already picked right after it is being created. There's a small hack, how to solve this problem: before picking Sprite1 by UID use "wait 0 seconds" command (this command breaks the event chain). But beware, that under some circumstances the whole thing could be unstable because of this hack. Or call function2 from the separate event. I really dislike this "feature".

  • Ahh ok, that's exactly what's happening. I'm glad I checked because I've run through my code a hundred times now and tried various ways around it but you're right, sprite1 in this instance simply doesn't exist until I've completed the event chain.

    Right now I can't think of a better way to do this in my program but at least I know what's happening now and so I simply have to find an alternative. I don't like the idea of using 'Wait 0 seconds' because as you say, it sounds a bit unstable. I'll just have to find a way of seperating the functions.

  • If anyone else comes across this problem and isn't sure where one event chain ends and another begins (I wasn't), it's basically an entirely new event. For example, in my example above, I couldn't even split this into 2 functions and do 'Call Function1', then 'Call Function2' below it because it's still part of the event chain, and the objects created in Function1 still weren't available.

    The only solution was to do 2 completely seperate 'On start of layout' events. So I've got 'On start of layout - Call Function1', then 'On start of layout - Call Function2' below it. The objects created in function1 are now avaiable to function2.

  • Just store the uid in a global.

    Create sprite1

    ->set gvariable to sprite one uid

    ->call function

    on function call

    ->create sprite2

    ->set xy to sprite1(gvariable) xy

    Also the wait action is just as viable as using a function, since all your doing is skipping a tick.

    Only you at least get to keep what was picked with "wait"

  • Yeah, I guess my solution is messy anyway so a global variable can't be any worse.

    The wait action is the cleanest. Since I'm doing this on startup timing isn't important anyway. Is there any danger that this might be unstable though?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well I wouldn't use 0 seconds.

    0.10 to 0.25 should be ok without being too noticeable.

    And I say that only since mobiles are kind of wonky as far as how well they keep track of time.

    Something else you might consider.

    Containers work by creating all objects associated, and are automatically picked on creation.

    You can, in theory, just set any unwanted objects to invisible/ 0 opacity.

    So basically, if sprite2 is in a container with sprite1, then sprite2 will be created at the same time, and can be referenced in the same event.

  • If anyone else comes across this problem and isn't sure where one event chain ends and another begins (I wasn't), it's basically an entirely new event.

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