0
$\begingroup$

If new_piece = scene.addObject(shape) makes a piece appear, why does new_piece = scene.endObject(shape) NOT make the piece disappear?

if key[bge.events.PAD5] == pressed:              # If 5 key pressed
    target = scene.objects['Empty1']             # Find where it goes
    new_piece = scene.addObject(shape)           # Create the piece..
    new_piece.worldPosition = target.worldPosition  # and place it.

if key[bge.events.PAD0] == pressed:
    new_piece = scene.endObject(shape)
                # 'KX_Scene' object has no attribute 'endObject'

This is the simplest game I could make to figure this out. The entire blend file is here: https://github.com/quazipseudo/Done-Gone/blob/master/ApDisap002.blend?raw=true

$\endgroup$
0
$\begingroup$

The process of ending an object is different to the process of creating it. Typically you delete it using a reference to the object, using the function piece.endObject()

Modifying your above code:

if key[bge.events.PAD5] == pressed:              # If 5 key pressed
    target = scene.objects['Empty1']             # Find where it goes
    new_piece = scene.addObject(shape)           # Create the piece..
    new_piece.worldPosition = target.worldPosition  # and place it.

if key[bge.events.PAD0] == pressed:
    # First up we retrieve the existing object. In this case we get it by name.
    # Make sure the name is the name of the object you want to remove
    existing_object = scene.objects.get(shape)
    # If it exists, then remove it
    if existing_object is not None:
        existing_object.endObject()
$\endgroup$
  • $\begingroup$ Yes! existing_object = scene.objects.get('Ball'), I replaced 'existing_object' with just 'x' to see if it was a random variable or something bge would always recognize, and that worked too. So you could use any variable name. It surprises me that setting a variable to an instance of an object gives the variable authority over the object. I find it counter intuitive. I would expect changing the obj would reset the var, not the reverse. Following that logic, would this progression be correct? (a = 'Dog'), (b = 'Cat'), (new_variable = 'Dog'), (new_variable = 'Cat'), (a = 'Cat')???? $\endgroup$ – quazipseudo Nov 30 '17 at 21:31
  • $\begingroup$ There are two separate things: using an object and operating on an object. If you have a=3, and then do a+5,you don't expect the variable 'a' to change. This is using the variable 'a'. But if you have a car, and paint it blue, then you expect the car to be blue. So in your cat/dog example, 'a' will remain as dog. $\endgroup$ – sdfgeoff Nov 30 '17 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.