Swap a variable between two identical objects.

0 favourites
  • 3 posts
From the Asset Store
Full game Construct 2 capx code source to post on Google Play
  • I have a bunch of identical objects that have the drag and drop behaviour and each has a variable attached. When I drop one on top of the other, I want to simply switch the variables.

    It feels like one of those issues that are fairly unique to the way Construct works. When you do 'Object On DragDrop drop' and then 'Object is overlapping Object', you've picked ... I think the 2nd object? I suppose therefore the solution is to save a copy of the first objects UID and the variable, save the 2nd objects variable, put the first variable into the collided object, do a pick-all object, find by UID of the first again, put the 2nd variable into there?

    Or am I being dumb and this is much simpler?

  • This should work:

    + Object: On collision with Object
     | Local number tmp‎ = 0
     | Local number tmp2‎ = 0
    ----+ System: Pick Object instance 0
    -----> System: Set tmp to Object.value
    ----+ System: Pick Object instance 1
    -----> System: Set tmp2 to Object.value
    -----> Object: Set value to tmp
    ----+ System: Pick Object instance 0
    -----> Object: Set value to tmp2

    Maybe a problem is if there are more than two objects collide. For example when you drop one object on two others. It should pick the object on top but I'm not 100% sure.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • With those 2 conditions, both objects will get picked. To tell them a part I would save the UID of the object you're dragging to a global variable. Then when you drop and test for overlapping, pick the UID that is NOT equal to to the one you dropped, then save the UID of that other object. Now you have both UIDs of the dropped object and the overlapping object. If you have that then swapping their variables should be easy.

    I hope that makes sense.

    That would only work with 2 objects though. If 3 or more objects are overlapping then you would need further conditions...

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