Pairing objects at runtime

For questions about using Classic.

Post » Wed Jul 23, 2008 11:13 pm

How would I go about pairing two instances of different objects at runtime, like containers, but chosen at runtime.

What I need is for on collision between object1 and object2, object1 always sets position to object2. (for a while anyway) and these two objects need to stay connected like this. There are multiple object1's and object2's and others pair together aswell.
I've tried things with object IDs but couldnt get it to work, the best I can do to pick the instances on overlap between these objects, but this bugs out constantly.
Reminiscent of when I've tried pairing things without containers (horrible, horrible :( )
B
2
S
2
G
5
Posts: 236
Reputation: 2,122

Post » Wed Jul 23, 2008 11:26 pm

Is there always one object2 attached to an object1? If so, containers will do the job. Otherwise, if you explain what you're trying to achieve a bit more, we could come up with a bit more of a specific way to help you...
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,630

Post » Wed Jul 23, 2008 11:47 pm

No they're not always paired, none are paired at the start of the frame, then they pair up as they collide with eachother.
Basically like with containers, if object1 and object2 were in the same container, then doing:
always-> object1 set position to object2
the instance of object 1 would set its position to the instance of object 2 it is assigned to via the container
I need this sort of effect, but for the instances to be paired up at runtime.

what I have is loads of object1s and loads of object2s floating around the screen, and I want when one instance object1 collides with an instance object2, these two instances are paired, and I can just add an event to anything else where I need one object to select its assigned instance of the other.

Like having object1 collides with object2 -> object1 sets private value 'OBJ' to object2.OID
then,
when object2's OID = object1.value('OBJ') (pick these two instances) -> object2 set position to object1

I can imagine there is a simple way to do it, but as i said the best thing i've been able to come up with is
object1 overlaps object2 -> object2 set position to object1
which bugs out if for example object1 moves fast

Maybe it could be done with for all object loops? I've tried sortof placing for all object 1 and for all object2 in the event arbitrarily and shuffling them about with no success (unsuprising really).

Hope I've explained better
B
2
S
2
G
5
Posts: 236
Reputation: 2,122

Post » Thu Jul 24, 2008 3:02 am

I was actually working on something similar today, so I whipped up a quick example for you:

Object pairing

It's commented, so it should be fairly easy to understand. Hope that helps. :)
Moderator
B
99
S
38
G
19
Posts: 1,584
Reputation: 17,942

Post » Thu Jul 24, 2008 2:40 pm

I love you :D
B
2
S
2
G
5
Posts: 236
Reputation: 2,122

Post » Thu Jul 24, 2008 3:18 pm

[quote:2g3so7ue]FAGgatron: I love you :D[/quote:2g3so7ue]
Haha i bet you do XD, nice example link.
B
4
S
2
G
4
Posts: 719
Reputation: 2,938

Post » Thu Jul 24, 2008 3:39 pm

Glad I could help. ;)
Moderator
B
99
S
38
G
19
Posts: 1,584
Reputation: 17,942

Post » Thu Jul 24, 2008 10:10 pm

[quote="linkman2004":1ive9pf5]I was actually working on something similar today, so I whipped up a quick example for you[/quote:1ive9pf5]

Great example! For Each is the key there, and that's pretty much how I'd have done it as well.

One minor change though: in event 4, if you replace the random number with the object's unique ID (object1.UID) - that makes it bulletproof. With randomness, there's always the tiniest possibility it'll duplicate IDs. With object unique IDs, that never happens.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,630

Post » Sat Jul 26, 2008 7:04 pm

[quote="Ashley":1y0hsa5r] in event 4, if you replace the random number with the object's unique ID (object1.UID) - that makes it bulletproof.[/quote:1y0hsa5r]
Did you try this? - because when I do it it doesnt work, (my original problem) they don't set to anything if you set it to object1.OID and they all set to one of the object1's if you set it to object2.OID

I am assuming this is a bug???
B
2
S
2
G
5
Posts: 236
Reputation: 2,122

Post » Sun Jul 27, 2008 3:52 am

[quote="faggatron":28jm5rjw]Did you try this? - because when I do it it doesnt work, (my original problem) they don't set to anything if you set it to object1.OID and they all set to one of the object1's if you set it to object2.OID

I am assuming this is a bug???[/quote:28jm5rjw]

That's probably because you put OID instead of UID. :P The OID of object1 would be the same for each instance of object1. The UID is that instances unique identifier, so every instance has it's own UID. Hope that helps.
Moderator
B
99
S
38
G
19
Posts: 1,584
Reputation: 17,942

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 7 guests