Is it possible somehow..?
BlitzMax Forums/BlitzMax Programming/Is it possible somehow..?
| ||
Global Suzan:xxx=new xxx Global John:xxx=new xxx Global Ellen:xxx Global Goerge:xxx=Ellen Global Tony:xxx=Ellen Ellen=Suzan 'George and Tony should change.. to Suzan Ellen=John 'George and Tony should change.. to John Type xxx '... End Type Is it possible somehow that when I change Ellen Value, George and Tony will be updated? Im looking for a way to make George and Tony to be the pointers of Ellen.. So when Ellen is changing then those objects will change too.. So lets say if I have 100 Pointers towards Ellen, I wont need to change them one by one. Tnx |
| ||
I'm not sure what you're trying to achieve here... If all objects should always be identical to Ellen, why have all these other objects at all? |
| ||
This is what happens: Your best bet is something like: |
| ||
When George, Suzan, etc are separately declared as variables, they get their own memory allocated to them so they are not affected by anything unless you code them to do so. Suzan = Ellen This only means to copy the CURRENT field values from Ellen to Suzan ONCE, not making it a pointer. But if George, Suzan, etc objects should act like pointers, they should always be exactly equal to Ellen in which case I don't see their purpose in the first place. |
| ||
There is varptr but it only works on non-objects. You'll have to find another solution. You could work some carefully thought out c magic and use a function to assign and unassign the variables. I suggest not to do that though, and bite the bullet to iterate through a collection. |
| ||
My attempt was is to save speed.. I think what I desire is either impossible Or it doesn't make sense. But i had to ask.. I will rethink my options And see what to do Tnx |
| ||
You can use an array or self defined container to hold the reference:Local container:TString[] = ["Foo"] Local a:TString[]=container Local b:TString[]=container a[0]="Bar" DebogLog b[0] 'Bar This pattern is used in Java for anonymous inline functions so they can change the contents of a variable. |
| ||
Similar to Silver_Knee's approach I would throw in: - create a TMap containing: KEY->OBJECT (this ensures, that each key is only used once - compared to a TList) - now just add each of the objects to the map (map.insert("Suzan", Suzan) - and so on) - "Goerge" is then not an object of type "xxx" but becomes a string, containing the key of the referenced object (so Global Goerge:string = "Suzan") - objects are then retrieved via xxx(map.ValueForKey(key)) This way you decouple connections between variables: once you remove the value behind the key "Suzan" from the map, all other variables ("Goerge" and so on) will result in invalid ("null") variables, even if there referenced key is still set to "Suzan". So once you add "Suzan" back, they would result in valid objects again. bye Ron |
| ||
Ok, Great, Tnx guys for the help.. Ill see what I can do with that. |
| ||
Why have George and Tony? They are redundant since they are also Ellen initially? |
| ||
@ Matty I think the intention is, to get a general approach to handle multiple references to an object which auto-update on their own. Think of this: File 1: import "File2.bmx" Import "File3.bmx" 'create cars '.... car1 = null if car2.name = "Car1" then print "object still existing" File 2: Import "car.bmx" Global car1:TCar = new TCar.Init("Car1") File 3 (File 2 does not know what happens in File 3) Import "file1.bmx" global car2:TCar = car1 When now decoupling the real object reference by using eg. a "key->value"-TMap, you just have "car2" being a key with the value "Car1". The real "decoupling" happens, because you then use "Getters" ("GetCar(key)") instead of using the object directly. I think if you imagine a "Sprite"-type having an "image:TImage"-Field, you will understand what I am talking about. What happens, if multiple "Sprites" share the same "image". What happens then, if you adjust the image in one of the sprites. If you want to have all of them keep using the same image (the new one) you cannot do it this way - you either have to wrap the object (the TImage-property) in another container ("TSpriteImage") and adjust the containers properties ("TSpriteImage.SetImage(bla)") or you indirectly reference them by a unique key ("GUID", "ImageURI" or whatever). bye Ron |