Blender Game Engine - How to Copy Game Properties from One Object to Another

BLENDER GAME… PLEASE HELP

I am working on a game, and I am trying to find a python script to copy one (or multiple) game properties from one object to another. For example:
A player has a box (Box A). That box has 5 game properties (integers type) assigned to it. The player presses a button to spawn a different box (Box B), and when Box B is spawned, it assigns the properties from Box A to a Box B, both property names and values. I’m struggling to figure out how to do this with logic bricks, since spawned items technically have different object names…

Any ideas on how to do this?

THANK YOU FOR YOUR HELP

Heya! And welcome!

As far as I know that is not possible. Have you considered doing it with a script?

Thanks! Yeah, I assume it will need to be done with a script, but I wouldn’t know how, as I only understand the logic brick system… any pointers?

Clarification needed. Is Box A also a spawned object? Was it spawned from a copy of the same object that Box B is spawned from? Are they spawned BY the same object? Does Box B replace A(Do you end A or drop it or do something with it?) Will Box A’s properties always be the same or do they change during the course of the game? I’m asking because if they are pre-determined, you can have them set on the object before spawning and don’t even need to mess with python. If this isn’t the case, you have a couple options in scripting.

Depending on your answers, the possible setups could be different. But the main idea will be to get references to both objects before you can copy the properties over. Once you have those references, the copying part is simple:

# Create or overwrite property one at a time
for prop_name in previous_box.getPropertyNames():
    new_box[prop_name] = previous_box[prop_name]

This can be easy or hard depending on your aptitude for coding.

1 Like

Thank you for your response!
Box A is permanent (it never leaves the main scene or is deleted) and it is not a spawned object. Box A’s property values are updated throughout the game, and when Box B is spawned, it will copy over all the property values from Box A to Box B once. Box B does not replace Box A.

The basic premise is that a player has a “crate” (Box A) on their back that “stores” items (in the form of logging property integers). The player can then “drop” that crate on their back, but the game just spawns an identical crate (Box B). So when you “drop” or spawn in this box, the new box copies the properties from Box A to simulate having the inventory of the box that was just on your back (Box A)… make sense? Sorry for the confusion.

it makes sense.

When you ‘drop’ do you press a Keyboard sensor that’s connected to both an Add Object actuator (to spawn Box B) and a Visiblilty actuator (make Box A invisible)?

When you ‘pick up’ are you using a Collision sensor or a Keyboard sensor? If so, is it connected to another Visibility actuator(make Box A visible) and an End Object actuator(delete Box B)?

If this is the case, you can stick a python script between them to call the Add Object actuator and then copy the properties during ‘drop’. And then during ‘pickup’ it will again copy the properties in the other direction. It can also handle visibility and deleting the object if you want it to.

1 Like

Yes, that’s precisely it. To pick up, box B, I am using a collision sensor for a player selector mesh named “player_select” (it’s basically a long invisible mesh that collides with other objects that they can sense to know if the player plans on interacting with them.) and a keyboard sensor (to trigger the pickup). Once the player_select mesh and the key has been pressed, it tells Box A to go visible, and deletes Box B. And conversely, once Box B is spawned, it sends a message to Box A to go invisible.

So you’re saying the code could make it to where the instance box B is spawned, it will instantly copy all the properties from Box A. Got it. So would this code also tell the spawned Block B to send its property data to Block A before before deleting? … Do I understand that correct?

From the previous code snippet you gave me, is it possible to use some sort of designation for the object that is sending the code like “current object” instead of the actual name? Sorry, I don’t know a lot about code yet, I apologize…

Yeah you got it. And yes you can retrieve a reference to the object dynamically so you’re not limited to hardcoded names. Working on a simplified demo for you to have a look at.

So this is just super simple. No error checking or handling but just the stripped down basic mechanics of one way to do it without too much python.

D = Drop box
P = Pick up
Up arrow = add to the properties of the dropped box while it is dropped to show it’s working

During pickup, I know you’ll need to integrate your collision checking but I’ll leave that to you or for another time. Not a whole lot of bricks and not much code. Hopefully it will get you started. I recommend doing some tutorials in python and python in the game engine.drop_pickup.blend (493.1 KB)

1 Like

THANK YOU SO MUCH

I will give this a try and see how it handles.

No problem. Once you start learning python and the BGE API, you will discover many different ways to get the results you’re looking for. There are undoubtedly better ways to do this. Some might suggest a pure python approach and others might be able to this with bricks alone. I’ve never used logic bricks to any extent and only use them when they perform better or for quick tests.

Check out youtube channels for Goran Milovanovic, Wayward Art Company, Super Gloop, SolarLune(not a lot of blender but good game dev stuff), John Hamilton, and of course ThaTimster.

Hello! After a long period of time trying to semi-replicate your example that you made for me, I seem to not be able to crack it… SO! I thought I would reach out to you again and ask if you could check this out for me and see what i’m doing wrong. I’m trying to get box A to copy box B properties upon creation of box B with the space bar… included is an example file. Feel free to look at it/revise it. Use the arrow keys UP and DOWN to change the cube values… In this example, I want the red box to copy the property of the newly created blue box. Thanks! data_copy.blend (645.4 KB)

always check the console for errors if something does not work.

the first error i got from running you blend is

Error: And(Empty), Python error compiling script
File “copy”, line 5
cont = logic.getCurrentController()
^
IndentationError: unexpected indent

Gotcha. I checked it a couple of times while working on it and was getting errors referencing a syntax error. So I thought maybe I had capitalized something wrong or used an incorrect variable or something… I’ll keep fiddling with it.

Not to keep you from solving this with python, since its probably the best solution in the long run, but I believe you can do pretty much what you need by using the Property actuator on Copy mode. It works by copying the value of a property on a selected object and assigning it to a property on the owner (they can have the same name to avoid confusion). By linking it to an always sensor with pulse on, it will be kept up to date with the original object. You can then use property sensors to use those values to activate further logic if you want.