Having problems with scene.addObject

I am trying to get a bullet to spawn at an empty, but I am am running into some problems: 1) The object, when it spawns, is smaller than it is in the layer I put it in.
2) Also, it doesn’t seem to always spawn at the empty that I am telling it to.

Here is what I am doing:
I have an empty that is parented to a player object. This player object has a keyboard sensor with true level triggering enabled that is set to all keys. This keyboard sensor is set to a python controller that is running this script:


 
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]:
      scene.addObject('bullet', 'attack_spawn', 120)

As I said, the object spawns, but it never spawns correctly. If there is anything I can make clearer, please let me know (As I’m typing this, I feel that it makes very little sense, but that may just be how tired I am right now). Thank you for your help! :smiley:

Your object is probably scaled up rather than the mesh itself.
First get the object and do Alt - S at the same time,
then go into edit mode and scale it that way.

Hope that helps.

  1. Ensure that the spawning object has its scale set to 1, 1, 1. To do this, apply its scale in Object Mode with Ctrl + A. If you can’t afford to apply its scale, then you can fix the scale of the bullet after spawning, but try the suggested solution first.

  2. You should provide a pointer to the spawning object, not just the name. Use sce.objects to get the pointer, or if you want a particular spawning empty each time, then run the script on the empty and provide the object as the pointer, or run the script on a parent of the empty, and get the empty as its child:

An example of running the script on the parent.



from bge import logic

cont = logic.getCurrentController()
obj = cont.owner
sce = logic.getCurrentScene()

attackspawn = [c for c in obj.childrenRecursive if c.name == 'attack_spawn'][0]

if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]:       
     sce.addObject('bullet', attackspawn, 120)


@DarkFire963:
I have tried your method, but I am still coming up with the same problems. I am utterly baffled by this.

@SolarLune:
You were right, the bullet wasn’t at a scale of 1, 1, 1. I have fixed this, but the object’s shape is still distorted when it is spawned. For your 2nd point, I tried making the changes you suggested, and still nothing. However, I didn’t really understand your directions (more likely my fault than yours), so all I did was add in that line of code (changing it to fit with my naming of things). What did you mean by a pointer?

  1. Not the bullet, but the gun (or empty) that’s spawning the bullet should have a scale of 1, 1, 1. So, apply scaling on the spawning object and the bullet, not just on the bullet alone.

  2. Maybe I used the wrong word - reference is more accurate. If you pass a reference to the specific object that you want to sce.addObject(), it should add the object specified using that specific object reference. If you just pass a string, then I can only guess that it’ll just find whichever object is first with that name, and then spawn it there.

Instead of the string ‘attack_spawn’, get the actual object and use the object’s reference for the sce.addObject function.

@SolarLune:

Ahhhh, now I understand. That makes a lot of sense. Now things are working perfectly! Thank you so much SolarLune for your help. This bullet spawning code is one that I use very often, and now that you have improved it, I know I can rely on its functionality in the future!