I am just learning and my code is unelegant and clunky- but this worked for me and I had a hard time finding examples of how to do this in noob english.
Basically I wanted to take my character and run over a item- have that item vanish then update my inventory. Now in this picture I am just using a texture of a card that will be used later in the game- because I do not have any gem models and plus these were handy.
if look below you will see that the overall mana count increased, and the watergem count increased and that the card or item ralph touched is now gone. This can be applied to any type of item management in the game not just for mana.
here is what I did:
(I used roaming ralph as base)
I switched the enviroment map out for a flat one so my random items didn’t end up in a hill or something. You can change the map out or try to use the existing one- you will just need to adjust the items z-height to match the terrain.
Next I created a small array to create two items: watergem (self.mana) and deathgem(self.mana). BY the way all this code went inside of the
class World(DirectObject): def __init__(self):
self.mana= for xyz in range(2): self.mana.append(loader.loadModel("models/plane")) #<<<< change this to your own model- iused a plane (flat card) to just hold a card graphic that I just had laying in my game file. myTexture = loader.loadTexture("cards/ac1.jpg")#<<<again change the graphic to your own self.mana[xyz].setTexture(myTexture) self.mana[xyz].reparentTo(render) self.mana[xyz].setPos(self.ralph.getX()+10+random.randint(10, 30) ,self.ralph.getY()+10, 0) #<<< this places the cards somewhat close to ralph. self.mana.setTag('type', "watergem") #<<<here I setup my tags to identify the item later self.mana.setTag('type', "deathgem")
next I needed a collision:
But I was already using collision ray on the ground and I was not liking the effects of using a collision solids (I kept getting weird ray problems) So I switched to just measuring the distance and acting out the collision:
This code went in the collission movement task:
# Also deals with grid checking and collision detection def move(self, task):
ii=0 for i in self.mana: lenVector = self.ralph.getPos() - self.mana[ii].getPos() #<<this measures the distance between my actor and the items if lenVector.length()<2: #<< if this item is very close then perform collision if self.mana[ii].getTag('type')=="watergem": #<< check to see what type of item it is self.watergems+=1 self.textObject2.setText(str(self.watergems)) #<< update the online gui text if self.mana[ii].getTag('type')=="deathgem": #same thing again for the second item self.deathgems+=1 self.textObject1.setText(str(self.deathgems)) self.mana[ii].removeNode() del self.mana[ii] self.textObject7.setText(str(self.firegems + self.windgems + self.deathgems+ self.watergems + self.earthgems)) #<< update my total mana and display on gui ii+=1 #<< increments the count
You will need to create textOjects to display the texts of course. But what happens is if roaming ralph touches an item the item vanishes and the inventory variable is increased by one then displayed on a textObject.
Forgive my poor writing and worse good, I hope this helps someone.