Deleting objects according to properties

Hi,

I’m working on a Minecraft type clone in Blender. Click on that minecraft link or here if you don’t know what Minecraft is.

I already tried adding logic on the ground cubes:
Mouse Over Edit Object: Delete
+ ----------AND--------
Mouse Left Click (TAP)

But I want to have the deleting “mechanism” inside the player instead of the individual cubes. So how do I delete an object without having the logic in it? I found this code and I thought I could use it for this need:

def main():

    cont = GameLogic.getCurrentController()
    own = cont.owner
    
    sens = cont.sensors['hit']
    
    if own.init == 0:
        own.init = 1
        
    if sens.positive:
        sens.hitObject.endObject()

main()

I edited the original code that I found from someone else so I’m not sure how important the “if own.init==0, own.init = 1” part is. But my objective was to have this code check if the object my mouse was over and left clicking had a property of “ground”. If it had that property it would delete said object, if it didn’t have that property nothing would happen.

Input: Mouse must be over object, object must have property “ground” (or custom name), and left click must be positive. Outcome: delete object that mouse was over.

Any ideas?

I’ll post the original .blend file that has the original code in it from the thread I found it in.

it was made by jplur, i think i got the name right.

attached files:
blendcraft - the one i’m trying to do this upgrade on
inventory - as listed above
deletecube.png - current logic set up on individual sandbox cubes

Attachments

inventory.blend (176 KB)


blendcraft.blend (107 KB)

I didn’t get your blend at all…so I am just going to give you a generic script.

This script needs a mouseover sensor named mouse and a click sensor named click


import GameLogic

cont = GameLogic.getCurrentController()
own = cont.owner

mouse = cont.sensors["mouse"]
click = cont.sensors["click"]

if mouse.positive and click.positive:
mouse.hitObject.endObject()


Please ask questions.
-Sunjay03

Hi,
I understand what this script does. I’ll rephrase what I meant earlier.

If by using this script you showed here, is it possible to check if the object I am clicking on has a certain property at a certain value? Such as “prop: ground…value: 0”, and if it is true that the object being clicked has this property at the specific value, to delete the hit object?

This would prevent the player from accidentally deleting objects without the property. If possible, would it be able to search for just the property name without requiring a value such as 0? (just searching the hit object for a specified property name)

Thanks for the help

Here you go:


import GameLogic

cont = GameLogic.getCurrentController()
own = cont.owner

# the property that the object must have
PROP = 'Prop' 	# Change to own['PropName'] to assign to a prop on this object

# the value that property must be at
VALUE = 1 		# Change to own['PropName'] to assign to a prop on this object

mouse = cont.sensors["mouse"]
click = cont.sensors["click"]

if mouse.positive and click.positive:
	if not PROP or (mouse.hitObject.has_key(PROP) and mouse.hitObject[PROP] == VALUE):
		mouse.hitObject.endObject()


Leave prop as None to delete all objects under the mouse.
-Sunjay03

Thanks! This works great. I’ll post the .blend file of it below.
wasd - move
space - jump
left click - delete blocks

The only problem now is that with ~250 cubes, it reduces the game engine speed to about 9 fps :frowning:

Attachments

blendcraft python.blend (104 KB)