2
$\begingroup$

So I have a quiz set to where they can press a letter, if they press the right letter they get 10pts.

so the logic bricks look like

Keyboard (a) ->and ->property (mode: assign, property: -score, vale:10)
Keyboard (b) ->and ->property (mode: assign, property: -score, vale:0)
Keyboard (c) ->and ->property (mode: assign, property: -score, vale:0)
Keyboard (d) ->and ->property (mode: assign, property: -score, vale:0)

So you can press A which sets points to 10 then press B,C, or D which sets points back to 0 then press A again and set the points back to 10 again.

So let's say they press A and ready to go to question 2 they press Enter which takes them to scene 2.

When this happens points are set back to 0.

How do I get the 10 points from scene 1 to transfer to scene 2, scene 3, scene 4, and so on and so forth?

$\endgroup$

1 Answer 1

1
$\begingroup$

As these game status is part of your game model I strongly suggest to keep it in a way that does not get removed with the scene.

There are many ways to do that (e.g. use an overlay scene that you keep during the game).

Here is a more practical approach:

Store your game model in Python

To do so you need a Python structure to keep your data:

model.py

class GameModel():
   def __init__(self):
      self.points = 0

model = GameModel()

I hope you see it keeps the number of collected points starting with zero. This storage is your model. But you need some operations on it. Lets say you want to show the points in a scene via a text object. So lets add a points viewer - e.g. call it "Points.viewer":

Always sensor [no level trigger!] 
    --> Python Controller Module: viewer.pointsToText

enter image description here

viewer.py

import model
import bge

def pointsToText():
    getOwner().text = "{:d}".format(model.points)

def getOwner():
    return bge.logic.getCurrentController().owner

This should be enough to show the "0" somewhere at the screen.

Now we need operations that change the points in the model. Lets say you want to add 10 points after the user gives the right answer.

Sensor that evaluates true when points should be added
    --> Python Controller Module: controller.addValueToPoints 
        --> Property Actuator value: 10

enter image description here

Attention: The following code expects a single Property actuator that has the points to add in the Value field! The actuator will not be activated by the Python controller.

controller.py

import model
import bge

SUBJECT_POINTS_CHANGED = "points changed"    

def addValueToPoints():
    if not allSensorsPositive():
        return

    points = getPointsToAdd()
    model.points += points
    notifyPointsChanged()

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

def getPointsToAdd():
    actuator = bge.logic.getCurrentController().actuators[0]
    return int(actuator.value)

def notifyPointsChanged():
    bge.logic.sendMessage(SUBJECT_POINTS_CHANGED)

You might see that this code sends a notification message after changing the points in the model. While this is not necessary to update the model with the new value, it is an efficient way to tell all interesting objects that the points changed. This way any point viewing object can update itself and show the new values. So lets do that and add the according logic to the above "Points.viewer":

Always sensor [no level trigger!] 
+
Message sensor [True Level Triggering] Subject "points changed"
    --> Python Controller Module: viewer.pointsToText

enter image description here

These are the basics.

Summary:

  • You can store the points in a model that is stored in an separate Python module.
  • You can retrieve the points from the model.
  • You can add points to the model.
  • You notify interested objects when the points change.

With the same approach you can store other model data and add more operations (such as saving to a file).

  • Model: describes the game status (at least the important parts of it)
  • Viewer: presents the data of the model to the audience
  • Controller: processes input from outside and changes t

The model will exist until the game dies.

I hope it helps

$\endgroup$
3
  • $\begingroup$ Ah still learning blender...especially the python part...so does this require multiple blender files? 1 file with all the python code and then another with the actual game? Here's a file of what I've got so far. You did help me a little bit though with a different method via "messages" I just need to get them to act accordingly So 10pts is added once instead of multiple times. <img src="http://blend-exchange.giantcowfilms.com/embedImage.png?bid=672" /> $\endgroup$ Nov 23, 2015 at 22:58
  • $\begingroup$ No this solution expects one continuous running game (one blend file/ no game actuator with start game). It uses three Python files (either in text block or in Python search path). You could have all code in one file.. but this creates an hard to maintain monolithic block of code. $\endgroup$
    – Monster
    Nov 24, 2015 at 7:06
  • $\begingroup$ Ah k...yea been playing with this for a while and can't get the "0" to show. $\endgroup$ Nov 24, 2015 at 20:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .