Block Placement / Destruction Script?

Hello All,

I need a block placement and destruction script for a project that I would like to do. I think I fully understand how block placement works, I am just not familiar with Python programming in the BGE. I have been studying other various block placement scripts, but they all seem to have features other than placement and destruction of the blocks that make the code un-understandable.

How I see that block placement works is this:

Use a Ray sensor to see what the normal face of the cube is.
Then there is a calculation performed where the normal position is added to the cube’s position to figure out where to add the new cube.
I am not sure how the new cube is added.

If someone would be so kind as to help me create a script of my own or point me to where I can learn how to make one it would be very much appreciated. :smiley:

here you are :smiley:

Attachments

PlaceBlockExample.blend (443 KB)

Thank you!:slight_smile:
So how would one spawn the cubes when clicking rather than snapping the player to the block?
Or is this just a base script to show how it works?

it’s just a base script

you would just use

add=scene.addObject(“Block”,own,0)
add.worldPosition=(code from before)
add.worldOrientation=(code from before)

to place a new block

I also added a logic based timer, so building does not go out of control (place place place…place)

Attachments

PlaceBlockExample2.blend (453 KB)

Thanks a million! :slight_smile: :slight_smile:

This helps a lot!

Basically what I use.
Open system console to read the debug.
Mouse cursor should be set visible in the settings.

import bge

# block placement and deletion script
def manipulation():
   
    scene = bge.logic.getCurrentScene() 
    cont = bge.logic.getCurrentController()
    own = cont.owner


    #access all the sensors
    sensor          = cont.sensors['over_any']
    left_click      = cont.sensors['left_click']
    right_click     = cont.sensors['right_click'] 
    
    #get the information needed
    hit_object      = sensor.hitObject
    hit_normal      = sensor.hitNormal
         
    
    #for debug
    print(" ")
    print("Hit object:",hit_object)   
    
    # proceed to add block
    if left_click.positive: 
       
        #make sure we hit something
        if  hit_object :
            
            #debug
            print("adding blocks")
            print("Normal:",hit_normal)
            print("Hit obj pos:",hit_object.worldPosition)
            print("New location:",hit_normal+hit_object.worldPosition)
            
            
             # get new position based on mesh location and the handy hit_normal
            NewPosition     = hit_object.worldPosition + (hit_normal)
                         
            # add new block to scene
            new_block       = scene.addObject("block",own)
            # by default i thas to spawn somewhere,
            # the owner of this script is default
            
            # now lets set its's position to where we meant it to be
            # based on the calculations
            new_block.worldPosition = NewPosition
            
            # Be aware, that spawned object inherit the rotation of the script owenr
            # the camera has rotation, so we must reset it
            # you can comment this out to see why it's important
            new_block.worldOrientation = (0,0,0)
            
    
    # proceed to delete block
    elif right_click.positive:
        
        #make sure we hit something
        if  hit_object :
            
            # debug
            print("Delete block")
            
            # just kill it
            hit_object.endObject()
            
           


manipulation()

without the clutter:

import bge

# block placement and deletion script
def manipulation():
   
    scene = bge.logic.getCurrentScene() 
    cont = bge.logic.getCurrentController()
    own = cont.owner


    sensor          = cont.sensors['over_any']
    left_click      = cont.sensors['left_click']
    right_click     = cont.sensors['right_click']
    hit_object      = sensor.hitObject
    hit_normal      = sensor.hitNormal


    if left_click.positive: 
        if  hit_object :
            NewPosition     = hit_object.worldPosition + (hit_normal)
            new_block       = scene.addObject("block",own)
            new_block.worldPosition = NewPosition
            new_block.worldOrientation = (0,0,0)


    elif right_click.positive: 
        if  hit_object : 
            hit_object.endObject()
            
manipulation()

Attachments

block_placement_easy.blend (295 KB)

So, how would one keep a block from being placed inside the player with BPR’s script?

The only thing vegetablejuiceF with your script is if he delete all the blocks and starts over,it stops working.I think you need to fix that.I know because i tried it.

diff = own.worldPosition-sens.hitPosition
Dist=diff.magnitude:
if Dist>2:
(do stuff)

Attachments

PlaceBlockExample3.blend (454 KB)

I wouldn’t consider it a bug.
I chopped everything off from my own script to make it as simple as possible.
With any block placement script there should always be more code for choosing what to spawn, checking if the spot is taken and keeping track of objects added.
You could also be asking me why I don’t have mouselook, walkable character or a full game…

To exclude certain objects, you would give them some property.

 if  hit_object :

should be then something like

 if  hit_object   and  hit_object["editable"]==True:

(==True is easier to read for beginners)
Even if edit object would be a “None” object it would work.