Can't animate and added object with addObject.

Hi,

I reported this as a bug in https://projects.blender.org/tracker/?func=detail&atid=306&aid=36929&group_id=9 but agoose77 told me that this isn’t a bug. There is an example .blend there.

I’m still trying to animate an object added with addObject but I couldn’t!
So, I’ll apreciate some help on this! :slight_smile:

Did you activate an action actuator on the added object?

I never discovered such problems with added objects. It often happens that the wrong object gets triggered.

You have your script set up incorrectly. If you aren’t going to use modules, you would need a separate script for each object. The script you wrote adds the object and runs the animation. You need to either separate these 2 things or use modules. I cleaned up your blend for you. Look at it and let me know if you still have questions. I used modules instead of scripts.

addObjectAnimation.blend (547 KB)

Edit: You’ll have to click the red button under the script and click “Recreate File” to create the external python script required.

Let’s say that the problem is: “tool” is not in active layer when “instanciador” initiates its controllers, so the “open” actuator of “tool” never gets bound, not even later on when “tool” becomes alive. There are like a ton of “what” that follows this assumption but let’s not dive deeper, we want to make games.
Otherwise everything would be perfectly fine with your setup (just activate both layers in the scene, run the game and you’ll see your animation up and running) but there is this thing and it doesn’t work.
Because tool is an inactive layer, you have to control the animation in some other way.
One way is using the script to play the action, bypassing entirely the “open” actuator. You remove it, then the script becomes:

from bge import logic as GL

instanciador = GL.getCurrentScene().objects['Instanciador']
object = GL.getCurrentScene().addObject('tool', instanciador)
object.playAction("Key.002Action.001", 150, 170)

Another way is to separate the actuator “open” from the controller “Python” and using it with a controller bound to “tool”, like:

add a game property to tool named “play action”, of type Boolean, the create a brick like:

sensor -> Property “play action” Equal “True”
controller -> and
actuator -> the “open” actuator.

The script becomes:

from bge import logic as GL

instanciador = GL.getCurrentScene().objects['Instanciador']
object = GL.getCurrentScene().addObject('tool', instanciador)
object["play action"] = True #<- this is the right call
#Argh mistake
#object.playAction("Key.002Action.001", 150, 170, play_mode=GL.KX_ACTION_MODE_PLAY)

Update: copy-paste is the root of all evils

Thanks!
The main problem was that I just really don’t understood how all that works. I try to use as less logic bricks as possible and with that ones that I’m using… I did it wrong! xD
In my opinion it’s still a bit counterintuitive, but you all gave me a lot of info about the problem and great solutions :slight_smile:
The best solution for my code is bypassing the actuator, thanks pgi :wink:

It’s not an illogical system. It’s a byproduct of the unusual ability to link logic between objects. It is good practice to avoid dependencies upon other objects and this should be avoided for this reason. However there will be times when you need objects to talk to one another, say to open a door etc. Yours is one such occasion, and the best way to achieve this Is either to use python as before or to use messages. In order to use messages, delete the link between the animation actuator and the controller and add a message actuator to the controller object. Then name the subject field and add a message sensor on the added object. Connect it to an and controller (though you can use or or and with only one sensor) and that to the animation actuator. Make sure that you don’t set the object field in the actuator because that will reference the base object and not the instance.

Let’s explain why it doesn’t work, your method. Whenever you add an object from the layer it will copy all of its logic. If you added multiple copies you’d have multiple objects connected to the same controller. That’s not always going to do what you’d expect - all connections should be explicit.

Thanks! I have now a much clearer idea :slight_smile: