How can I fix up my random object spawner?

Hey guys,

I decided to work into the BGE. I have learned a lot by following tutorials etc., but now I came to several problems which I cant fix alone.
I wanted to make a really basic strategy game. Therefore I needed a Spawner for my Buildings. It should spawn buildings on random, but appointed positions.
I figured I could solve this by making Spawnplatforms which will be randomly selected by my spawner.

For everyone who does not need the .blend to see the problem here is what I’ve done so far:

#leftclick triggers the script

from bge import logic
from random import randint




def main():
    scene = logic.getCurrentScene()
    cont = logic.getCurrentController()
    own = cont.owner
    objs = scene.objects


#counting the number of platforms
    num_platforms = -1    
    
    for item in objs:
        if 'platNum' in item:
            num_platforms += 1    
    
#all possible platforms                           
    existing_platforms = [scene.objects["spawnPlatform.000"],scene.objects["spawnPlatform.001"],scene.objects["spawnPlatform.002"],scene.objects["spawnPlatform.003"],scene.objects["spawnPlatform.004"],scene.objects["spawnPlatform.005"],scene.objects["spawnPlatform.006"],scene.objects["spawnPlatform.007"],scene.objects["spawnPlatform.008"]]
     
#choosing a random platform
    randomNumber =  randint(0,num_platforms)
    randomPlatform = existing_platforms[randomNumber]
#display number for debugging    
    objs['houseSpawner']['number'] = randomNumber
#moving cursor in 'spawn-position'
    spawn_input = cont.sensors["event"]
    
    if spawn_input.positive:
        own.worldPosition = randomPlatform.worldPosition
       ### del existing_platforms [randomnumber]

I have 3 problems at once right now:

  1. The randomly generated platform in my Code does not match the actual onscreen platform. This was once the case, but I dont know what led to that Error.

  2. The most important part - I am not able to delete the platforms out of my list, after they have been “visited” by my spawner. Because of that my buildings would stack over one another. I tried using “del existing_platforms [randomnumber]” but it did not work out.

  3. I was wondering if there is a good way to automatically add my platforms into the existing_platforms list. If I want to add further platforms I would not like to individually put them in my list.

If anyone knows a solution to one of my problems I would already be thankful :slight_smile:

One more thing: As I stated I am really new to coding. So please explain it as you would explain it to a child :stuck_out_tongue:
And my code will probably hurt your eyes due to my noobness ^^

if I violated any rules I am really sorry, for I this is my first post.

Greetings from Germany!

Gee

[ATTACH]424697[/ATTACH]

There seems to be a Problem with my .blend upload.
I hope this one will work.

Attachments

share.blend (738 KB)

Hi, I made a commented example: http://www.pasteall.org/blend/40786
It’s a bit advanced but I hope you will understand

oops: there is a little mistake at the end, when the grid is filled… (chooseRandomPosition must always return a sequence of 3 numbers [n ,n ,n])… oops2: I just realized that my code is not good because if you run chooseRandomPosition on a 100000*100000 grid and that it remains only 1 empty place, it could take a while before the empty place to be found. So my system needs to be re-thought…

Wow, thank you very, very much!
It doesn’t matter if it is advanced. At the first glimpse I understood at least half of it. Im sure I’ll figure the rest out in the next few days.
I am glad, that you took the time to come up with a even better solution :slight_smile:
I guess there is no possibillity to create an “uneven” Grid? I’ll probably have to build it out of multiple smaller ones.

Anyways - thanks.
Ill keep this thread open a little longer, in case that another question pops into my head :slight_smile:

Here are to simplified examples… I didn’t choose the simpler way the first time :slight_smile: :

“uneven” grid: you replace y in range(10) with y in range(20) for example

Thanks for your Time :slight_smile:
Your new blends are helpful aswell.

I expressed kinda unclear - sorry for that. Of course I have guessed how i’d change the proportions of the Grid. With “uneven” i meant that there are parts missing, or on another level. Like so (a bit exaggarated to show what I mean :wink: )



After all my little Level should not contain one massive block of buildings when it is done. So I was just wondering if theres a special Way to ignore certain parts of my Grid, or if I’d have to build my deformed Grid out of smaller ones.