Please login or register.

Login with username, password and session length
Advanced search  

News:

IRC channel - server: waelisch.de  channel: #wme (read more)

Author Topic: SetCursor() too slow in game_loop.script  (Read 4509 times)

0 Members and 1 Guest are viewing this topic.

rkitting

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 44
    • View Profile
SetCursor() too slow in game_loop.script
« on: July 29, 2008, 07:07:43 PM »

Is it a good idea to change cursors dynamically in the game_loop.script, depending on what is under the mousecursor? It works but is very slow.... you can actually see it change from one cursor to the other within a second.

I use the following code-snippet in the while-loop:

Code: [Select]
  var ActObj = Game.ActiveObject; // evtl. für Mousecursor gebraucht ?
  var Item = Game.SelectedItem;
  if (ActObj != null)
  {
if (ActObj.Type == "button" && !ActObj.Disabled && !ActObj.CanHandleEvent("RightClick")) ActObj.SetCursor("sprites\cursor\leftclick.sprite");
else if (Item != null && ActObj.Name != "inventory" && ActObj.Type != "window" && ActObj.CanHandleEvent(Item.Name)) ActObj.SetCursor("sprites\cursor\leftclick.sprite");
else if (Item != null && !ActObj.CanHandleEvent(Item.Name)) ActObj.SetCursor(Game.GetCursor());
else if (ActObj.CanHandleEvent("RightClick") && !ActObj.CanHandleEvent("LeftClick")) ActObj.SetCursor("sprites\cursor\rightclick.sprite"); // rightclick
else if (ActObj.CanHandleEvent("RightClick") && ActObj.CanHandleEvent("LeftClick")) ActObj.SetCursor("sprites\cursor\bothclick.sprite"); // both
else if (ActObj.CanHandleEvent("LeftClick")) ActObj.SetCursor("sprites\cursor\leftclick.sprite"); // left, needed?
  }
Logged

robot

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 38
    • View Profile
Re: SetCursor() too slow in game_loop.script
« Reply #1 on: November 13, 2008, 08:11:55 PM »

hi!

i have the same problem and i think this is because of the Sleep() function in the gameloop.script.  i have got this workaround: in the MouseLeave-event of the object, for which the cursor should change automatically, an "empty" cursor is set (a sprite without an image). so when you enter the active object you will not see the cursor change because of the empty cursor.
Logged

sychron

  • Wanderer zwischen den Welten
  • Global Moderator
  • Regular poster
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 223
  • There is no spoon. The enemy gate is down!
    • View Profile
Re: SetCursor() too slow in game_loop.script
« Reply #2 on: November 13, 2008, 09:48:04 PM »

You should probably not do it in the game loop, but create another script running an infinite loop, but only doing that cursor stuff and attach it to the game. Remember, WME can use script multitasking.
So the load intensive game loop will run with the high sleep timeout, while your script can run with a lower timeout. But you should NOT run the second loop without any sleep() at all.

But, it's a far better way to change the cursor at the enter and exit event methods of the objects in question. It is more work to do while developing the game, but it pays off, for the work is only done when needed, and you can define special cursors for every single object -- and it is the much "cleaner" way.
« Last Edit: November 13, 2008, 09:51:11 PM by sychron »
Logged
... delete the inner sleep ...

robot

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 38
    • View Profile
Re: SetCursor() too slow in game_loop.script
« Reply #3 on: November 13, 2008, 10:28:03 PM »

yes i agree but it is a "very" lot of work. i thought i could create an array with all possible cursor actions. on rightclick it goes through all possible actions for THIS object (with the CanHandleEvent method) and shows the specified cursor. in the gameloop i set the default cursor for an object (this is the place when the cursor changing is visible for a little (very little :) moment.

i tried to set the sleep in the gameloop to 1 ms, but the changing is still visible, so i make this workaround...
but thanks for your suggest, i will try to make an extra loop for the cursor thing....

i don't know how it is with the performance. now i have only 3 rooms with a little amount (i think 5) of object and 5 possible cursors (but there will be much more in future)
could this be a problem when there are more things in a scene?
« Last Edit: November 13, 2008, 10:29:50 PM by robot »
Logged

sychron

  • Wanderer zwischen den Welten
  • Global Moderator
  • Regular poster
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 223
  • There is no spoon. The enemy gate is down!
    • View Profile
Re: SetCursor() too slow in game_loop.script
« Reply #4 on: November 13, 2008, 10:39:24 PM »

you don't need an array for that. Have a look at the original gameloop to find out how to find the current active object via actobj. If this is not null, you can query it via canHandleEvent().

What are the five cursors you are talking about? Do you want to show every possible object interaction upon entering an object or region?
As far as I understood your request, you want to change to a "can be manipulated", "can be crawled through" etc cursor like in Overclocked, right?
« Last Edit: November 13, 2008, 10:42:24 PM by sychron »
Logged
... delete the inner sleep ...

robot

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 38
    • View Profile
Re: SetCursor() too slow in game_loop.script
« Reply #5 on: November 13, 2008, 11:21:23 PM »

i don't exactly remember how it was in overclocked, but yes, you have f.e. an radio. with a rightclick you can change the cursor to look, take, play, record. and, lets say, you have a football - the cursor will change to look, take, kick. in the array i have all possible actions with the belonging mouse cursor (look, mouse1, take, mouse2, play, mouse 3, record, mouse4, kick, mouse 5).

so in my opinion its easy when i want to add an action i only have to add in the array and implement the "on event" in my object.
or perhaps there is an easier way to handle this?
Logged

sychron

  • Wanderer zwischen den Welten
  • Global Moderator
  • Regular poster
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 223
  • There is no spoon. The enemy gate is down!
    • View Profile
Re: SetCursor() too slow in game_loop.script
« Reply #6 on: November 14, 2008, 12:08:58 AM »

I would do it this way:

1.) Assign a unique number to each action.
2.) set up an array containing the mouse cursors.
3.) set up an array containing the corresponding action names
4.) set a "currenAction"-variable to 0 (no special action)
5.) modify each objects rightclick-handler to perform the following loop:

-increase the currentAction
-check wether the object can handle this event
-if no: loop again
-if yes:set the cursor

you should make sure that
- after the range of actions is depleted, you should restart searching from 0 or 1
- you should probably store the number you started with in a special variable to decrease the rare risk for an infinite loop (for if you obey the last point, the search will at least stop at the action you currently display if there is only one action)
6.) modify each objects exit-event to reset the currentaction to 0 and display the standard cursor
7.) modify each objects enter-event to set a default if you like

the "modify each objects"-part can be handled by a object script template afterwards.
Logged
... delete the inner sleep ...

robot

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 38
    • View Profile
Re: SetCursor() too slow in game_loop.script
« Reply #7 on: November 14, 2008, 01:34:33 AM »

yeah that seems to be the "clean" way, like you said  :) but it also sounds like much of script-code.
i have implemented the cursor handle (for now) in the game.script:

Code: [Select]
  ...
  // item-sensitiven cursor setzen
  else if(ActObj != null) {
    var i=1;
var found=false;
var actualCursor = ActObj.GetCursor();

while(i<allActions.Length) { //suche postion in allActions...
  if(actualCursor == allActions[i]) {
    found=true;
break;
  }
  i=i+2;
}
if (found) { //...wenn gefunden...
  if(i==allActions.Length-1) i=1;
  else i=i+2;
 
  while(i<allActions.Length) { //...setze cursor für nächste, mögliche action
    if(ActObj.CanHandleEvent(allActions[i-1])) {
      ActObj.SetCursor(allActions[i]);
  i=allActions.Length+1;
    }
    else if(i==allActions.Length-1) i=-1;
      i=i+2;
  }
}
else ActObj.ApplyEvent("RightClick");
  } 


allActions is my array with all possible events and the belonging mouse cursors.

Code: [Select]
global allActions = new Array
("LookAt", "sprites\system\lupe.sprite",
"Take", "sprites\system\hand.sprite",
"Talk", "sprites\system\sprechblase.sprite",
"Record", "sprites\system\record.sprite",
"Open", "sprites\system\open.sprite"
);

in the mouse-leave/entry events of the objects i reset the cursor/set the default cursor.





Logged
 

Page created in 0.19 seconds with 23 queries.