My first 100% own script.

Greetings, fellas! I have now entered the realm of programming! For the first time I was able to have a problem, think of a solution and write type a script for it in less than a day! But as most novice inventors, my script is faulty, is like getting a car to run, but forgetting to invent the brakes. What was the problem? I needed to parent various cargo boxes to a vehicle and transport it to another place. The solution ? The simplest way is to use a near sensor with a parent to object with ‘ship’ property. But the cargo boxes are relatively less important objects, so having them have the sensor would not work. So I though of reversing the operation. Have the boxes have an ‘item’ property, and the ship detect them and then parent them… It works fine, till I have to unparent them… Logic on the box is unthinkable, so I am stuck. Is there a way to do it, though one script? Here’s the script:

  import bge  cont = bge.logic.getCurrentController() own = cont.owner  sce = bge.logic.getCurrentScene () near = cont.sensors['near'] out = own["out"]  obj= sce.objects   for cargo in obj:     if  "item" in cargo:                  if near.positive and out==True:             child = near.hitObjectList             for OB in child:                 #par= OB.actuators['Parent']                 OB.setParent(own, False, False)                 if out== False:                     OB.removeParent 

Good work torakunsama! My game doesn’t even have a single actuator - it’s all done in python… once in the realm of python - always in the realm of python…

Thanks HyperReal! Can you help me though?

When I was using my character toss a gun, I just deleted the parented gun and created a new one.
You could also try:

loc = own.getLocation

for obj in child:
obj.removeParent
#place the crates to one side of the ship
obj.location == loc + [however far it is to the dock from the deck of the ship]

I’m still transitioning from python 2.49 to python 2.57, and I wrote this without checking anything so the script wont actually work, but you get the general idea.

If you told us what the boxes were doing when they messed up you might get more help.

Well, the problem is that the boxes stay parented. For me, the script doesn’t detect them since they are now ‘part’ of the ship, so if I think in these terms, I need a way to detect them from the ship and unparent all objects parented to it! Besides, the player will have to physically carry small cargo. Some items are to be deployed on place. So your script would be: loc = own.worldPosition[Axis]#for in this case the boxes will be delivered on the side. for obj in child: obj.removeParent # to be replaced with" for OB in own: OB.removeParent" at least I presume so… … objLocatio = loc+(d) Edit: maybe a for ship.child in sce: do stuff. In other subject, the boxes will be visible, and not necessarily in a specific spot (snaped to specific position), so replacing them will add more logic to them. If I could use logic, I’d use message and remove parent sensor/actuator on the box to unparent them. The player should walk around the ship, while in movement (still working on a script for that) so the boxes should be identifiable, so as for the player to pick one. Or unparent all and pick one of them…

Nice, but the forums obliterated the tabs in the first post. In any case, let me see if I understand your problem.

  1. When boxes get close to the ship, they get parented to it.
  2. Let’s say you want to unparent the boxes on keypress.

Well, when you unparent them, set a timer on the box that you want to unparent, or all of them, to 0. Once the timer is greater than, say, 5, they can be picked up again. All you would have to do is add a if ob[‘timer’] > 5: line to the check before parenting - this will only parent the boxes if they are ‘parentable’. If you press the key, they will unparent, and it should work.

Well, I have 2 conditions for the parenting, a press and property, both activated by key press. The problem is to unparent, when these conditions aren’t met. I can do it if I add a simple logic brick line. The problem with if it is that there will be many objects around asking for logic juice, namely things outside the ship, the players in the ship, the ship controls, weapon, engine systems, etc. The boxes are the last in logical priority. So as far as they have a property and are actors, they have consumed enough. Besides, I want the player to have to freedom to stuff the ship with cargo if he can.

So what exactly is the problem? Is it that once you unparent the boxes, they immediately get reparented because they are so close to the ship?

No. There’s no unparenting. The near sensor is just one of the requirements. When, I set the ship to unparent , it changes state, so the near sensor is not even active anymore. The ‘removeParent’ part isn’t working!

I don’t know if it was cut off in your original code, but it should be

OB.removeParent()

not

OB.removeParent

I tried that, it does not work here’s the blend http://dl.dropbox.com/u/6162142/set%20children.blend

I think I know what your saying. you know how to parent the objects when the conditions are met, but you don’t know how to unparent the object without adding more sensors and expending more logic juice. As you probably know when a sensor shuts down it sends out a negative pulse, so you could try:

if not sense1.positive or not sens2.positive:
unparenting action
if sens1.positive and sense2.positive:
parenting action

I don’t know if actuators work without a controller, but I tried adding an unparent actuator to the boxes and activate them via the code, they still did not comply. I think that the if ‘property’==False: pretty much means the same! I still insist that I must have to access the children in a different way, like through the parent! Something like box=own.children() box.removeParent()…? Why don’t I try it then?