MouseClick Python

Okay heres the deal, I was fooling around with some python in blender and noticed my LEFTCLICK wouldn’t work. What do i do wrong? (I want the left click in python and not in logic bricks (btw in logic bricks it works) because I like to have a clean code and not messy logic bricks) I might just be stupid and not see the problem but my console says nothing and it feels like I’ve tried everything xD

import bge


class Player(object):
    
    def __init__(self, cont):
        self.cont = cont
        self.own = self.cont.owner
        self.scene = bge.logic.getCurrentScene()
        
        #Mouse
        self.MouseClick = bge.logic.mouse.events
        self.Input_Just = bge.logic.KX_INPUT_JUST_ACTIVATED
        
        self.MouseOver = self.cont.sensors["MouseOver"]
        
        self.selected = False
    
    def Update(self):
        if self.selected == True:
            self.MovementCoords()
    
    def _Selected(self):
        #If the mouse is over and if you left click
        if self.MouseOver.positive and self.Input_Just == self.MouseClick[bge.events.LEFTMOUSE] and self.selected == False:
            self.own.color = [0,1,0,0]
            self.selected = True
        
    
    def MovementCoords(self):
        pass
    
    def Movement(self):
        pass
    
def Main(cont):
    own = cont.owner
    
    if not "init" in own:
        own["init"] = 1
        own["PlayerClass"] = Player(cont)
    own["PlayerClass"].Update()


Sorry, but this code looks messy. It does not tell what it is good for or what it is supposed to do.

I suggest you separate player logic from user input. Before you do you should define what the player is supposed to do. What should player.update() do? update what? and update how? what is the purpose?

For User input I would use logic bricks as input. It is much cleaner than hiding it in some messy code blocks. You do not even rely on left or right click, keyboard, joystick, network or decision finding. Connect whatever sensor you like and its done. If your really need more complex user processing, use a separate object to deal with it and establish a simple communication to the according objects (e.g. player). This way you can replace the user input any time you like (e.g replace left click with right click and add keyboard input)

Better do not include selection logic into the player. Wouldn’t it make more sense to have one “selector” that deals with selections and notifies the participants when they are selected (or lose selection)? Otherwise you have to implement the selection method at each single select-able object. If the selector does selection only, it logic is pretty simple and very easy to maintain.

There is no need to check initialization all the time. If you do it once before anything else, you can assume the object is initialize. This way you have separate initialization and normal processing. There is no need to have it at the same place. “init”=1 is very noobish. Your object is not initialized when “playerclass” does not exist. There is need for an additional property. Btw. call a property “class” when it contains a class. In your case it contains an instance of a class which is an object. If it is supposed to be a class you should remove the () from Player() ;).

Right now it ain’t good for anything and is only suppose to do one thing, turn green when the object is being clicked.
Like I said I was just fooling around with some python and was just wondering why it wouldn’t bother my input. But i guess i have to use a logic brick for that one :). Sure it is messy for you but for me, it doesn’t look messy, guess it’s a own thing :).

There is no need to check initialization all the time. If you do it once before anything else, you can assume the object is initialize. This way you have separate initialization and normal processing. There is no need to have it at the same place. “init”=1 is very noobish. Your object is not initialized when “playerclass” does not exist. There is need for an additional property. Btw. call a property “class” when it contains a class. In your case it contains an instance of a class which is an object. If it is supposed to be a class you should remove the () from Player() ;).

Hmm I quiet do not understand that very well. But it do sound interesting. Would love if you could make an example of it :). And yeah i am quiet noobish on python xD just beginned with class systems and trying to learn them :).

No problem. Here is a very quick demo, that shows several separate concerns. It might look a bit silly to separate these simple things into several parts. But they are simple because they are separated.

Yes, you can do it differently and more condensed. I think you want to add much more purposes to your game. It will be easier to manage if the thinks remain simple. You will still create enough complex things. So better keep it simple and flexible whenever you can. A very annoying thing is, when you shake one aspect of the game (such as exchange left click with right click) other aspects doe not work anymore or not as expected.

I hope it helps

Attachments

separationDemo.blend (92.2 KB)