A character that walks to a table and picks up a pencil.
Being a Python beginner I have trouble achieving that. I know how to spawn objects, however, how to spawn the object just right in the character’s hand and let it follow its movements I find quite tricky.
Can anybody walk me through this?
Thanks for all your hints,
Sputi
Make 2 instances of the pencil. Put one on the table, and make it visible. Put one in another layer. Add an empty. Parent and position it to the hand. Then spawn the pencil in game. Make it set a parent to the hand. Turn off visibility for table pencil.
or
Make 2 instances of the pencil. Put one on the table, and make it visible. Put the other one in the hand. Parent and position it to the hand. Make it invisible. In game when the pencil is needed, turn off visibility for table pencil and make the pencil in the hand visible.
It works just fine, HOWEVER I always get two pencils spawned; one in the hand that follows the armature bone just fine but also another pencil that sticks just where it has spawned and doesnt move.
Has anybody and idea?
import random
from bge import logic
cont = logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()
def spawnZiggi():
scene.addObject("ziggi", "spawnHandR")
# get a list of objects in scene
objList = scene.objects
# get the object named Cube from the list
ziggi = objList["ziggi"]
# get the object named Suzanne
emptyR = objList["spawnHandR"]
# make cube a child of suz, combine shapes, no ghost
ziggi.setParent(emptyR, 1, 0)
spawnZiggi()
Thanks, very helpful information. However I don’t get why my problem has something to do with the sensors since I spawn the pencil within the python script and not with the edit Object actuator. Or do am I missing something?
The controller gets triggered twice - on status change from False to True and from True to False. As you do not check the sensor status, your code runs twice.
Btw. this is just a guess. I do not know if that is the situation you have. Nevertheless checking the sensor state is always a good idea.
Well that’s what I’ve thought first too, but then only one pencil becomes parent to the hand, the other don’t. Thought this could be a known issue but I guess I have to look into it deeper now…
Yes, indeed the second pencil would be parented too. If you un-comment the line with the addObject() is there still a pencil added?
You can add a print() statement when adding via Python. so you see if it gets executed twice.
The trick with the print() showed that indeed the script gets exectued twice. Strange that one object gets parented the other doesn’t… I think what might have happened that some other objects could have been attached to the script as well and it runs now two times… is there a easy way to find out how many objects are using a certain script?
Turns out no matter what I do it always spawns an object that is parented and an object that is not. Doesn’t matter on which object the script is attached to, always the same result. I think I must go the make visible and unvisible path since the way of spawnign and parenting seems to be broken in some way…
I now know why it happens but don’t know the solution to the problem:)
It only happens when I let the pencil spawn from a property which is connected to a frame property of an animation. The property says “if the animation’s frame is equal to “x” - execute the python script”.
Also what I’ve found out is that if I change the property to “interval”, it seems that firstly it spawns the parented object and only after the pulse becomes false it adds the static object.
Very strange I must admit.
As said, it calls addObject twice. It should do that only when the condition is met rather than when it is not met. I can|t tell why the second object is not parented.
Yeah but WHY does it call addObject twice? How could it spawn the object if I disable addObject?? Or am I completely on the wrong train here? Thanks for your patience.
Nope it isnt, if I print (cont.owner) it returns only Armature.
It works however if I spawn the object with an actuator and tell it with the script to be parented. Still gets executed twice, I guess I’ll never find out why. At least I have it somewhat working now
Thx