Quick propertylist question.

It is fairly easy to get an objects propertys with getPropertyNames(), but I was wondering if there was a quick way to copy all properties from one object to another?
EXAMPLE 1:
like object.propertylist = own.propertylist…I have not tried it, but in my head it just copies the names, not the values…

so would I need to walk through the list(increment) and change each value
EXAMPLE 2:
var = 0

object.propertylist[var] = own.propertylist[var]
var += 1

I do realize I would need to check the len/range of the list ‘for i in range len list’(not exactly like that :))

I would love to just copy them all via example 1…would that work, or do I need to go with example 2? both objects have the same properties in the same order.

so getPropertyValues() is a real function??? …I guess not…I gave it a try after seeing it on blender stack exchange…either it is depreciated or just bogus.

in the viewport, hit spacebar and type “copy game property”

inside the game, property is store inside obj.attrDict so you should be able to use update to copy the key and value

untested

obj1.attrDict.update(obj2.attrDict)

sorry, I do not understand how using ‘copy game property’ from outside the engine will make this work via script?

This should work


for prop_name in obj1.getPropertyNames():
    obj2[name] = obj1[name]

getPropertyNames is not deprecated (to my knowledge), so you should be fine in using it.

Python is much nicer than C when it comes to iterating through lists.

I’m finding it quite fun to watch you learn python as I think you already know C? You tend to try do things the long way (python is very expressive and can do complex things with simple code), and you haven’t quite understood namespacing yet (which is fair enough because it doesn’t exist in C at all), and possibly some issues with the OO (object oriented) paradigm, as again, C doesn’t have that concept.

So here’s a tip on the API:

  • bge.types contains the various ‘structs’ that exist in BGE. A struct by itself isn’t useful, but it does store data (and in this case functions as well).
  • the others (bge.render, bge.logic) are functions that can be called directly.

I don’t think I was clear :slight_smile: both objects have identical property names. I was just hoping for a quick method of copying the values between the two objects. :slight_smile: so the properties are not the issue, just copying the values in an efficient manner.

EDIT: for now I will do this the hardcoded way…yay?

side question:
I am trying to find the max index in a list?..or I am not it is generally len(some_list)…but when I access it I get an index out of range error unless I decrement the index by 1…what is the proper way to sort…I think it is list.sort()… I kinda think it is, but I am getting errors that a gameObject is an unorderable type…yay?..I am basically trying to find the first slot in the inventory…if I use list[0] it gives me the last slot…as in the slot with the highest number ‘cell_36’ I am numbering from cell_00 up to cell_36 FYI.

EDIT 2: @sdfgeoff: yes…I am hardwired to do everything differently and I just kinda plunged into the water(python) without knowing how to swim :)…
I don’t mind not knowing…I have programming concepts, but zero python knowledge…and I have never coded any large scale projects…not alone anyway…I am an idiot, but I get by :slight_smile:
and thanks for the link BTW!

I don’t think I was clear both objects have identical property names. I was just hoping for a quick method of copying the values between the two objects. so the properties are not the issue, just copying the values in an efficient manner.

In a python dictionary, creating a new entry is the same as setting it. So the code I posted above is likely the most efficient method. At the very least, its the most efficient obvious method.

As I hinted at, properties are stored in a dictionary-like structure, so you can do:


for prop_name in obj.getPropertyNames():
    print(prop_name, "has value", obj['prop_name']

Regarding the list question: python is zero indexed


a = [1, 2, 3, 4]
len(a) = 4  # see, there are four items. Count'em: 1, 2, 3, 4
a[0]  # But it's zero indexed, so this is the "first item"
a[1]  # second item
a[2]  # third item
a[3]  # fourth item
a[4]  # Oops, there are only four items, and because it's zero indexed, accessing a[4] tries to get the fifth.

You can also index from the end using negative numbers, so:


a = [1, 2, 3, 4]
print(a[-1])  # 4

I just kinda plunged into the water(python) without knowing how to swim …

BGE taught me to code in python. It was my first ever programming beyond a couple lines in BASIC STAMP. It is so hilarious to look back at my old code. And learning through doing is the most fun way of learning!

I have never coded any large scale projects…not alone anyway

I’m not sure many people around here (or anywhere really) have. When you break the 2000 lines of code mark, you’re probably gone further than most other BGE users. Unfortunately this means there are very few people here I can discuss how to manage 10,000 line projects with 20-odd blend files.

I am an idiot

I disagree

attrDict is kind of a weird thing. It only exists to store non-default property types such as lists, custom classes, etc. You still access them normally of course. The documentation is somewhat misleading.

3492 lines! I feel special now.

@pqftgs: grats! we need a you make and eat py award :slight_smile:

HA! :slight_smile:
I think we are all idiots :slight_smile: I do not mean that too seriously though. I also don’t really think I am an idiot…I mean that in good fun…we all make simple mistakes :slight_smile:

I understand that lists are 0 index based…as are all arrays…I assume that is true of any language…that is why my immediate response was to add listIndex[maxLen-1] to find the last item in the list…unfortunately if I use list[0] it returns the last cell…not the first and alternatively if I use the len(list -1) method it returns cell_08…it could be some internal name it is working off of…I’m unsure.
I’m also having some other silly problems I need to work out…for instance when the cells are occupied by an item they have a single property empty[‘True’]# default…
once an item is placed each cell does a simple distance check and if the item is within distance it should switch to empty[‘False’]…
I’m unsure why it s no longer working…it worked before…so now I have to track that, albeit simple, issue down…

@sdgeoff

This should not work: obj2[name] is NOT obj2[prop_name] (because you iterated for prop_name) in …

@JustinBarrett But i dont see the problem with this code, it should copy the values properly


for prop_name in obj1.getPropertyNames():
    obj2[prop_name] = obj1[prop_name]

Hi,

  1. You are right, the max index of a list in a zero-indexed programming (such as python) is the length of that list minus one. Please
    keep in mind not all programming languages are zero-indexed (indexing may start at 1).

  2. When you access your list of cells, you reference list[0]. This references the first element of your list. I’m pretty sure your problem is that your list is not in order - your last cell seems to be the first element. By the way, “the gameObject is unorderable” for your situation means no game object can be less than, equal to, or greater than another game object (such as gameObject1 >= gameObject2).

  3. For reference, you can’t do:


object.getPropertyNames() = own.getPropertyNames()

because you cannot assign a “list literal” (an actual list) to another list. But you can assign a list to a variable.

ok…I do not know a language that is not zero index based…that aside…

this does work, I was just busy with other things to return and post…


temp = len(slot_list)
temp -= 1
icon = ui_scene.addObject('INV_ITEM',slot_list[temp],0)
for prop_name in mouse_over.hitObject.getPropertyNames():
    icon[prop_name] = mouse_over.hitObject[prop_name]
slot_list = []
mouse_over.hitObject.endObject()

it has a dependency on other code clearly, like the mouse over. So, if you say it does not work, I have to tell you…it does. I do not expect people to provide perfect working code…I can figure out that things don’t match up and may need some work :)… This is clearly not all the code…but I am not going to post all of it…I do not feel there is a need :slight_smile:
I can take an object from the game scene copy its properties to a created icon at runtime and place it in the inventory.
One of the property names is a string to the icon it will use and one is a string for the real world object…I can just switch between the two to get from icon to realworld and back…and forth…etc.


for prop_name in whateverobj.getPropertyNames():
    icon[prop_name] = whateverobj[propname]

Error: propname not defined. (Except you defined propname before and that coincidentally is a property in whateverobj, in which case all properties would have the same value)


for prop_name in whateverobj.getPropertyNames():
    icon[prop_name] = whateverobj[prop_name]

i meant that there was a “_” missing in the not working code. the concept works yes.

anyone who would not catch that has no business trying to code. :wink: I just typed it out real fast. I have a video up on my WIP thread from my youtube channel showing it work.

I updated the post to appease you my lord :slight_smile:

Oops, my bad for mistyping. Good catch Leralass

ok…I do not know a language that is not zero index based…that aside…

For instance, here’s a list of languages: https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array)

it has a dependency on other code clearly, like the mouse over. So, if you say it does not work, I have to tell you…it does. I do not expect people to provide perfect working code…I can figure out that things don’t match up and may need some work :)… This is clearly not all the code…but I am not going to post all of it…I do not feel there is a need :slight_smile:

Yes, of course, I understand. I did not say your code does not work, but I was just telling you the options you had in case you wanted further help.