general question about classes

hi guys,
im trying to understand classes and i made a little example, id like to know if im doing it right,
can someone give a look to the blend file and tell me what i can improve or how i can write better the code or if im doing totally wrong pls?
the class in the file its a simple class button, just move the mouse upon the meshes and left click.
the classes seem to be hard to understand well to me.
thx

Hello giobbo,

I have several remarks.

  • A better version of method ‘botton’ would be:
    def botton(self, hover, select):
        if hover:
            scale = [1.2,1.2,1.2]  
        else:
            scale = [1.0,1.0,1.0]  
        if select:
            scale = [0.8,0.8,0.8]
            cont = bge.logic.getCurrentController()
            cont.activate(cont.actuators[0])
        self.localScale = scale
  • But actually you wouldn’t be needing this method (or the class containing it) if you change method ‘bottone’ to:
def bottone(cont):
    own = cont.owner
    mouse_over_sensor, mouse_left_button_sensor = own.sensors
    if mouse_over_sensor.positive:
        if mouse_left_button_sensor.positive:
            scale = [0.8, 0.8, 0.8]
            cont.activate(cont.actuators[0])
        else:
            scale = [1.2, 1.2, 1.2]
    else:
        scale = [1.0, 1.0, 1.0]
    own.localScale = scale
  • However in this method the button is scaled every logic tic. Ideally this would only happen when necessary.
  • Using a Mouse Over Sensor for each individual button is not the best solution. The more buttons you have, the more processing time it will take. So use one Mouse Over Any Sensor instead.
  • A better use of classes is shown in the attached blend file. You’ll notice that all previous points are applied in it. Also a class of functions (Actions) is added.

Attachments

class_menu_with_mouse_over_any_buttons_good.zip (105 KB)

thx for the answer, if you can write few comments on your code it will be usefull for me thx anyway

Here’s the same blend file with comments. Put it in the same folder with the Sounds folder (or else it won’t find the Sounds folder of course). Read from bottom to top. Good luck! If there are any questions, remarks, … I’m happy to answer.

Attachments

class_menu_with_mouse_over_any_buttons_with_comments.blend (93.7 KB)

thx for the comments