0
$\begingroup$

I'm trying to set up physics through python, what I want is an object fired from an empty at every mouse click. My first problem is spawning an object (the bullet), my class asks for a gameObject which can't be a "bpy" data as I tried here, how can I tell python that I want to use this "Icosphere" object ? Same issue with the KX_SCA_AddObjectActuator, I need to provide the refered object. enter image description here In this picture the click pops up a sphere but it creates two of them, one on pressure and one on relief, just as when following the answer. I want only on object created with a mouseclick.

enter image description here

Added a check for the sensor status (from this list): I think it speaks for itself.

enter image description here

$\endgroup$
2
$\begingroup$

Never ever use bpy within bge code.

  • bpy is the Blender API (not available without Blender, will not effect running scenes)
  • bge is the BGE API

An object can easily be created via:

copiedObject = addObject(originalObject, referenceObject, time=0)

See addObject()

The new object will inherit

  • the mesh, physics and material data from originalObject
  • the position, orientation, scale, velocities from the referenceObject

ReferenceObject needs to be inactive (at an hidden layer).

Both - originalObject and referenceObject - can be provided as name (string). If there is no such object with the given name, the call will fail. If there is more than one object with the same name, any of the found one will be used for copying.

example code, assuming you have "Empty" at layer 1 and "Icosphere" at layer 2 (hidden).

import bge

scene = bge.logic.getCurrentScene()
newSphere = scene.addObject("Icosphere", "Empty")

I hope it helps

$\endgroup$
2
  • $\begingroup$ I tried it with these commands and it does what is said but creates one object on mouse click and another one at release. Strange ? Maybe I will go to the addObject controller instead. Please look at the edited post. $\endgroup$
    – Yvain
    Jul 5 '16 at 14:42
  • $\begingroup$ The controller will run each time any sensor triggers it. As you use a custom controller, you have to check the sensors' state by yourself. $\endgroup$
    – Monster
    Jul 6 '16 at 4:05

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.