MODULE: DragDrop 1.1.0: helps to drag things around your AGS game!

Started by Crimson Wizard, Mon 14/03/2016 00:09:54

Previous topic - Next topic

Crimson Wizard

DOWNLOAD DragDrop 1.1.0 MODULE
DOWNLOAD DragDropCommon 1.1.0 MODULE
DOWNLOAD Demo game (needs AGS 3.2.1 or higher)

Additionally, latest sources of both the script module and the demo game may be found in this repository:
https://github.com/ivan-mogilko/ags-script-modules

Full documentation for this module is now available here:
https://github.com/ivan-mogilko/ags-script-modules/wiki/Drag-&-Drop-module

Module supports AGS 3.2.1 and higher.


DragDrop and DragDropCommon are two modules that make it possible to drag things around in AGS. What is the difference between these two?

DragDrop module implements an abstract drag-and-drop functionality. This means that the module does not drag any actual game object on its own, but rather calculates the dragging and dropping action, and tells you what and how happens, so that you could script the actual object movement. You might say, this module provides an idea of dragging, rather than dragging itself :).

The upside of such approach is that you may script dragging of anything, both standard AGS objects and your own custom things, even non-ordinary pseudo-objects. For example, you may use this module to script dragging a line from point A to B, or draw a rectangle by dragging one of it's corners. The downside is that you have to be ready to do some extra work, and understand scripting well.

When you need to just make normal ordinary game objects dragged around in your game, this is what DragDropCommon module is for. DragDropCommon supports drag-and-drop for Characters, Room Objects, GUIs and GUI controls, and inventory items, right out of the box, only with a minimal setup.

NOTE: DragDropCommon module actually depends on DragDrop module, so you will have to add both to your game if you want to use DragDropCommon.

selmiak

Now this sounds really useful! Especially for mobile games and even cooler ingame computerscreens with hacking!

Crimson Wizard

And hereby I release Drag & Drop module v1.0.0.
DOWNLOAD DragDrop 1.0.0 MODULE
DOWNLOAD DragDropCommon 1.0.0 MODULE
DOWNLOAD Demo game (needs AGS 3.2.1 or higher)
(Also updated first post)

Not sure how much this was popular, since I got only 1 response and no questions about module usage, but I keep hopes that it will be useful to some.

What's new in 1.0.0

This update was focused on making DragDropCommon module capable of dragging object images instead of objects themselves, which also let me implement dragging of InventoryItems.

1. Added DragDropCommon.DragMove property, which determines the way dragging is done. It can be:
- eDDCmnMoveSelf - drags the object itself at real time (does not work for Inventory Items);
- eDDCmnMoveGhostOverlay - drags overlay with object's image on it (won't work for GUI and Controls);
- eDDCmnMoveGhostGUI - drags supplied custom GUI with object's image on it (currently does not work for GUI and Controls, but this is ONLY way to make inventory items drag & drop);

2. Additionally to DragMove, following properties can be set:
- DragDropCommon.GhostTransparency - determines ghost mode image transparency;
- DragDropCommon.GhostAlpha - determines whether ghost mode uses images with alpha channel;
- DragDropCommon.GhostGUI - sets custom GUI to be used for eDDCmnMoveGhostGUI mode (this MUST be set, otherwise it won't work);

3. Following new properties are meant for reading current state:
- DragDropCommon._InvItem - pointer to inventory item (if one is being dragged);
- DragDropCommon.ObjectWidth - returns dragged object/representation width, either taken from its properties, or deduced from the image;
- DragDropCommon.ObjectHeight - returns dragged object/representation height, either taken from its properties, or deduced from the image;
- DragDropCommon.UsedGhostGraphic - returns actual graphic index used in ghost modes (or 0);

4. Added DragDropCommon.TryHookInventoryItem() function, which, naturally, tries to start dragging inventory item under DragDrop.DragStartX / DragStartY coordinates.


Extended and improved Demo Game. I was actually worried if those who downloaded it noticed there are more than 1 room. So this time I provided slightly better help messages and a kind of "startup menu".
Also, now there are 4 demo rooms.

katie08cd

thank you, but I can edit it like the deduction room, like Sherlock Holmes: Crimes & Punishments?

Crimson Wizard

Quote from: katie08cd on Fri 20/04/2018 17:08:47
thank you, but I can edit it like the deduction room, like Sherlock Holmes: Crimes & Punishments?

Yes of course. The module was created having extension in mind, that allows you to script dragging of both built-in AGS objects and also any kind of custom "entities" in your game, that you define yourself.

Before using it I recommend to first find out how do you set up the deduction room itself. After you know what kind of objects you will have there, and which AGS features will represent them, then I may be able to give more detailed advice for the use of this module.

Cassiebsg

Okay, I just installed this module as I got an idea where this will be perfect.
But after following your instrucs from the first post:
Quote
1. Add both DragDrop and DragDropCommon modules to your game. DragDrop should be higher in the module list, or your game won't compile.
2. In one of the rooms add following code to the "After Fade-in" event handler:
Code: Adventure Game Studio

      DragDrop.Enabled = true;
      DragDropCommon.ModeEnabled[eDragDropCharacter] = true;
      DragDropCommon.ModeEnabled[eDragDropRoomObject] = true;
     

3. Just in case, add following code to the "Leave room" event handler:
Code: Adventure Game Studio

      DragDropCommon.DisableAllModes();
      DragDrop.Enabled = false;
     

4. That's it! Now you can drag and drop characters and objects with your mouse in that room. Have fun :).

I immediately get a crash when trying to click on a character or object.
It fails on line 67, 147, 270 & 632 of DragDropCommon.asc with "Error running function 'repeatedly_execute_always': Error: Null pointer refenced

I'm using the latest AGS, 3.5.0.24

I haven't tried anything else or look at the demo yet... but since you wrote that was all that was needed to try it...  (roll)
I'll check the demo now.
There are those who believe that life here began out there...

Crimson Wizard

@Cassiebsg, looks like it works in "Ghost GUI" mode by default (don't know why :/), which requires you to set a GUI object to the module.

Change to another mode as
Code: ags

DragDropCommon.DragMove = eDDCmnMoveSelf;
   OR
DragDropCommon.DragMove = eDDCmnMoveGhostOverlay;


I must update this module when I get spare time, with fixes and maybe also better tutorial.

Cassiebsg

Thanks, I'll give a try later on, but will add the lines already before the forum crashes again.  :)

Edit: Works now! :) So guess I can now play with programming the game.  :-D
There are those who believe that life here began out there...

Amir

Can u drop and drag inventory items with the module and let them interact with other inventory items, hotspots, objects and characters?

I tried but I couldn't get it. Codes or modules for drop and drag of inventory items in other threads would also help although I couldn't find any.
Truly, truly, I say to you, blessed are those who play adventure games, for theirs is the kingdom of heaven.

Crimson Wizard

Quote from: Amir on Thu 01/07/2021 18:11:54
Can u drop and drag inventory items with the module and let them interact with other inventory items, hotspots, objects and characters?

I tried but I couldn't get it. Codes or modules for drop and drag of inventory items in other threads would also help although I couldn't find any.

Inventory items themselves cannot be dragged, because they are not real on-screen objects. You need something that would represent them in game.

DragDropCommon module implements this using either a GUI or Overlay, but for items only feasible solution is GUI. If I remember correctly, Overlays don't work well because they are always appear behind GUI, which means they will be behind InventoryWindow too. Which is ugly. So best use GUI mode.

You may check how this is done in the Demo game's room 4 script (download link in the first post).

PS. For a brief example, here's how you set a GUI for representing item drag:
Code: ags

DragDropCommon.DragMove = eDDCmnMoveGhostGUI;
DragDropCommon.GhostGUI = MyGUI; // where MyGUI is the GUI you create yourself for this purpose


That may be enough for starters.

eri0o

 @Amir, are you trying to use this module on Android? It doesn't work with the allegro4 based engine with touchscreen devices.

Crimson Wizard

Quote from: eri0o on Thu 01/07/2021 18:38:45
Amir, are you trying to use this module on Android? It doesn't work with the allegro4 based engine.

What do you mean, why?

eri0o

There's no click and hold in allegro4 port - you can't check if a "mouse" button is down in the old allegro 4 Android port. This is the main thing I want to change with the new Android port (besides soon adding proper crossplatform multi-touch)

Edit: CW, I will not answer here to avoid complicating the topic, I just mentioned because Amir was asking about Android the other day. But in short we need proper way to support touch devices, including devices that have both, like laptop tablet hybrids. It's possible code wise with SDL, it's more about figuring the right design on how to handle this in AGS, I plan to open a ticket about this when I have at least a sketch of something.

Crimson Wizard

Quote from: eri0o on Thu 01/07/2021 18:40:43
There's no click and hold in allegro4 port. This is the main thing I want to change with the new Android port (besides soon adding proper crossplatform multi-touch)

If it's about controls and default ones don't work, this module allows to create custom ones. For example, if you check the demo game, there's one room where you press a key to start drag, and then press it again to stop drag.

I'd imagine there might be a way to figure out a solution on touch devices as well.

EDIT: I mean, that won't be literal "drag" anymore :), but rather click/touch to move, click/touch to drop workaround, or something similar.

Amir

@Crimson Wizard,  I checked room 4 script and created a gui like in ur module and I could drag the inventory items, but I couldn't let them interact with other inventory items, hotspots, objects and characters.

@eri0o  Oh, that's right. I'm trying to change a few things in my game so that it can be played on Android devices. All that remains is to drag the inventory items like other AGS games on Android, Whispers of a Machine, Blackwell series, etc.

if you are sure eri0o that this this module doesnt work on Android, is there some other code or method anywhere to do that?
Truly, truly, I say to you, blessed are those who play adventure games, for theirs is the kingdom of heaven.

eri0o

WoaM and Kathy Rain use a different code that works with the old android port - I actually have the script code because I asked by mail the dev, but I think you should just ask them yourself. If you want to use this module and use the same behavior for mouse and fingers, you need to use the Android SDL Port.

I have a little game that uses this module too here: https://github.com/ericoporto/dungeonhands

It has touch on the webport, but I don't know how well it runs on old devices. :/

Crimson Wizard

Quote from: Amir on Thu 01/07/2021 19:14:07
@Crimson Wizard,  I checked room 4 script and created a gui like in ur module and I could drag the inventory items, but I couldn't let them interact with other inventory items, hotspots, objects and characters.

You need to handle the "DragDrop.EvtWantDrop" event. For example, if you look into the room 4 script, there will be this piece of the code:
Code: ags

function room_RepExec()
{
  <...>
  else if (DragDrop.EvtWantDrop)
  {
    InvWindow* drop_to = GetInvWindowUnderObject(o_x, o_y, o_w, o_h);
    if (drop_to != null && ItemOrigin != null && drop_to != ItemOrigin)
    {
      ItemOrigin.CharacterToUse.LoseInventory(DragDropCommon._InvItem);
      drop_to.CharacterToUse.AddInventory(DragDropCommon._InvItem);
    }
  }
}


You write something similar in your "repeatedly_execute" function (e.g. in global script), and replace this with checking if there's a character, object, etc under the dragged item (or under the mouse x,y if you prefer), and if there's then, for example, call RunInteraction(eModeUseinv) for that object below.


Quote from: Amir on Thu 01/07/2021 19:14:07
if you are sure eri0o that this this module doesnt work on Android, is there some other code or method anywhere to do that?

This module is not strictly specific to desktop controls, it lets you customize itself to any control scheme you like. The problem is only in finding out what works with Android port, and applying here.

If the new Android port works better, 3.6.0 engine should be running games from previous versions well, you don't even have to recompile them in 3.6.0 (but you may).


I'd propose to separate this in two problems:
1. Making this interact with other objects after you drop the item;
2. Solving the Android controls.

Perhaps its best to test the first problem on desktop (like Windows) to make sure first part works.

Crimson Wizard

I might need to add another room to this demo showing how to script this kind interaction...

Quote from: eri0o on Thu 01/07/2021 18:40:43
There's no click and hold in allegro4 port - you can't check if a "mouse" button is down in the old allegro 4 Android port.

What happens when you press the finger and hold on Android, what kind of events does it send to AGS, if any?

Amir

Ahaaaa, I see.

I've tried something similar but not exactly as you described it. I will try it. Many Thanks.

QuoteI might need to add another room to this demo showing how to script this kind interaction

This is the best idea I ever heard  ;-D

QuoteI have a little game that uses this module too here: https://github.com/ericoporto/dungeonhands

I will check it out.

Truly, truly, I say to you, blessed are those who play adventure games, for theirs is the kingdom of heaven.

Amir

Quote
You write something similar in your "repeatedly_execute" function (e.g. in global script), and replace this with checking if there's a character, object, etc under the dragged item (or under the mouse x,y if you prefer), and if there's then, for example, call RunInteraction(eModeUseinv) for that object below.

Hi, I wrote something and I can now interact with hotspots,objects and characters but wrong interaction and inventory items don't interact with other inventory items. Do u know what's wrong?

Code: ags
function game_start() 
{ 

  DragDrop.Enabled = true;
  
  DragDropCommon.ModeEnabled[eDragDropInvItem] = true;
  DragDropCommon.DragMove = eDDCmnMoveGhostGUI;
  DragDropCommon.GhostGUI = gInvItemGhost;
  DragDropCommon.GhostTransparency = 0;
  DragDropCommon.GhostAlpha = false;
  DragDrop.DefaultHookKey = 0;
  DragDrop.DefaultHookMouseButton = eMouseLeft;
  DragDrop.DragMinDistance = 0;
  DragDrop.DragMinTime = 0;
  DragDrop.AutoTrackHookKey = true;
  DragDrop.DefaultUnhookAction = eDDUnhookRevert;
  }
  




Code: ags


function repeatedly_execute()
{
Character *chara = Character.GetAtScreenXY(mouse.x, mouse.y);
Object *obj = Object.GetAtScreenXY(mouse.x, mouse.y);
Hotspot *hot = Hotspot.GetAtScreenXY(mouse.x, mouse.y);
InventoryItem *inv = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);

if (DragDrop.EvtWantDrop)
{
  if ((chara != null) || (obj != null) || (hot != null) || (inv !=null))
  {
   Room.ProcessClick(mouse.x, mouse.y, eModeUseinv);
  }
}
}



Truly, truly, I say to you, blessed are those who play adventure games, for theirs is the kingdom of heaven.

SMF spam blocked by CleanTalk