BGE 2.78: How do I add object X to object Y in scene Z?

I’m having problems adding an object to another scene (inventory hud scene).

main scene where the game is running = Scene
inventory overlay scene = inventory
object to be added = log (on the second layer of “Scene”)
empty object in scene “inventory” = slot1

In the main character script in “Scene” I have tried the following code:

hud = logic.getSceneList["inventory"]

hud.addObject['log','slot1']

But the code doesn’t work.

This is the complete code on the player:

import bge



cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner
hud = logic.getSceneList["inventory"]


move = cont.actuators['move']       # set motion actuator variable
pressup = cont.sensors['up']        # set Keyboard up variable
pressdown = cont.sensors['down']    # set Keyboard down variable
pressleft = cont.sensors['left']    # set Keyboard left variable
pressright = cont.sensors['right']  # set Keyboard right variable
jump = cont.sensors['jump']         # set Keyboard space variable
speed = move.dLoc[1]                # set motion variable
rotation = move.dRot[2]             # set rotation variable
hitGround = cont.sensors['hitGround']
treeHit = cont.sensors['treeDetect']
logHit = cont.sensors['loghit']
logObj = logHit.hitObject
hitObj = treeHit.hitObject
lmb = cont.sensors['lmb']
use = cont.sensors['use']
forcer = [ 0.0, 200.0, 0.0]
forcel = [ 0.0, -200.0, 0.0]
local = True
player = scene.objects['player']






if pressup.positive:        # Move forward
  speed = 0.2
  move.dLoc = [0.0,speed,0.0]
  cont.activate(move)
    
elif pressdown.positive:    # Move down
  speed = -0.2
  move.dLoc = [0.0,speed,0.0]
  cont.activate(move)
    
else:                       # stop moving
    speed = 0
    move.dLoc = [0.0, speed, 0.0]


if pressleft.positive:      # rotate left
    rotation = +0.05
    move.dRot = [0.0, 0.0 , rotation]
    cont.activate(move)


elif pressright.positive:   # rotate right
    rotation = -0.05
    move.dRot = [0.0, 0.0 , rotation]
    cont.activate(move)
     
else:                       # stop rotating
    rotation = 0
    move.dRot = [0.0, 0.0, rotation]
    
if jump.positive and not own['jumping']:      # jump
    own.applyForce([0,0,2000],True)
    own['jumping'] = True
    
if hitGround.positive:     # jump
    own['jumping'] = False
    


# delete trees if lower then 1 hitcount
    
if lmb.positive and treeHit:
    hitObj['hitcount'] -= 1
    if hitObj['hitcount'] == 0:
        addlog = scene.addObject('log','itemDropper')
        addlog1 = scene.addObject('log','itemDropper')
        addlog2 = scene.addObject('log','itemDropper')
        addlog.worldPosition = treeHit.hitPosition
        addlog1.worldPosition = treeHit.hitPosition
        addlog2.worldPosition = treeHit.hitPosition
        a,b,c = addlog1.localPosition
        addlog1.localPosition = [a,b,c+10]
        d,e,f = addlog2.localPosition
        addlog2.localPosition = [d,e,f+20]
        hitObj.endObject()
        addlog.applyForce(forcer, local)
        addlog1.applyForce(forcel, local)
        addlog2.applyForce(forcer, local)


if use.positive and logHit.positive:
    logObj.endObject()
    player['logamount']+=1
    
# if player['logamount'] > 0:
hud.addObject['log','slot1']


if logHit.positive and player['logpickuptext'] == False:
    player['logpickuptext'] = True
    scene.addObject('pickuplog','player')
    scene.objects['pickuplog'].worldPosition = player.worldPosition


if logHit.positive:
       scene.objects['pickuplog'].worldPosition = player.worldPosition
    
if not logHit.positive == True:
    player['logpickuptext'] = False
    scene.objects['pickuplog'].endObject()



Anyone have an idea how to get this to work?

You are using:
scene = bge.logic.getCurrentScene()

so thats the scene your object will be added to.

assuming you added the hud scene in game, as far as i know, you cant reference scenes by name. also, your missing “bge.” before logic


import bge

scene = bge.logic.getSceneList()[0]
hud = bge.logic.getSceneList()[1] #scenes are listed in the order they were added


## OR ##
#
#from bge import logic #exclusively import the logic module
#
#scene = logic.getSceneList()[0]
#

you can define scenes by name, but you need to check if it exist.


from bge import logic


def get_scene(scene_name):
    
    scenes = logic.getSceneList()      
    scene = None
         
    for sce in scenes:
        if sce.name == scene_name:
            scene = sce
    
    if scene == None:     
        print('
*** No scene found with the name: ' + str(scene_name) + ' ***
')
    else:
        return scene

use this example:

inventory_scene = get_scene('inventory')

Make sure that the object you want to add is on layer 2 of the inventory_scene, not the main scene.
also make sure you grab the object before adding, saves a few headaches, like this:

object_to_add = inventory_scene.objectsInactive['object_to_add']

then you should be able to do:

inventory_scene.addObject('object_to_add', 'spawn_location')

well, i was referring to the logic.getSceneList[“Name”] method in the code, which isnt correct. i dont think logic.getSceneList()[“Name”] would work either.

if 'StoreScene' not in own:
    for scene in bge.logic.getSceneList():
         if scene.name == "waffles":
              own['StoreScene']=scene]

@mr. pancake

hud.addObject['log','slot1'] 

should actually have round brackets.

EDIT: Oh, and I wanted to mention: follow Daed’s helpful syntax as well (getSceneList is actually getSceneList(), and don’t reference by name, add in bge before ‘logic’).

Got it to work!

Code on empty object wich adds the inventory as overlay once:

bge.logic.addScene(‘inventory’,1)

Get the scene list:

scenes = bge.logic.getSceneList()

Code for adding an object to the hud where scenes[1] gets inventory from the scene list:

if player['logamount'] > 0:
    scenes[1].addObject('item_log','slot1')

I don’t know for sure, but will the addobject infinitely add the item_log object if logamount isn’t 0?

Assuming you have an always sensor that is positive every frame executing that code, the item_log object will constantly be added as long as player[‘logamount’] is greater than 0.

Ah yes it will only be ran forever if there is an always sensor with true triggerering. Thanks for the help!