Assigning rotation to added objecd B from added object A not working

Hello!

In my scene i got a place where one object is added (Object A), then this object is addin an other object (Object B)
upon adding Object B is getting an prop (string) whit the name of Object A, This prop is used so that Object B remember’s the name of it’s creator, and can tryck to it - this part works fine

Then when Object B is getting close to Object A, Object B is trying to get the worldOrientation from Object A, this is the part where the strange things begin to happend… Getting the worldOrientation works… some times, if I got multiply object A and multiply object B’s they all lose the object that they are supose to get the Orientations from and start to get the Orientation from the same object, not their own creator as they are supose to…

This is the code that is running on every Object B:


import bge
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner
Parent = scene.objects[str(own["parent"])]
distance = own.getDistanceTo(own["parent"])
if not own["alowmove"] == 0:
    if distance > 0.4:
        own["alowmove"] = 1
    else:
        own.worldOrientation = Parent.worldOrientation
        own["alowmove"] = 0
else:
    own["alowmove"] = 2

A summary of my question wold be, “why is Object B losing track of the object to take it’s orientation from, but not the object to track and get distance to?”

Well theres a couple things I notice right off the bat:

  1. there is no variable ‘own’ until after you setup Parent. You cannot reference own before it exists.

  2. You do not need to reference objects by name every time you need to access them, and this code does it both ways anyhow. If own[‘parent’] references an object, just use own[‘parent’], there is no need to do all the scene.objects[str(own[‘parent’]) stuff. If that object is dynamically created, trying to reference it by name probably wont work how you expect it to.

here, try this:



import bge


cont = bge.logic.getCurrentController()
own = cont.owner
Parent = own['parent']


distance = own.getDistanceTo(Parent)
if not own["alowmove"] == 0:
    if distance > 0.4:
        own["alowmove"] = 1
    else:
        own.worldOrientation = Parent.worldOrientation
        own["alowmove"] = 0
else:
    own["alowmove"] = 2

If you aren’t already, I suggest you turn on the console window when you run the game. As you pasted it, this script should not run at all due to the reference to ‘own’ before you initialized it. If the console is open, you can see simple errors like this.

This probably won’t completely fix your problem, but it should clear things up a little.

Ohh, sry for the “reference own before it exists” error, it came there when i did this post, it isen’t there in the code.

corrected the first post.

thx for the reply Hobomatic, will try your sugestion and only assign the value once

ok problem solved

I where using scene.objects[str(own[“parent”])] as I got some error when only using scene.objects[own[‘parent’]]

Can’t see why I did assign the value multiply times at the first place, I feel rly stupid…

anyway thx for the help :slight_smile: