[SOLVED] How do I address the right object instance?

0 favourites
  • 3 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Okay, I am desperate, I tried for some days to figure out how to address an individuall sprite and alter its instance variable 'angle' but nothing works.

    Here is what I have done so far to create my game:

    What it is supposed to do is:

    When I press the 'F'-Button i want to spawn a sprite and then call further functions to set some instance variables of the just create sprite.

    I want for example to set the angle of the the red team's sprites to 180 degree.

    Now, when I press 'F' the sprite is created but its angle is not changed:

    It changes only the moment I create a second sprite by pressing 'F' again:

    What did I do wrong?

    How can I change the angle of the first sprite without having to press F again and spawn a second one?

    I already tried to use several pick conditions for the functions 'setFactionRed' and 'set_Tri-Fighter' like for example "pick highest instance variable PilotIDNumber" or pick via "comparing PilotIDNumber to global variable latestSHIPID" but both didn't work.

    Thank you very much for your time invested.

  • Hello,

    The problem you have is probably related to SCOPE, object picking and object creation. The functions have a scope, itself, and almost everything outside is ignored. So when you call the "spawn_ship" function, it works as expected. But when you call after that the function "set_FactionRed", the latest created SHIP_small instance is out of scope. Also, when you create an object, you can just set up its variables in the first following event scope. When you press F a second time, your function "set_FactionRed" will get al last the first instance of the SHIP_small object type, due to the fact that objects are accessible the next tick they are created (except modifying them just in the same event scope after system => create object). That's why operations in the "spawn_ship" function works correctly but not operations from the "set_FactionRed" function.

    I hope it's not confusing...

    So what you can do is putting everything in the first function (quick but not very practice), or put the "returnValue" property of your functions with the UID of the nely created object (Object.UID, not ID as it's not reliable) pass it (Function.ReturnValue) to the functions that are following. In these functions, you have to pick the instance by UID first (so in a function, it's with Function.Param(something)) and do the modifications you want.

    Not confusing ?

    I'm not very good in english for explanations...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hello Nabu, thank you for your advice I really appreciate it.

    I think I understand what you mean.

    Now at the moment I don't have acccess to my computer since I am at work but as soon as I'll get home I will try to pass the UID to the following functions via function parameter and return value. I have to admit I didn't try that one.

    Thank you again for your help, I will let you know whether I was successfull or not.


    Thanks to your input Nabu I found 2 ways of making this work, the first and simpliest was to add a wait event right after calling my "spawn_ship"-function.

    I followed your suggestion though to work with the UID as a retrun value which also works just fine and is in my oppnion the more elegant way of solving this:

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