Noob need help with Python (Blender Game engine) - Destructible wall

Hi everyone, I have already posted in “game engine support and discussion
but nobody has been able to help me at the moment .

I found a script on the forum,
Which activates the dynamics of objects in a certain area to the impact of a sphere.
But sometimes objects lose their dymanics after a certain time and I can not solve this problem.

I would also like to activate the dynamics of the objects above those in the impact area.
I tried with a ray sensor, but I’m just starting to learn python (just for this script)
and I can not get it

blend : tester.blend (645 KB)

Can anyone help me please? :smiley:

No one?

I solved the first problem, and I think I found the solution for the second.
But I can not put it on python.

I use a radius on my sphere,
which removes the parent of the bricks lying inside, and activates their ray sensor

I would like that the bricks hit by a ray sensor also activate their ray sensor,
in order to create a chain reaction

Blend. : Mon script4.blend (1.28 MB)
(Space to shoot, “q” and “d” to move)

Here is my code:

import bge
from bge import render
from bge import logic as gl

def bullet(cont):
own = cont.owner

  collision = cont.sensors["Collision"]
  message = cont.actuators["Action"]

  radius = own.get('radius', 3)

  if collision.positive and not "r" in own:
         bge.logic.bullet = (collision.hitObject, radius)
         own["r"] = 1

def brick(cont):
own = cont.owner
message = cont.sensors[“Message”]

if message.positive:
      hitObject, radius = bge.logic.bullet
      distance = own.getDistanceTo(hitObject)
        if distance < radius:
               ray = cont.sensors["Ray"]

if ray.positive:
      render.drawLine(own.worldPosition, ray.hitPosition, [255, 0, 0])
      cible = (ray.hitObject)
      ray.hitObject = cible.removeParent() 
      ray = cont.sensors["Ray"] 

The last line does not seem to work

First there’s errors in your script. You can see em in the console.
If you’re on Windows it’s Window -> Toggle System Console

Python script error - object 'Cube.132', controller 'Python':
Traceback (most recent call last):
  File "/home/user/Downloads/Mon script4.blend/", line 36, in brick
UnboundLocalError: local variable 'ray' referenced before assignment

The indentation at line 36 needs to be 1 level deeper.

AttributeError: module 'Scripts' has no attribute 'main'
Python controller found the module but could not access the function - object 'Cube.110', controller 'Python.001':

There’s no main function in the script. I assume that controller is redundant anyway.

Traceback (most recent call last):
  File "/home/user/Downloads/Mon script4.blend/", line 40, in brick
AttributeError: attribute 'hitObject' of 'KX_RaySensor' objects is not writable

Line 40 ray.hitObject is read-only, documentation here:

After fixing the errors it still does nothing, so I re-factored it using states and group instances.

-Changed bricks to group instances for less duplicated logic headache, defined in layer 4, see or google
-Radar instead of ray because bricks weren’t being detected at the seams
-Logic states, see
-Script reduced to 11 lines

Blend file:
destruction.blend (725 KB)

Thank you very much !! It’s perfect, and I understand how it works!
Except for the “logic states”
But I would see tomorrow, I need to sleep ^^
thanks again :smiley: