How to access objects from specific layer in blender game engine
Question:
I want to make a game with progressive level generation. I have some diferent parts of the level on another layer and I spawn them in the first layer using add object in python controller.
The problem is when there are multiple objects in the first layer which all have the same name. Then instead of accessing the original object on another layer, blender spawns in the object from first layer.
How can I access objects
Here’s some code
#import
import bge
from bge import logic
#define add
add = logic.getCurrentScene().addObject
#add object
add('objectName',' location',0)
Answers:
If you wish to directly access an object you could make a list and access it:
import bge
from random import random
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
#Your list
objlist = [obj for obj in scene.objects]
entity = objlist[obj]
add = scene.addObject(obj,loc,0)
add(entity,' location',0)
or call the object directly:
import bge
from random import random
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
#direct access
entity = scene.objects['obj']
add = scene.addObject(obj,loc,0)
add(entity,' location',0)
I want to make a game with progressive level generation. I have some diferent parts of the level on another layer and I spawn them in the first layer using add object in python controller.
The problem is when there are multiple objects in the first layer which all have the same name. Then instead of accessing the original object on another layer, blender spawns in the object from first layer.
How can I access objects
Here’s some code
#import
import bge
from bge import logic
#define add
add = logic.getCurrentScene().addObject
#add object
add('objectName',' location',0)
If you wish to directly access an object you could make a list and access it:
import bge
from random import random
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
#Your list
objlist = [obj for obj in scene.objects]
entity = objlist[obj]
add = scene.addObject(obj,loc,0)
add(entity,' location',0)
or call the object directly:
import bge
from random import random
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
#direct access
entity = scene.objects['obj']
add = scene.addObject(obj,loc,0)
add(entity,' location',0)