Pick Object Created in a Function

Get help using Construct 2

Post » Sun Dec 11, 2016 12:27 pm

I know I have seen the answer to this, but I've tried searching and I've failed miserably to find it. If I have created an object in a function, and I then I want to pick that object. I am creating things procedurally within the function, so I know what family the object is in, but not necessarily the object itself (hence why I can;t just pick it directly)

Is there a way to pick a family object by using 'Pick Nth' or perhaps by it's IID/UID? I'm sure I remember something like the IID will be -1 or something? I just can't remember, any help appreciated
B
15
S
5
Posts: 141
Reputation: 1,657

Post » Sun Dec 11, 2016 12:50 pm

It may help: https://www.scirra.com/tutorials/361/un ... moke-trail

By the way, I'll explain you the easiest way to pick the object.

First, you should know its UID or at least something to identify it in the runtime.

I prefer to use instance variables in the object itself, works great too.

When you create the object in the function, set the instance variable in the sequence, like this:
Image
Sample CAPX: https://dl.dropboxusercontent.com/u/470 ... ction.capx
Image
B
104
S
23
G
17
Posts: 1,350
Reputation: 22,028

Post » Sun Dec 11, 2016 1:18 pm

Thanks for the reply. Unfortunately I've just realised that I'm coming up against a limitation of Construct 2, which is explained in this thread:
https://www.scirra.com/forum/viewtopic.php?t=73647&start=0#352637

As per the explanation in the topic, I can't take any action on the object I just created within the rest of the function, or rather I can but only by explicitly referring to it within its originating subevent

So I can't do the below (which is just an example of the type of thing I'm, trying to do)
Image

I realise there are many easy ways to do what I need by putting those global variables under each of the creation subevents, but when there is going to be 100+ then it's just repeating the same code 100 times. I haven't found any easy ways of procedurally creating objects really. I have tried the Wait 0 Seconds, which does sort of work, but when doing things like setting Z Indexes you get a nasty frame of the item being on top and then popping to where it should be.

I will think of an alternative way to do it
B
15
S
5
Posts: 141
Reputation: 1,657

Post » Sun Dec 11, 2016 1:33 pm

I prefer to use events instead of instances, where you're able to setup everything in a visual way, but that's just a personal preference.

Did you tried to run it once using the "Run Once While True"?

Without analyzing the whole code I can't help you to workaround it with a different logic.
Image
B
104
S
23
G
17
Posts: 1,350
Reputation: 22,028

Post » Sun Dec 11, 2016 2:14 pm

First, use int(random(1,5)) the get the numbers 1 to 4. Second, lookup the Nickname plugin. It will let you name your objects so that they can be created dynamically (use a number as the name in this case). That should help a lot.
ImageImageImage
B
71
S
21
G
221
Posts: 3,649
Reputation: 124,227

Post » Sun Dec 11, 2016 2:38 pm

@blackhornet I didn't know about the nickname plugin, that looks exactly what I need, thanks!
B
15
S
5
Posts: 141
Reputation: 1,657


Return to How do I....?

Who is online

Users browsing this forum: David181, InfiniteRockets, marcinkowski and 16 guests