Elegant/Compact way of writing this script **SOLVED**

Started by Knox, Thu 07/07/2011 21:09:18

Previous topic - Next topic

Knox

Hi,

I wrote a script where I can set different mouse bounderies depending on if certain gui's/menus are visible. It works, only I believe there is a nicer way to do it so that its compact, and non-redundant.

Also, everytime I add a new enum "eType" (because Im still not sure how many I need since Im still designing), I have to manually add new lines each time into the script, and create new global variables. Is there a way  to do this so that the script scans the enum list (which is going to grow with time as I work on the game), creates a global variable for each new addition, and then runs the script so everything works? (If not, atleast, how to do it so the script isnt redundant and shortest possible.)

Code: ags

//.ash
enum eGameBoundsType 
{
  eGamePlayBounds, 
  eMenuBounds, 
  eMagnifierBounds,
};



Code: ags

//.asc
bool bGamePlayBoundsSet = false;
bool bMenuBoundsSet = false;
bool bMagnifierBoundsSet = false;

void setMouseBounds(eGameBoundsType eType)
{
    if (eType == eGamePlayBounds)
    {
      if (!bGamePlayBoundsSet)
      {
        Display("eType == eGamePlayBounds...");
        mouse.SetBounds(7, 53, 1000, 700); //Lft Top Rght Btm
        bGamePlayBoundsSet = true;
        //set all other bools to false...
        bMenuBoundsSet = false;
        bMagnifierBoundsSet = false; 
      }
    }
    else if (eType == eMenuBounds)
    {
      if (!bMenuBoundsSet)
      {
        Display("eType == eMenuBounds...");
        mouse.SetBounds(7, 53, 990, 700); //Lft Top Rght Btm
        bMenuBoundsSet = true;
        //set all other bools to false...
        bGamePlayBoundsSet = false; 
        bMagnifierBoundsSet = false; 
      }
    }
    else if (eType == eMagnifierBounds)
    {
      if (!bMagnifierBoundsSet)
      {
        Display("eType == eMenuBounds...");
        mouse.SetBounds(99, 99, 1000, 700); //Lft Top Rght Btm 
        bMagnifierBoundsSet = true;
        //set all other bools to false...
        bGamePlayBoundsSet = false; 
        bMenuBoundsSet = false;
      }
    }
}


Thanks!  :)
--All that is necessary for evil to triumph is for good men to do nothing.

monkey0506

Well why on earth do you need a specific bool for each of the enumerated values?? That's what enums are for!!

Code: ags
//.asc
eGameBoundsType mouseBounds;

void setMouseBounds(eGameBoundsType eType)
{
  if (eType == eGamePlayBounds) mouse.SetBounds(7, 53, 1000, 700); //Lft Top Rght Btm
  else if (eType == eMenuBounds) mouse.SetBounds(7, 53, 990, 700); //Lft Top Rght Btm
  else if (eType == eMagnifierBounds) mouse.SetBounds(99, 99, 1000, 700); //Lft Top Rght Btm
  mouseBounds = eType; // set the global variable to keep track of the current bounds
}


If you still specifically need to be displaying a message about the passed value, I'd suggest just creating a separate function:

Code: ags
String GetBoundsTypeName(eGameBoundsType eType)
{
  if (eType == eGamePlayBounds) return "eGamePlayBounds";
  else if (eType == eMenuBounds) return "eMenuBounds");
  else if (eType == eMagnifierBounds) return "eMagnifierBounds");
}

// wherever
Display("Current bounds: %s", GetBoundsTypeName(mouseBounds));

Knox

#2
Ah crap, well I thought that if I put that script in rep_exec...I dont want AGS to constantly reset the mouse bounderies over and over again...so I added a bool "if it hasnt been setup yet, set the bounderies to this"...that way I figured it will only run it once.

Ok, Ill try what you wrote, Ill keep you posted :)

**EDIT**

Awwwwwww YEAH!! Much cleaner + more effecient Monkey! Shanks!!1

--All that is necessary for evil to triumph is for good men to do nothing.

monkey0506

I'm not sure, but AGS is probably already making sure what the current mouse bounds are before performing any "expensive" operations, so it likely wouldn't even be an issue, but there's nothing to stop you putting:

Code: ags
  if (eType == mouseBounds) return; // unchanged, do nothing


At the top of your setMouseBounds function.

Anyway, glad it worked for you. Really from the standpoint of the code itself the logic is pretty much the same, it's just a matter of how much work you're doing for the same end-result. Enums (directly) are just a better fit for this scenario since as you expand the enum you're automatically expanding the possible values for your stored variable, and you only have to have one variable instead of a dozen. :P

Knox

Ahhhhh!! Didnt know I could do that...looks like Im gonna be rewriting a crap-load of stuff now, hehe!! Well you're quite the teacher :)

Thnx Monkey, I always appreciate your help!!
--All that is necessary for evil to triumph is for good men to do nothing.

monkey0506

If the return type of the function is set to void or function then you can call the return keyword by itself to abort the function. Otherwise you must return a value, but you can keep in mind that any pointer types could return null in the same situation.

Even if you couldn't do that, you could enclose your code inside of if-else if-else blocks, but depending on how many nested conditions you might need, that could become rather unwieldy, which is why the return keyword is so useful. ;)

SMF spam blocked by CleanTalk