I am trying to make a simple 10 x 10 grid of nodes with a model instanced to each. here is how I have it set up:
block = loader.loadModel('block.glb')
block.reparentTo(self.worldNP)
size = 10
spacing = 2
nodeCount = count
self.level = []
for i in range(100):
tile = self.worldNP.attachNewNode(f'tile{i}')
self.level.append(tile)
block.instanceTo(tile)
# tile.setX(i+1)
for x in range(0, size, spacing):
for y in range(0, size, spacing):
if nodeCount > 0:
tile.setPos(x,y,0)
nodeCount -= 1
When I do this, the block is only instanced twice instead of the whole grid. I can see that the nodes are spaced out properly by using getPos() on each node in the loop. Any help is greatly appreciated!
I believe that it’s because you’re creating and instancing to your tiles in the first loop (the “i in range(100)” loop), rather than within the x- and y- loops.
As a result, the code is creating a tile, instancing to it, then moving it iteratively across x- and y- before leaving it in the last such position.
(And doing that 100 times over.)
If you instead create your tiles and instance to them within the “for y in range...” loop, you should find your grid appearing!
That said, let me ask if I may: why are you instancing here…?
hmm when I move the instanceTo into the x and y loops, I see no change:
for i in range(100):
tile = self.worldNP.attachNewNode(f'tile{i}')
self.level.append(tile)
# tile.setX(i+1)
for x in range(0, size, spacing):
for y in range(0, size, spacing):
if nodeCount > 0:
tile.setPos(x,y,0)
block.instanceTo(tile)
nodeCount -= 1
You’re still creating the tile outside of the x- and y- loops, however. As a result, you’re still just moving the tile around as you instance things to it.
for x in range(0, size, spacing):
block.instanceTo(tile)
for y in range(0, size, spacing):
block.instanceTo(tile)
if nodeCount > 0:
tile.setPos(x,y,0)
nodeCount -= 1
I mean that the actual creation of the tile–the first two lines within the “for i in range...” loop–would be within the x- and y- loops.
You see, at current, in the i-loop you’re creating a tile. In the x- and y- loops you’re then setting its position–but since it’s the same tile every time–you’re not currently creating a new one for each position–all that does is update the position of the tile: it doesn’t leave another tile in its place.
So I’m suggesting that you instead create a new tile for each position–i.e. that each time you “go to a new position” you create a tile for that position.