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

Get help using Construct 2

Post » Sun Mar 29, 2015 9:18 pm

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:

Image

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:

Image

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

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.
Last edited by Jay K on Tue Mar 31, 2015 12:41 pm, edited 2 times in total.
B
2
Posts: 2
Reputation: 154

Post » Sun Mar 29, 2015 9:36 pm

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 ? :cry:
I'm not very good in english for explanations...
B
14
S
4
Posts: 112
Reputation: 1,287

Post » Mon Mar 30, 2015 9:51 am

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. ;)

Edit:
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:

Image
B
2
Posts: 2
Reputation: 154


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, richcoz81 and 34 guests