Collisions and sensors II

A typical use case is to find out what kind of token has been touched.

There are several solutions to this problem:

Attribute token_type

You can add a token_type attribute to all your objects:

Warning

With this access, you must give every object a token_type attribute.

Otherwise you must also check if this is present at all, if you don’t want your whole program to crash otherwise.

This can be done with:

if other_token.token_type and other_token.token_type == "actor":

If each token has the token_type attribute, then you can omit this query.

Listen

You can add objects to a list to check if the touched object is in this list.

warning:

With this access you have to take care that deleted objects are also removed from the list e.g. in the following way:

.. code-block:: python

  walls.remove(wall)
  wall.remove()

Classes

If you work with classes, the miniworldmaker takes some work off your hands, because it can now recognize for itself which child class of token an object is.

Here you can add the following method to your class:

def on_sensing_[class-name](self, other)

Example

View