Collecting coins

I got a plaform kinda thingy going on and I’m collecting coins xD

The coins are collected by:
A near sensor on the coin that filters the player property (player is just a cube)
That near sensor sends a message and ends the object

On-screen you see an overlay with a counter.
The counter is set up to receive the message and add one to the property that then gets assigned to text.

But when collecting lots of coins in short amount of time I lose some coins.

I now how many are in my level but I never get that number on screen.

any suggestions?

I’ve seen this bug myself. A good idea is to use a Collision sensor instead of a Near sensor. That way, you’ll be triggering fewer coins at a time (which in my experience is the problem, the logic can only keep up with so many things per frame), while also cutting down on resource use (near sensors are major resource drains).

When I use the collision sensor i can jump on/against them before they dissapear. that’s why I tried something else.
Also the near is set to tap is it still a big resource drain then?

You can enable ghost physics on the coin objects, (Meaning they detect collisions but don’t actually react to them, like stopping your character or actually colliding with it)

But if you already did add ghost physics to the object but aren’t satisfied by having to actually touch the object to make it disappear, you can simply give it a collision object bigger than the actual coin and make it invisible.

Collision sensor in player

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
Collision = cont.sensors['Collision']

if Collision.positive:
    for objects in collision.hitObjectList:

Collision(property coinValue)----------(thisPython)

property ‘CoinValue’ must be in coins and be an integer like 1, 5, 10 etc

The design does not support collecting multiple coins at the same frame very well.

A) when the collected coin sends a message, several messages can be received at the same time.

use a custom brick (Python controller) to deal with all received messages. The advantage is you could send the coin value in the body of the message.


import bge

def printCoinValues():
    value = sumMessageBodies()
    print("value of collected coins this frame:", value)

def sumMessageBodies():
    total = 0
    bodies = getMessageBodies()
    for body in bodies:
        total += float(body)
    return total

def getMessageBodies():
    bodies = []
    for sensor in bge.logic.getCurrentController().sensors:
             bodies += sensor.bodies
        except AttributeError:
    return bodies

You want to try it?

  1. Place the snippet in a file called
  2. bricks: message sensor -> Python controller Module: demo.demo.printCoinValues
  3. run the game and check the console for output.

B) sensing multiple coins at the same time
Similar to the message solution, this requires more dynamic behavior. The default behavior deals with the fact that anything gets sensed. It does not deal with the sensed objects. So you need a custom bricks that does that for you.

I suggest you look at post#5 for a code snippet

So if I understand correctly?

BPR’s code: if I collide with an object coin with the property “CoinValue” it ends the object.

That part worked fine, all the coins get ended by collision or the near sensor.
It’s the counter that can’t handle the amount of messages in a short amount of time.

Monster’s Code: makes a function printcoinvalue by counting the number of message-bodies per frame.

I’ve tried both the script and the module.

I can’t make either work!
With BPR’s code the obj’s don’t end when i collide and nothing in the console for Monster’s module :frowning:

Again I’ve only made a script run once!
So probably I’m doing something wrong.

On what do i put the module? Does it even matter? it just needs to be a message sensor connected to the module right?

So does every coin need another value or just one value for every coin?

You want a short demo?

here is a very simple one.


Look at scene overlay to find the counting object
Look at layer 2 (of the level scene) to see the coin setup.


Collect_Coins.blend (485 KB)

Ok I managed to get it working in my file thx to the demo.

Tested it!
Works like a charm :smiley:

Maybe this is of-topic but…
If one wants to learn python, where to start?
Start scowering internet for resources, get a book…
Tell me! enlighten me xD

There are a plenty of quite good online curses including exercises. I suggest to look for pure Python (without BGE). I do not have a link available. I just know they exist ;).