0
$\begingroup$

So I made a button with a certain function and I have made more buttons like this and they worked. But this one seems to get triggered when only mouseover is true. Even though it should only get triggered when both mouseover and click are true. Why is this not working properly?

    import bge

cont = bge.logic.getCurrentController()
close = cont.actuators['Scene']
act = cont.actuators['Action']
act2 = cont.actuators['Action.001']
mouseover = cont.sensors['MouseOver']
click = cont.sensors['Mouse']
resume = cont.actuators['Scene.001']

def actions():
    #animations
    if mouseover:
        act.action = 'Plane.010Action'
        act.frameStart = 1
        act.frameEnd = 2
        act.priority = 0

        cont.activate(act)

    if not mouseover:
        act2.action = 'Plane.010Action'
        act2.frameStart = 2
        act2.frameEnd = 1
        act2.priority = 0

        cont.activate(act2)

actions()


#Turn off guidline and resume game
def clicked():

    if (mouseover and click):
        resume.mode = 8
        resume.scene = 'Main game'

        close.mode = 6
        close.scene = 'Guideline'

        cont.activate(resume)
        cont.activate(close)

        bge.logic.addScene('Main game')

        scenes = bge.logic.getSceneList()
        for scene in scenes:
            if scene.name == 'Main game':
                guide = scene.objects['Guideline']
                guide.setVisible(False)

        bge.render.showMouse(False)

clicked()
$\endgroup$
  • $\begingroup$ Your script does a lot of things that as nearly no relationship to each other. Because of that it is not clear what this code is supposed to do. Finally the Python controller will be triggered independently from your code. It completely relies on the setup of all connected sensors. Just place a print statement right at the beginning of the code and you will see when the controller gets triggered. $\endgroup$ – Monster Jan 10 '17 at 6:04
2
$\begingroup$

Simply use an AND controller:

enter image description here

This is just animating the button. To trigger certain operations there are many options.

Send a message

This way the button does not even care who is processing the command.

enter image description here

The recipient can even reside in a different (but active) scene.

enter image description here

Direct operation

This means the button performs the operation. This creates a tight coupling between button and operation. I suggest to use this only on menu operations (such as switching menus, or closing the overlay scene).

Logic Bricks

enter image description here

Python

There are few ways too. The problem is that you can't connect to the already setup AND controller as there is no Python actuator.

This means you need to recreate the AND controller behavior within your custom brick.

enter image description here

Sample code:

import bge


def allSensorsPositive():
    for sensor in bge.logic.getCurrentController().sensors:
        if not sensor.positive:
            return False
    return True

if allSensorsPositive():
    print("run your code here")

Remarks

I strongly recommend to separate button presentation from button operation. Otherwise your design will quickly become unnecessary complex.

Debug sensor status

If you do not know the sensor status you can run this script. It will print it to console:

import bge

sensors = bge.logic.getCurrentController().sensors
output = ""
for sensor in sensors:
    output +="'{0}' t:{1:d} p:{2:d} |".format(sensor.name, sensor.triggered, sensor.positive)
print(output) 

Example:

'Mouse' t:0 p:0 |'Mouse over' t:1 p:1 |
'Mouse' t:0 p:0 |'Mouse over' t:1 p:0 |
'Mouse' t:0 p:0 |'Mouse over' t:1 p:1 |
'Mouse' t:0 p:0 |'Mouse over' t:1 p:0 |

It is

  • 'sensor name'
  • t: 0=not triggered, 1=triggered
  • p: 0=evaluated nothing, 1=evaluated positive

Obviously at least one sensor must be triggered to get a new output.

| improve this answer | |
$\endgroup$
  • $\begingroup$ It works on other scripts where I have done the same. Do you know what's causing only this one to act so weird? $\endgroup$ – Hendriks3D Jan 9 '17 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.