what wrong with this code?

please get this blend file and run the game engine please. All I do in 2.49b.
Simple use WASD to control the cube, press “q” to add a shooting Cone and “e” to remove it. Better if still use Top Ortho view to see the effect clearly:)

test_pickup_sample.blend (142 KB)

This is what I want. For simple condition like this, it work well enough. But how I wonder if I use many - many object as the cone? I think it will be troublesome to do and the logic for the Empty “mount_01” will so messy…

to simplify the process I try search on the web and at tutorialsforblender3d I found a sample command using object.setParent. I copy the code as is and get this blend file. But why the game didn’t run as I want? I mean as the first file that didn’t use python.

test_pickup.blend (147 KB)

someone can help me?
Thank u very much for the response.:slight_smile:

you define “mount” but use then “mount_01”, thats wrong.
You can add Objects by GameLogic.getCurrentScene().addObject(“Player”, cont.owner)

thax for help sevi, but it still didnt’work. this the file that i corrected according to your suggest. I commented line that i try to corrected.
test_pickup_corrected.blend (147 KB)

in console window it said
File “mount”, line 7, in <module>
"Key Error : “GList[key] '‘Cone’'key not in list”

where my wrong? :no:

Firstly it says that there is not an object called Cone in the scene.
Secondly, you need to use OB in front of object names in 2.4
corrected code

cont = GameLogic.getCurrentController()
scene = GameLogic.getCurrentScene()

own  = cont.owner
objList = scene.objects

con    = objList["OBCone"]
mount = objList["OBmount_01"]

pick = cont.sensors["sensPick"]
rmov = cont.sensors["sensRemove"]
actPick = cont.actuators["actPick"]
actRemove = cont.actuators["actRemove"]
actPick.object = "Cone"

print objList;

if pick.positive == True :
	if own["pick"] == False :
		GameLogic.getCurrentScene().addObject("Cone",own) # &lt;= change this part
		con.setParent(mount, 0, 1) # &lt;== corrected this
		own["pick"] == True	
if rmov.positive == True :
	if own["pick"] == True :
		own["pick"] == False

Hei agoose77,

it still didn’t work… in console it said

Traceback (most recent call last):
file “mount”, line 7, in <module>
KeyError: “CList[key]: ’ ’ OBCone’ ’ key not in list”

the cone object already there in layer2 (off)
i use blender 2.49b for this file…
if the code work, when in the GE i hit “q” it must adding the shooting cone as child of the cube as in file “test_pickup_sample.blend” did…
still confuse…

Threads like this are what’s wrong with this board.
Use some critical thinking skills to attempt a solution on your own. Look at line 7 in your file and find out why that cone isn’t there.

If the object isn’t on the same layer as what you are viewing, it isn’t in the scene object list.

If the object isn’t in scene.objects, is it in scene.objectsInactive? Sorry if this doesn’t help, but 2.5x is far more friendly for scripting.


Forget the new ui, the main improvement for non-artists in 2.5x is the api changes. This is the main reason that bge users moved to 2.5x when is was still deep in alpha. Unbelieveable control at your fingertips. I personally could never go back to 2.4x not because of the ui, but because of the Python api.

Hope you sort it out. :slight_smile: