I have set up a camera with a script and logic bricks so that when I use left mouse button it creates an object and places it at the specific click location.

enter image description here enter image description here

Nevertheless, I encountered an issue with that the first of my created object doesn't care about the lines following cont.activate(Add)and leaves it at the camera location, just as seen in the picture below (the cube and rectangles are other objects, the one I'm calling is the cone).

Why is that ?

enter image description here


You need to take care of the timing.

Within a single frame (over the complete scene):

  • sensors evaluation
  • controllers execution
  • actuators running
  • physics, render etc.

The code always runs with "controllers execution". This does not mean that all operations have immediate effect.

Activating an actuator, does not make the actuator running at the moment of the call. The actuator gets a signal to activate. The actuator will run within "actuators running". Which is later than your code.

KX_SCA_AddObjectActuator.objectLastCreated provides you a reference to the last created object by that actuator. That can be last frame or any frame before.

Within the first execution of your Python controller the actuator never added a new object. This means you get None. I expect you get an error each time you trigger the controller the first time as None has no "worldPosition" (always have the console window open when you run Python code with the BGE).

There are several ways to solve the timing issue.

A) Two step processing. You store the position and adjust the position of the new object within the next frame. The drawback is, that you can see the unchanged position for a single frame. This can be annoying.

B) Do not activate the actuator, but let it add the object via instantAddObject(). AFTER that call objectLastCreated will return the just created object.

You code could look like that (supposed to run in script mode):

import bge

controller = bge.logic.getCurrentController()

mouseSensor = controller.sensors["Ray"]
activationSensor = controller.sensors["Clik"]

if activationSensor.positive and
    addActuator = controller.actuators["add"]
    addedObject = addActuator.objectLastCreated
    addedObject.worldPosition = mouseSensor.hitPosition

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.