Please login or register.

Login with username, password and session length
Advanced search  

News:

For WME related articles and tutorials visit WME Resource Center.

Author Topic: Using the mouse to select multiple actors in game  (Read 7710 times)

0 Members and 1 Guest are viewing this topic.

piere

  • Supporter
  • Frequent poster
  • *
  • Karma: 4
  • Offline Offline
  • Posts: 302
  • Sorry for any bad english in my posts. Game on !
    • View Profile
Using the mouse to select multiple actors in game
« on: June 09, 2011, 01:46:28 AM »

Hello all. I want to make a game and let the player control one or multiple actors. Can someone help me on a script that can do this? For Example, on your Windows desktop you can click down and move the mouse to select multiple icons. I want to be able to hold down the mouse in the game and highlight and select the actors. Anyone know how to get started? Another example would be in strategy games like Command and Conquer where you can click and hold the mouse to select multiple characters. Thanks !!
Logged

Azrael

  • Regular poster
  • ***
  • Karma: 9
  • Offline Offline
  • Gender: Male
  • Posts: 155
    • View Profile
    • Mad Orange
Re: Using the mouse to select multiple actors in game
« Reply #1 on: June 09, 2011, 07:20:56 AM »

I don't think it will be easy to do.

For example a good way to check what is inside an area should be on "LeftClick" taking the initial coordinates and on "LeftRelease" the final. Than check if some actors are inside these coordinates. But to do this you have to change how game handle "clicks" because usually if you "LeftClick" in a entity the related script start. You probably have to change the basic scripts to check both "LeftClick" and "LeftRelease" so if you "LeftClick" and "LeftRelease" on a hotspot the related script start, otherwise nothing happen.

Another problem is that actor's coordinates are at their feet. Sorry if i can't explain correctly but for example if with your area you take only the head of the actor you will not select the actor, because actor's coordinates refer to a point at his feet.

Also there will be some more minor problems, it depends on what you are trying to achieve.

Logged

piere

  • Supporter
  • Frequent poster
  • *
  • Karma: 4
  • Offline Offline
  • Posts: 302
  • Sorry for any bad english in my posts. Game on !
    • View Profile
Re: Using the mouse to select multiple actors in game
« Reply #2 on: June 09, 2011, 07:36:04 AM »

Thanks Azrael. Ok I am new to programming but trying to learn. I was thinking something like this, in words not full code

While (left click = true)

Click = Original click area;


Release = Click release location;

Pos1 = Actor1 position ;

Pos2 = Actor2 position ;

if (Pos1.Location or Pos2.Location >  Click)
Actor.Select; Actor2.Select;


I know this isnt actual code, its just in words that anyone can understand. Let me know if I am on the right track. Thanks everyone on here you are always helpful and friendly.
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Using the mouse to select multiple actors in game
« Reply #3 on: June 09, 2011, 08:25:25 AM »

I think it's doable, but it willr equire changes to the standard scripts (namely game.script).
Right now, left clicking is executed immediately when the user presses the mouse button (they don't have to release it). To be able to perform mouse dragging, you will need to move the left-click action to LeftRelease.


game.script would contain something like this (disclaimer: I just wrote this from the top of my head, it's totally untested, it's just to give you the idea):


Code: WME Script
  1. // the user needs to move the mouse for at least this many pixels
  2. // to consider it dragging
  3. var minDragDistance = 20;
  4.  
  5. global g_IsDragging = false;
  6. var origPos;
  7.  
  8.  
  9. ////////////////////////////////////////////////////////////////////////////////
  10. on "LeftClick"
  11. {
  12.   // initiate the dragging
  13.   g_IsDragging = true;
  14.   origPos.X = Game.MouseX;
  15.   origPos.Y = Game.MouseY;
  16. }
  17.  
  18.  
  19. ////////////////////////////////////////////////////////////////////////////////
  20. on "LeftRelease"
  21. {
  22.   if (!g_IsDragging) return;
  23.  
  24.    g_IsDragging = false;
  25.  
  26.   // check if we moved the mouse enough to consider it dragging
  27.   if (Math.Abs(Game.MouseX - origPos.X) >= minDragDistance || Math.Abs(Game.MouseY - origPos.Y) >= minDragDistance)
  28.   {
  29.     HandleEndDrag();
  30.   }
  31.   else
  32.   {
  33.     HandleLeftCLick();
  34.   } 
  35. }
  36.  
  37.  
  38. ////////////////////////////////////////////////////////////////////////////////
  39. function HandleLeftClick()
  40. {
  41.   // the user just clicked
  42.   // this function would contain the same code which the on "LeftClick" handler contains
  43.   // in the standard game.script
  44.  
  45.   // ...
  46.  
  47. }
  48.  
  49.  
  50. ////////////////////////////////////////////////////////////////////////////////
  51. function HandleEndDrag()
  52. {
  53.   // this is called if the user dragged the mouse for at least
  54.   // 'minDragDistance' pixels
  55.  
  56.  
  57.   // get the actual selection rectangle
  58.   var left, right, top, bottom;
  59.  
  60.   if (Game.MouseX < origPos.X)
  61.   {
  62.     left = Game.MouseX;
  63.     right = origPos.X;
  64.   }
  65.   else
  66.   {
  67.     left = origPos.X;
  68.     right = Game.MouseX;
  69.   }
  70.  
  71.  
  72.   if (Game.MouseY < origPos.Y)
  73.   {
  74.     top = Game.MouseY;
  75.     bottom = origPos.Y;
  76.   }
  77.   else
  78.   {
  79.     bottom = origPos.Y;
  80.     top = Game.MouseY;
  81.   }
  82.  
  83.  
  84.   // now you can check if the actor is within the rectangle...somehow
  85.   // we'll just test its position for now
  86.  
  87.   if (actor.X >= left && actor.X <= right && actor.Y >= top && actor.Y <= bottom)
  88.   {
  89.     // select the actor (change the color to red)
  90.     actor.AlphaColor = MakeRGBA(255, 0, 0);
  91.   }
  92.   else
  93.   {
  94.     // deselect the actor (change the color back to normal)
  95.     actor.AlphaColor = MakeRGBA(255, 255, 255);   
  96.   }
  97.  
  98. }
  99.  
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Using the mouse to select multiple actors in game
« Reply #4 on: June 09, 2011, 08:29:58 AM »

And a separate problem is how to display the selection rectangle. For this purpose I'd create a GUI window (with "Transparent" property set to true, so that it ignores user input). The window would be loaded all the time and it would contain one static control - a resizable rectangle.

In game_loop.script one would check if g_IsDragging is true. If not, the static control would be hidden. If dragging was in progress, one would get the dragging rectangle (the code is in the above script) and position/resize the static control appropriately.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Using the mouse to select multiple actors in game
« Reply #5 on: June 09, 2011, 08:32:19 AM »

Also note that the script doesn't take scrolling scenes into account. If you wanted to support them, you'd need to convert the actor scene-position to screen-position (by subtracting Scene.OffsetX / Scene.OffsetY).
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

piere

  • Supporter
  • Frequent poster
  • *
  • Karma: 4
  • Offline Offline
  • Posts: 302
  • Sorry for any bad english in my posts. Game on !
    • View Profile
Re: Using the mouse to select multiple actors in game
« Reply #6 on: June 09, 2011, 09:25:13 AM »

Thanks Mnemonic for taking the time to help me. It seems like when i put the LeftRelease function in the game script, the game screen is all black when it runs. Any Ideas?
Logged

piere

  • Supporter
  • Frequent poster
  • *
  • Karma: 4
  • Offline Offline
  • Posts: 302
  • Sorry for any bad english in my posts. Game on !
    • View Profile
Re: Using the mouse to select multiple actors in game
« Reply #7 on: June 10, 2011, 05:13:23 AM »

Sorry. I got part of the code working. I'm having trouble getting the window to load. Would I use the Game.LoadWindow for the rectangle selection? Also how would I get the static control in there? Thanks !!
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Using the mouse to select multiple actors in game
« Reply #8 on: June 10, 2011, 03:23:07 PM »

Yes, use Game.LoadWindow(). As for the static control, lookup "tiled images" in the documentation.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

piere

  • Supporter
  • Frequent poster
  • *
  • Karma: 4
  • Offline Offline
  • Posts: 302
  • Sorry for any bad english in my posts. Game on !
    • View Profile
Re: Using the mouse to select multiple actors in game
« Reply #9 on: June 11, 2011, 06:35:33 AM »

Thanks. I am getting the window to come up but it only appears in one section of the screen, not where the mouse drag is happening. Also How would I go ahead to setting up the rectangle resizing? Would I need to make a definition file? Thanks for the help again, everyone here is really nice !
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Using the mouse to select multiple actors in game
« Reply #10 on: June 11, 2011, 09:35:08 AM »

Tiled images are described in the "WindowEdit tool guide" chapter. Basically it's an ordinary image which you divide into nine tiles, and the engine then repeats the inner tiles when it needs to stretch the image.
In your case the image would contain a simple rectangle, transparent inside. Assign the tiled image to a static control, and by changing the static control's X, Y, Width and Height properties you can position the selection rectangle anywhere on the screen.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

piere

  • Supporter
  • Frequent poster
  • *
  • Karma: 4
  • Offline Offline
  • Posts: 302
  • Sorry for any bad english in my posts. Game on !
    • View Profile
Re: Using the mouse to select multiple actors in game
« Reply #11 on: June 11, 2011, 09:53:37 AM »

Thanks, What would you think the code would be for the clicking position, so if I click in a different area, the X and Y position will be different so the box doesnt always appear in the same place. Also what would be the code for the dragging the rectangle art so the more I drag, the more of the image is revealed. Sorry for all the questions, but I will for sure credit you as a thanks once the game is out.
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Using the mouse to select multiple actors in game
« Reply #12 on: June 15, 2011, 04:49:07 PM »

1) Create a new window.
2) Edit it in WindowEdit, delete everything, set the window's "Transparent" property to true.
3) Add a new static control and name it e.g. "sel_rect".
4) Assign a tiled image to the static control (see the forementioned docs).
5) In the window's script add convenience methods for setting the restangle position and for hiding it.

Code: WME Script
  1. method ResizeRect(left, top, right, bottom)
  2. {
  3.         var rect = this.GetControl("sel_rect");
  4.         rect.X = left;
  5.         rect.Y = top;
  6.         rect.Width = right - left;
  7.         rect.Height = bottom - top;
  8.        
  9.         rect.Visible = true;
  10.  
  11. }
  12.  
  13. method HideRect()
  14. {
  15.         var rect = this.GetControl("sel_rect");
  16.         rect.Visible = false;
  17. }
  18.  


6) In game.script, load the window.

Code: WME Script
  1. global winSelection = Game.LoadWindow("path_to_your_window_file");
  2.  

7) Also in game.script, change "var origPos;" to "global origPos;" so that we can access the drag start point from another script.


7) In game_loop.script resize the rectangle if dragging is in progress.


Code: WME Script
  1. function HandleDragging()
  2. {
  3.   global g_IsDragging;
  4.   global origPos;
  5.   global winSelection;
  6.  
  7.   // not dragging, just hide the rectangle
  8.   if (!g_IsDragging)
  9.   {
  10.         winSelection.HideRect();
  11.         return;
  12.   }
  13.  
  14.   // get curect drag rectangle (same code as above)
  15.   var left, right, top, bottom;
  16.   if (Game.MouseX < origPos.X)
  17.   {
  18.     left = Game.MouseX;
  19.     right = origPos.X;
  20.   }
  21.   else
  22.   {
  23.     left = origPos.X;
  24.     right = Game.MouseX;
  25.   }
  26.   if (Game.MouseY < origPos.Y)
  27.   {
  28.     top = Game.MouseY;
  29.     bottom = origPos.Y;
  30.   }
  31.   else
  32.   {
  33.     bottom = origPos.Y;
  34.     top = Game.MouseY;
  35.   }
  36.  
  37.   // resize the restangle
  38.   winSelection.ResizeRect(left, top, right, bottom);
  39. }
  40.  


Again, it's totally untested.
Note that this is relatively advanced scripting, and since you may not be experienced enough yet, maybe it would be better to start with something simpler.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave
 

Page created in 0.07 seconds with 22 queries.