Game Development Community

OnWake called Twice?!

by Xavier "eXoDuS" Amado · in Torque Game Engine · 10/03/2003 (10:53 pm) · 8 replies

I have several gameTSCtrl's that I use to render over my mission, but each time I call Canvas.setContent() on them the onWake function is called twice! So everything I do in the onWake method happens twice and it's not good.

I haven't digged deep in the code yet, but If anyone has any information on this I'll appreciate it, or maybe it's a real good bug (tm)...

Regards,
Xavier

#1
10/03/2003 (11:13 pm)
Well, this is the fastest bug fix I ever saw, I hadn't finished writing the post and TheAce had already found the problem.. he didn't even give me a chance to dive in :P

The bug is in guiCanvas.cc. We have some code that reads:
if(gui)
   {
      //add the gui to the front
      if(!size() || gui != (*this)[0])
      {
         // automatically wakes objects in GuiControl::onAdd
         wakedGui = !gui->isAwake();
         addObject(gui);
         if (size() >= 2)
            reOrder(gui, *begin());
      }
      index = 1;
   }
The comment there already explains that GuiControl::onAdd wakes the gui elements... so the problematic code is a few lines after that and reads:
if(gui)
   {
      GuiControl *oldResponder = mFirstResponder;
      mFirstResponder = gui->findFirstTabable();
      if(oldResponder && oldResponder != mFirstResponder)
         oldResponder->onLoseFirstResponder();

      //wake the object up in script
      if(wakedGui)
         Con::executef(gui, 1, "onWake");
   }
Those last 2 lines are checking if the gui is awaken (from the above line in the previous code snippit) and if it isn't then it calls onWake.

Well I thought a bit about this and the appropiate solution is to swap these two lines:
wakedGui = !gui->isAwake();
         addObject(gui);
Why? Well, because wakedGui is checking if the gui is awake _before_ addObject() is called, so of course it always returns true (mind the ! operator). Then the addObject is called and onWake is called by addObject, but after that the other if also evaluates and onWake is called again.
So best way would be to swap those lines, so the gui is first added and then check to see if it's awake.

Regards,
Xavier
#2
10/04/2003 (5:02 pm)
Thanks Xavier,

I too was having the same problem with a irc client I am working on. The onWake was being called twice which caused me to register on the irc server twice and stuff. Not good. Thanks for the fix.
#4
10/06/2003 (10:19 am)
This bug actually reflects a change where all controls in the GUI get script onWake'd in GuiControl::onWake (not onAdd as the comment would have you believe). I just ripped out the whole test in GuiCanvas, and let GuiControl handle it.

- Mark
#5
10/06/2003 (12:34 pm)
That makes sense Mark :)
Thanks.
#6
12/16/2003 (4:35 am)
Should this go in HEAD?
#7
12/16/2003 (9:29 am)
It was checked in head...
#8
12/18/2003 (9:49 am)
Oddly, by switch those two lines around my chat hud dissapeared and when I leave the missionEditor(F11) I lose all input except the console and guiEditor(F10).

By going back to the old way, I get my chatHud back and the missionEditor exits correctly.