spawn Player at vertex points

have some trouble to get the brackets from the “point_to_spawn” vector away.
the given vector looks like this:
[Vector,((x,y,z))]

[] <— this is what I want to remove without to convert to string


'''
spawn script to spawn at specific vertex points by the_cannibal

'''
from bge import logic
import random

def random_pick(list, number):
    choice = random.sample(list, number)
    return choice

def get_spawn_point(spawn_point_obj):
    position_list = []
    mesh = spawn_point_obj.meshes[0]
    got_last_v = (mesh.getVertexArrayLength(0) -1)
    for v in range(mesh.getVertexArrayLength(0)):
        if v != got_last_v:
            vert = mesh.getVertex(0, v)
            vert_position = vert.getXYZ()
            position_list.append(vert_position)
        else:
            spawn_point = random_pick(position_list, 1)
            return spawn_point

def spawn(cont):
    scene = logic.getCurrentScene()
    scobj = scene.objects
    to_spawnobj = scobj["HoverCar_Basic:Chrome"]
    spawn_point_obj = scobj["Spawn_Points_Player"]
    point_to_spawn = get_spawn_point(spawn_point_obj)
    print(point_to_spawn)
    #to_spawnobj.position = point_to_spawn

Point_to_spawn[0]?
Or maybe you need random.choice() not random.sample()

have solved it with an iterator:

for o in point_to_spawn:

but if I want to use it to add objects how can I add an object at an vector and not add an object?

that is the problem now: to add Object at Vertex position!


'''
spawn script to spawn at random vertex points by the_cannibal

'''
from bge import logic
import random

'''
def item_timer(start, stop, step):
    t = start
    if t &lt; stop:
        t += step
        print(t)
        return False
    else:
        return True
'''

def random_pick(list, number):
    choice = random.sample(list, number)
    return choice

def get_spawn_point(spawn_point_obj, choicepicks):
    position_list = []
    mesh = spawn_point_obj.meshes[0]
    got_last_v = (mesh.getVertexArrayLength(0) -1)
    for v in range(mesh.getVertexArrayLength(0)):
        if v != got_last_v:
            vert = mesh.getVertex(0, v)
            vert_position = vert.getXYZ()
            position_list.append(vert_position)
        else:
            spawn_point = random_pick(position_list, choicepicks)
            return spawn_point

def spawn(cont):
    scene = logic.getCurrentScene()
    scobj = scene.objects
    
    death_collide = cont.sensors["Death_Collision"]
    to_respawnobj = scobj["HoverCar_Basic:Chrome"]
    spawn_point_obj = scobj["Spawn_Points_Player"]
    spwan_points_items = scobj["Spawn_Points_Items"]
    item_obj = scene.objectsInactive["Cube"]
    timer = True
    #timer_start = 0
    #timer_stop = 10
    #timer_step = 1
    #trigger_spawn_items = item_timer(timer_start, timer_stop, timer_step)
    
    
    #if trigger_spawn_items == True:
    if timer == True:
        choicepicks = 4
        point_to_spawn = get_spawn_point(spawn_point_obj, choicepicks)
        for o in point_to_spawn:
            #scene.addObject[item_obj, o]
            print(point_to_spawn)
    
    if death_collide.positive:
        choicepicks = 1
        point_to_spawn = get_spawn_point(spawn_point_obj, choicepicks)
        death_obj = death_collide.hitObject
        death_obj.position = point_to_spawn[0]

@smoking_mirror Point_to_spawn[0]? <---- it can be so easy (so silly from me)

is the addObject modul calling position from object based on name and is it enough to get an dict looks like this [“name”, Vertex-Position] or should I have to recalculate the vertex to anything? I mean the standard process is:
…addObject[“object_to_add”, “position_from_other_object_to_spawn”, lifetime]
is there a way to simulate “position_objects” ?

ok solved it with this:


item_placer = scobj["item_placer"]

for o in point_to_spawn:
            item_placer.position = o
            scene.addObject(item_obj, "item_placer", 500)
            print(point_to_spawn)

to get an object at places the items should spawn.

Another way to do it would be using the camera, wich you can acces with bge.logic.getCurrentScene().active_camera, as the placeholder, and then moving the object to it’s true location on the same frame. Like


for o in point_to_spawn:
            obj = scene.addObject(item_obj, scene.active_camera, 500)
            obj.worldPosition = o

Of course you may also need to tweak orientation and size. The advantage of this is that you don’t need to create an extra object as the placeholder and that you will never have problems with finding the placeholder (since an active camera must always exist and is accesible from everywhere even without knowing its name).

quick tip, you can use None and it is faster than looking up scene.active_camera inside the loop


scene.addObject(item_obj, None, 500)

sorry guys but if I use “scene.active_camera” or None state only one object will spawn!

here is the complete code:


'''
spawn: script to spawn at random vertex points by the_cannibal

'''
from bge import logic
import random

def item_timer(obj, start, stop, step):
    if not "timer" in obj:
        obj["timer"] = start
    if obj["timer"] &lt; stop:
        obj["timer"] += step
        return False
    else:
        obj["timer"] = start
        return True

def random_pick(list, number):
    choice = random.sample(list, number)
    return choice

def get_spawn_point(spawn_point_obj, choicepicks):
    position_list = []
    mesh = spawn_point_obj.meshes[0]
    got_last_v = (mesh.getVertexArrayLength(0) -1)
    for v in range(mesh.getVertexArrayLength(0)):
        if v != got_last_v:
            vert = mesh.getVertex(0, v)
            vert_position = vert.getXYZ()
            position_list.append(vert_position)
        else:
            spawn_point = random_pick(position_list, choicepicks)
            return spawn_point

def spawn(cont):
    scene = logic.getCurrentScene()
    scobj = scene.objects
    
    death_collide = cont.sensors["Death_Collision"]
    spawn_point_obj = scobj["Spawn_Points_Player"]
    spwan_points_items = scobj["Spawn_Points_Items"]
    item_placer = scobj["item_placer"]
    item_obj = scene.objectsInactive["Cube"]
    timer_start = 0
    timer_stop = 300
    timer_step = 1
    timer = item_timer(spwan_points_items, timer_start, timer_stop, timer_step)

    if timer == True:
        choicepicks = 8
        point_to_spawn = get_spawn_point(spwan_points_items, choicepicks)
        for p in point_to_spawn:
            item_placer.position = p
            scene.addObject(item_obj, "item_placer", 600)
    
    if death_collide.positive:
        choicepicks = 1
        point_to_spawn = get_spawn_point(spawn_point_obj, choicepicks)
        death_obj = death_collide.hitObject
        death_obj.position = point_to_spawn[0]
        death_obj.orientation = [[1.0, 1.0, 1.0],[1.0, 1.0, 1.0],[1.0, 1.0, 1.0]]

it is working extreme well:cool:

it isn’t finished yet but for tests it is a good start

@the_cannibal

elmeunick9 script shows you can directly assign the position to all instances without keeping a placeholder, you can do something like this


scADD = scene.addObject #localize for immature optimization
for p in point_to_spawn:
   scADD(item_obj, None, 600).worldPosition = p

@guramarx
ah, have update the script thanks.


'''
spawn: script to spawn at random vertex points by the_cannibal

'''
from bge import logic
import random

def item_timer(obj, start, stop, step):
    if not "timer" in obj:
        obj["timer"] = start
    if obj["timer"] &lt; stop:
        obj["timer"] += step
        return False
    else:
        obj["timer"] = start
        return True

def random_pick(list, number):
    choice = random.sample(list, number)
    return choice

def get_spawn_point(spawn_point_obj, choicepicks):
    position_list = []
    mesh = spawn_point_obj.meshes[0]
    got_last_v = (mesh.getVertexArrayLength(0) -1)
    for v in range(mesh.getVertexArrayLength(0)):
        if v != got_last_v:
            vert = mesh.getVertex(0, v)
            vert_position = vert.getXYZ()
            position_list.append(vert_position)
        else:
            spawn_point = random_pick(position_list, choicepicks)
            return spawn_point

def spawn(cont):
    scene = logic.getCurrentScene()
    scobj = scene.objects
    addObj = scene.addObject
    
    death_collide = cont.sensors["Death_Collision"]
    spawn_point_obj = scobj["Spawn_Points_Player"]
    spwan_points_items = scobj["Spawn_Points_Items"]
    
    item_obj_list = ['item_speedup', 'item_fire', 'item_slowdown', 'item_bomb', 'item_rage', 'item_thunder', 'item_trap', 'item_repear', 'item_imun']
    
    timer_start = 0
    timer_stop = 300
    timer_step = 1
    timer = item_timer(spwan_points_items, timer_start, timer_stop, timer_step)
    
    if death_collide.positive:
        choicepicks = 1
        point_to_spawn = get_spawn_point(spawn_point_obj, choicepicks)
        death_obj = death_collide.hitObject
        death_obj.position = point_to_spawn[0]
        death_obj.orientation = [[1.0, 1.0, 1.0],[1.0, 1.0, 1.0],[1.0, 1.0, 1.0]]
        death_obj.linearVelocity = [0.0, 0.0, 0.0]
    
    #
    # think about settings to disable items!!! ,you more confused
    #
    
    if timer == True:
        choicepicks = 8
        point_to_spawn = get_spawn_point(spwan_points_items, choicepicks)
        for p in point_to_spawn:
            choicepicks = 1
            item_choice = random_pick(item_obj_list, choicepicks)
            addObj(item_choice[0], None, 600).position = p