Jibble

Author Topic: Drag and drop puzzle help  (Read 2122 times)

rongel

  • Posts: 99
Drag and drop puzzle help
« on: 25 Aug 2014, 16:31 »
Hi!

I'm trying to do simple drag & drop puzzle. I have a room (without characters), and some objects that you are supposed to drag to right places. Currently i'm using following script:

Code: Adventure Game Studio
  1. function oMyItem_Interact(){
  2.   while(mouse.IsButtonDown(eMouseLeft)){
  3.     oMyItem.X = mouse.x;
  4.     oMyItem.Y = mouse.y;
  5.     Wait(1);
  6.  

That works for dragging the item around (even though my item "jumps" a bit annoyingly every time it's clicked). But how can I drop the item in the right place so that new command is activated? For example if my puzzle had a draggable key and I would drop it over a lock, what is the best way to make it work? I have read the forums, but haven't found an answer (many of the old links are not working anymore...)

Anyway, all help is welcome, thanks in advance!

Crimson Wizard

  • Posts: 8,794
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Lifetime Achievement Award Winner
    • Crimson Wizard worked on a game that won an AGS Award!
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
Re: Drag and drop puzzle help
« Reply #1 on: 25 Aug 2014, 16:43 »
You do that in a non-optimal way: you are locking the dragging process inside one function, thus blocking the rest of the game.
Instead, I suggest to perform processing in "repeatedly_execute":
Code: Adventure Game Studio
  1. bool WasDragging;
  2. int OldMyItemX;
  3. int OldMyItemY;
  4.  
  5. function room_RepExec()
  6. {
  7.     if (mouse.IsButtonDown(eMouseLeft)) {
  8.         // have we pressed a button over our object?
  9.         if (!WasDragging && Object.GetAtScreenXY(mouse.x, mouse.y) == oMyItem) {
  10.             // just started draggin? remember where you took it
  11.             OldMyItemX = oMyItem.X;
  12.             OldMyItemY = oMyItem.Y;
  13.             WasDragging = true;
  14.         }
  15.         else if (WasDragging) {
  16.             // mouse button still pressed? continue dragging
  17.             oMyItem.X = mouse.x;
  18.             oMyItem.Y = mouse.y;
  19.         }
  20.     } else if (WasDragging) {
  21.         // was dragging but mouse button now released? drop it
  22.         WasDragging = false;
  23.         // ... you drop code here ...
  24.         // for example:
  25.         if (oMyItem.IsCollidingWithObject(oLock))
  26.             UnlockTheLock(); // success!
  27.         else {
  28.             // failure (return the item on previous place?)
  29.             oMyItem.X = OldMyItemX;
  30.             oMyItem.Y = OldMyItemY;
  31.         }
  32.     }
  33. }
  34.  


To solve the "jumping" problem, remember the offset between mouse coordinates and object's coordinates at the moment you take it:
Code: Adventure Game Studio
  1. int DragOffsetX; <-------------------------
  2. int DragOffsetY; <-------------------------
  3.  
  4. <...>
  5.     if (mouse.IsButtonDown(eMouseLeft)) {
  6.         // have we pressed a button over our object?
  7.         if (!WasDragging && Object.GetAtScreenXY(mouse.x, mouse.y) == oMyItem) {
  8.             // just started draggin? remember where you took it
  9.             OldMyItemX = oMyItem.X;
  10.             OldMyItemY = oMyItem.Y;
  11.             DragOffsetX = mouse.x - oMyItem.X; <-------------------------
  12.             DragOffsetY = mouse.y - oMyItem.Y; <-------------------------
  13.             WasDragging = true;
  14.         }
  15.         else if (WasDragging) {
  16.             // mouse button still pressed? continue dragging
  17.             oMyItem.X = mouse.x - DragOffsetX; <-------------------------
  18.             oMyItem.Y = mouse.y - DragOffsetY; <-------------------------
  19.         }
  20.     }
  21.  

EDIT: duh! fixed silly mistake.
EDIT2: fixed typos.
« Last Edit: 25 Aug 2014, 20:31 by Crimson Wizard »

rongel

  • Posts: 99
Re: Drag and drop puzzle help
« Reply #2 on: 25 Aug 2014, 18:25 »
Thanks for the super-quick answer!

That is quite heavy stuff... I did try your example, but my object didn't move anywhere, but at least no script errors!

Is the onMyItem a new item, what is it's difference with oMyItem? And is UnlockTheLock(); just a place for any command, for example: Display("Succes!");

Anyway, thanks for the info, I'll keep trying!



Crimson Wizard

  • Posts: 8,794
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Lifetime Achievement Award Winner
    • Crimson Wizard worked on a game that won an AGS Award!
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
Re: Drag and drop puzzle help
« Reply #3 on: 25 Aug 2014, 18:32 »
Is the onMyItem a new item, what is it's difference with oMyItem?
That's a typo!

And is UnlockTheLock(); just a place for any command, for example: Display("Succes!");
Yes, it's an example of calling your custom function. Just put your own code there.

Sorry, I did not really test the code, just typed it by memory. I am going to check it out myself now :).

Another mistake: in rooms you usually use "function room_RepExec()". Create one by going to Room's events and create the handler for "Repeatedly execute" (like you did for Object's interact event).
Then paste function contents there.
« Last Edit: 25 Aug 2014, 18:37 by Crimson Wizard »

rongel

  • Posts: 99
Re: Drag and drop puzzle help
« Reply #4 on: 25 Aug 2014, 19:42 »
Ok, finally fot it working!

Well mostly, for some reason nothing happens when I drag my "key" to my "lock", the key just jumps back to it's starting location. Hmm?

EDIT:

Does (oMyItem.X == oLock.X && oMyItem.Y == oLock.Y) mean that they have to be exactly on the same position? My lock object is large, but does that mean I need to find the exact pixel?
« Last Edit: 25 Aug 2014, 19:47 by rongel »

Crimson Wizard

  • Posts: 8,794
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Lifetime Achievement Award Winner
    • Crimson Wizard worked on a game that won an AGS Award!
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
Re: Drag and drop puzzle help
« Reply #5 on: 25 Aug 2014, 20:29 »
Does (oMyItem.X == oLock.X && oMyItem.Y == oLock.Y) mean that they have to be exactly on the same position? My lock object is large, but does that mean I need to find the exact pixel?
Yes,... that was a bad example.
Of course, in normal situation you would need to compare to rectangle bounds, or do a pixel-perfect detection.
Code: Adventure Game Studio
  1. if (oMyItem.IsCollidingWithObject(oLock))
  2.  

rongel

  • Posts: 99
Re: Drag and drop puzzle help
« Reply #6 on: 25 Aug 2014, 20:57 »
Got it working, thanks! :-D

Maybe it needs a bit fine tuning, but overall it's great, I hope this helps others as well!

Tremulas

  • Posts: 10
    • I can help with play testing
    • I can help with story design
    • I can help with translating
    • I can help with voice acting
Re: Drag and drop puzzle help
« Reply #7 on: 26 Mar 2016, 20:47 »
Can we apply isCollidingWithObject on inventory items ?

Crimson Wizard

  • Posts: 8,794
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Lifetime Achievement Award Winner
    • Crimson Wizard worked on a game that won an AGS Award!
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
Re: Drag and drop puzzle help
« Reply #8 on: 26 Mar 2016, 21:08 »
Can we apply isCollidingWithObject on inventory items ?

No. InventoryItem cannot really collide with anything, nor be placed anywhere in the room. It exists in "separate dimension" (so to say), not related to characters and objects.
« Last Edit: 26 Mar 2016, 21:09 by Crimson Wizard »

Kumpel

  • Posts: 1,458
  • On the way to break new ground...
    • I can help with AGS tutoring
    • I can help with animation
    • I can help with backgrounds
    • I can help with characters
    • I can help with proof reading
    • I can help with story design
    • I can help with translating
    • I can help with voice acting
Re: Drag and drop puzzle help
« Reply #9 on: 26 Mar 2016, 21:12 »
To actually place items somewhere in a room you have to combine them with objects representing that item. And these objects are of course appliable with isCollidingWithObject.

Or am I misunderstanding your question?

Tremulas

  • Posts: 10
    • I can help with play testing
    • I can help with story design
    • I can help with translating
    • I can help with voice acting
Re: Drag and drop puzzle help
« Reply #10 on: 27 Mar 2016, 10:33 »
Actually I was wondering if I could use IsCollidingWithObject and the whole Drag and Drop System in my Inventory ,to combine inventory items .

Crimson Wizard

  • Posts: 8,794
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Lifetime Achievement Award Winner
    • Crimson Wizard worked on a game that won an AGS Award!
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
Re: Drag and drop puzzle help
« Reply #11 on: 27 Mar 2016, 15:19 »
Actually I was wondering if I could use IsCollidingWithObject and the whole Drag and Drop System in my Inventory ,to combine inventory items .
No, InventoryItems cannot be moved, dragged etc. They simply do not have coordinates as such. Conceptually they are part of linear list.
The inventory window is displaying them arranged in grid using its own logic, but items themselves do not posess such property as "position on screen".

You might need to script something that represents inventory item (similar to how room object may represent an item lying in the room).
For example, you could create graphical Overlay to drag item image around; but you will have to test whether this overlay is above another item yourself, calculating item position in window using InventoryWindow properties, such as its coordinates, ItemWidth and ItemHeight values.

Unfortunately I do not have much free time right now, but I may post some experimental example of script later, unless someone else will before.
« Last Edit: 27 Mar 2016, 15:22 by Crimson Wizard »

Tremulas

  • Posts: 10
    • I can help with play testing
    • I can help with story design
    • I can help with translating
    • I can help with voice acting
Re: Drag and drop puzzle help
« Reply #12 on: 27 Mar 2016, 15:26 »
Ok , thank you so much for the information. I'll be looking forward for your example script in case I wont find a solution !

Thanks again !

Crimson Wizard

  • Posts: 8,794
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Lifetime Achievement Award Winner
    • Crimson Wizard worked on a game that won an AGS Award!
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
Re: Drag and drop puzzle help
« Reply #13 on: 28 Mar 2016, 20:56 »
Hmmmmm... I forgot that overlays are always drawn behind GUI.
Perhaps using cursor graphic is the only way to visually depict dragging inventory item (same as when you select ActiveInventory).