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)
         cont.activate(message)
         own["r"] = 1

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

if message.positive:
      hitObject, radius = bge.logic.bullet
      distance = own.getDistanceTo(hitObject)
      #print(dir(own))
    
        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/Scripts.py", 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/Scripts.py", line 40, in brick
AttributeError: attribute 'hitObject' of 'KX_RaySensor' objects is not writable

Line 40 ray.hitObject is read-only, documentation here: https://www.blender.org/api/blender_python_api_2_78a_release/bge.types.KX_RaySensor.html

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 http://www.blendernation.com/2015/11/08/blender-tip-group-instances/ or google
-Radar instead of ray because bricks weren’t being detected at the seams
-Logic states, see https://www.blender.org/manual/game_engine/logic/states.html
-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: