Hi, I am a bit stuck at the moment. The problem I am having is when I refer to multiple instances of the same object (Cube mesh etc). As they all have the same name only one object ‘responds’ to my python commands.
The problem is that both cube’s spawn but only one cube moves when I press the w key. Could anyone help shed some light on what I need to make this work? I want both cubes to move when I press the w key.
The solution was simple really in the end once I got my head around a few concepts. Thanks for the help everyone.
Ran this in a python controller in modular mode, which was new to me ( spawn_characters() and move_characters() (Always pulse on) ).
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
own['spawn'] = scene.addObject("Player",spawnLocation)
wKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.WKEY]
jimmothy = spawney('EmptyTwo')
bruce = spawney('Empty')
Use module mode, don’t run this in script mode.
Store a reference to the original cube, or its python ID (in this case, scene.objects has a from_id function that you can use to get the GameObject by its python ID)
It seems that you’re trying to perform some kind of object management system. You shouldn’t use classes if all they will do is wrap things like that - in this case it will be easier for you to just use functions. However, I think that you might want some more flexibility, so here is an example system.
You are correct in saying I want more flexibility, and that I’m trying to perform some kind of object management system. Often I struggle to realise what I am even trying to do! That code is extracted from part of a character management system, which controls character movement, their inventory and actions as well as spawning them and turning them into rag dolls (spawning rag dolls in their place).
Looking at the blend their are a few things I still need to learn, but you have solved my problem and I’m looking forward to learning more by studying the blend.
I am having a hard time understanding this. Are you reffering to my original post? I am aware that objects are objects and not names, but I am unaware of how to make objects move without reffering to there name… I think…
What I think I need to do (which I don’t think I have made very clear).
Spawn an Object (‘Cube’)
Spawn the same item
Manipulate each (‘Cube’) seperately
Can I cheekily request you embed that code into my original example so I have a full working demo I can learn from?
Could you can suggest some reading, or some posts to read? I am still very new to python.
However I now have a new problem. The object has about 13 children. If I spawn the parent they spawn the children, which is fine. How do I refer to each children? There will be lots of the same children from different parents (“PlayerBounds”).
I think Monster may have tried to steer me to a solution earlier, although I don’t really understand, or know how to implement it.
Thanks BluePrintRandom, that worked for me and I was able to get everything working last night, was up till 5am, but got it all working!
I’m away from my computer with the blends on it, but I will update my original post with an exact solution, although it isn’t neccesarily pretty or good coding practice.
@Monster: I’m unsure what you meant about avoiding indexed names? Do you mean like ‘Object1’, ‘Object2’? In my blend they are named differently, (‘forearmLeftBox’, ‘bicepLeftBox’,‘ChestBox’, ‘forearmRightBox’, etc) however I still think you’re right about needing a different data structure.
I will have to learn to think in KX_GameObjects definately, I’m struggling along with coding I think I have more of a flaky arty fart brain than a coder’s brain, but I’m not giving up or anything.
Anyway thanks for everyone’s help, very much appreciated. I got where I wanted to get. Once I get back to my usual machine I’ll update the original post and mark as solved probably this evening when .
Yes, this is what I mean. I know that your code is just a snippet. You can see in a lot of threads that people use this naming in production code, which makes it pretty dirty. The names you mention make a lot of sense and are perfectly fine :yes:.