Author Topic: enable/disable buttons based on a host of conditions  (Read 3382 times)

silversteez

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
    • MattSilverstein.com
enable/disable buttons based on a host of conditions
« on: November 06, 2012, 03:36:13 PM »
Hello,

New NGUI user (and new to programming, in general). This is probably a naive question, but i'll ask anyway because i don't want to tread too far down the wrong path...

I have a host of buttons that fire off player abilities and I want to enable/disable each button based on a host of bool conditions that change frequently during gameplay. Conditions such as:

-isCurrentlyPlayerTurn
-hasResourcesToUseAbility
-enemyIsNearby
-etc

Does it makes sense to put an OnUpdate script on each button that constantly checks for all the necessary conditions to be true? Seems like a lot of checking every frame, but I don't really know if that's standard procedure or foolish coding. Is there a more efficient way to handle this?

Thanks very much!

Arowx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: enable/disable buttons based on a host of conditions
« Reply #1 on: November 06, 2012, 04:01:24 PM »
If all of the boolean values can be stored in a single object, then you can just use getter setter methods to update the states of the buttons when they change as opposed to constantly checking every frame.

So the button calls a method to change the state on the object and then this in turn updates the buttons.

e.g. Pseudo Code
  1. Turn Over Button...
  2.  
  3. OnButton() {
  4.   Player.SetTurnOver();
  5. }
  6.  
  7. Player...
  8.  
  9. SetTurnOver() {
  10.   isCurrentlyPlayerTurn = false;
  11.   updateRelevantButton1();
  12.   updateRelevantButton2();
  13. }
  14.  
« Last Edit: November 06, 2012, 04:04:29 PM by Arowx »