0
$\begingroup$

I've multiple planes acting as sensors(buttons) in my game. I want to keep track on the order in which these buttons were clicked and also the total number of clicks. This information has to be processed to give some output. How can this be done? I think this can be done by adding a game property with all the objects sharing this common property and incrementing the property value on click of the plane. How can we have a single common game property for all the objects?

$\endgroup$
  • 1
    $\begingroup$ if only it were that easy. You can have a property like that if you create it and access it via python, but there is another way. I recommend having one object with the property, the same object that will process the data on that property. Have all your sensor planes send a message to that object, and have that object receive the message, and then modify the property appropriately. $\endgroup$ – X-27 wants to Reinstate Monica Sep 25 '16 at 14:17
  • $\begingroup$ how will the order of clicks be maintained? $\endgroup$ – Gauri Chitale Sep 25 '16 at 14:31
  • $\begingroup$ well, you'll just have to figure out a way to keep track of the order in which the object with the properties receives the messages - it would be no different than trying to record the order of clicks then if each plane could access a global property. $\endgroup$ – X-27 wants to Reinstate Monica Sep 26 '16 at 0:21
1
$\begingroup$

Properties belong to a single game object. They are not shared by default.

There are several ways to implement the requirements you have.

Here is one:

  • a single object handles the clicking -> button controller
  • the buttons do nothing regarding the click processing (maybe some feedback animation). Each button provides a meaning (e.g. via name or via property).

The button controller can be an empty as it has no further meaning within the 3D scene other than processing the clicks.

The button controller triggers a Python controller on mouse button and mouse over any sensor. Then a button is sensed the controller identifies the meaning and adds an entry to a button tracker. The button tracker tracks the button clicks. It can be a simple list.

example:

import bge

def recordClicks():

    if not allSensorsPositive():
        return

    hitObject = getHitObject()

    meaning = hitObject["meaning"]
    getTracks().append(meaning)


    print("clicks are", getTracks())

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

def getHitObject():
    for sensor in bge.logic.getCurrentController().sensors:
        try:
            return sensor.hitObject
        except AttributeError:
            pass

def getTracks():
    tracker = bge.logic.getCurrentController().owner
    try:
        return tracker["tracks"]
    except KeyError:
        tracks = []
        tracker["tracks"] = tracks
        return tracker["tracks"]

The clicks are recorded in property "tracks". It is a list (not view able by show debug properties) containing the clicked objects in the order they are clicked.

The clickable objects should contain a property called "meaning". The best is to filter the mouse over any sensor with property "meaning".

Configure the Python controller with Module mode and "demo.recordClicks".

Unfortunately you did not mention what to do with the tracked clicks. So this example just prints them to console.

enter image description here

$\endgroup$

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.