Ah, I see, I believe.
The thing is, what you put on the left-hand side of a single equals-sign is taken by the program to be the variable to which you want to assign whatever you have on the right-hand side of the equals-sign.
Thus, in your first excerpt, you were assigning the result of the call to âattachNewNodeâ to to i-th item in a variable named âself.modelnodeBââand if you hadnât yet created anything called âself.modelnodeBâ, then the program would fail as a result.
Do you mean the internal node-name (i.e. what you currently have as âmodelAâ)? If so, then you can use string-methodsâa simple way would be something like this:
for i in range(20):
name = "modelA" + str(i)
That should take the value in âiââwhich should update with each run of the âforâ-loopâthen convert it into a string, and then append that string to âmodelAâ. The result, if printed in each iteration, might be something like this:
modelA0
modelA1
modelA2
# And so on...
(There are arguably-better ways, but those are more complex, and Iâd rather stick to simple approaches for the moment.)
If, however, you mean the name of the variable in which you store the result of the call to âattachNewNodeâ, then thatâs a little more complicated.
Defining a new variable each iteration is possible, but a little complex.
However, a relatively-simple approach might be to store your results in a list, adding to the list via the âappendâ method. Something like this:
# define the list before the loop
self.myList = []
for i in range(20):
newNodePath = self.worldNP.attachNewNode(BulletRigidBodyNode("modelA"))
self.myList.append(newNodePath)
You can then access these items via their index. Something like this:
someObject = self.myList[2]
# "len" is a built-in method that returns the length
# of a list or other such structure
for i in range(len(self.myList)):
print (self.myList[i])
That said, when iterating in a loop, itâs perhaps simpler to just iterate over the list itself. Something like this:
for someObject in self.myList:
print (someObject)