x


Modify Pointers from & to an Object

Hallo, and guten morgen

Can anyone spare how i can modify the pointers reference?

I have looked into System.Reflection and i think i need to go through there, but im not familiar with this NameSpace, and id like a little help.

My summary of events:

I have an object: A ; There is an object B that references object A ; If i exchange object A with another object (object Q), how can i manually modify the references to object A and tell it to point at object Q.

From what i read in the Reflection NameSpace, i believe i can modify pointers manually. Im reasonably new coding (around 1 1/2+ years) , so im hoping someone with more experience can give me an example of this implementation.

In the reflection class there is Pointer, as i understand:

Requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Is Unity the immediate caller or am i (the unity user) the immediate caller? Can i access pointers path directly through Unity, or do i need to make a plug-in to directly mingle with the .Net framework. I want to establish myself as the caller so i may switch the references myself.

What is the best way to make this exchange: -object A- to -object Q- ?

Danke Thankyou!

more ▼

asked Aug 18 '12 at 06:16 AM

OperationDogBird gravatar image

OperationDogBird
1.1k 1 8 15

You need unsafe code which gives you access to 'real' pointers etc. You CAN do this in unity but it it a bit annoying and won't let you build for browser (and possibly any other platform other than PC, I'm not sure). You should not do this unless you are confident with C++ style pointers. It's super easy to f**k you project with pointers and manual memory management - and it can be a dick to find the problem in you code when it relates to pointers.

Really, just tell A and B to be Q or something to that effect.

Sep 01 '12 at 04:56 AM Khada

Hi @Khada , i appreciate your help from before. Since then i just kinda left the project as is. Gonna do some more experimenting right now and let you know.

Sep 01 '12 at 06:50 AM OperationDogBird

@Khada in theory your suggestion would be the simplest, however there is nothing i can swap. Since Q a new gameObject i cannot simply say that B = Q because gameObject is read only.

Sep 01 '12 at 08:16 PM OperationDogBird

Mmm. You may need to get tricky with it. Maybe use a Transform instead of a GameObject to reference Q's transform. Then get the game object via transform.gameObject.

Sep 02 '12 at 11:05 AM Khada

If you explained what you're trying to do that might make it easier to come up with a solution.

Sep 02 '12 at 11:06 AM Khada
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

For some reason it took me a while to understand what's your problem. Here's what I get:

You have a gameobject Master that references a gameobject Slave. You created a script that deletes Slave and replace it by Clone - but Master never hear about this, so it start throwing NullReferenceException everytime it calls something on Slave/Clone. From what I read, you also plan to redistribute the system, which let me think you cannot assume anything about the project.

AFAIK, there's no standard way to handle this. I don't know of a way to trace all references to an object - especially not in a managed environment, in the Unity context...

I don't think Pointer are of any help here - and anywhere really. I might be biased, but my understanding is that managed languages does its best to make sure you never have to do any risky stuff. Let the proven environment handle your garbage and your pointer stuff, also 'unsafe' sounds bad.

Reflection might get you somewhere, but I'm afraid it'll be painful and very inefficient to reflect, loop through, find and hold relevant references from all the projects' classes. I'm not even sure if that would work really, I haven't used it often.

If I was you, I'd first try to modify my script so it never deletes the gameobject in the first place, if the system is a mesh merging one, couldn't it just modify the mesh and renderers, instead?

more ▼

answered Sep 03 '12 at 08:27 AM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

@BY0LOG1C Thanks for the response. I cant keep the original gameObjects because the newly merged objects do not have a mesh renderer or mesh filter, instead i take all that info (all mesh filter info, verts, tris, etc) for use in a skinned renderer so i end up with a single shared mesh for all objects.

I think you are right on track here with the Reflection idea, can you give me some tips on how to loop through Everything in the scene?? Speed isnt an issue so this would be exactly what im looking for! Im very interested in the Reflection idea, i tried looking through that namespace before but i have never used it for anything and couldnt find anything of value on my own. Thanks

Sep 04 '12 at 12:44 AM OperationDogBird

Sorry for the delay, I've been busy lately... Well, I gave the idea some more thought and started pseudo-coding when I realized even Reflection won't help you peek into Unity-serialized data. So... I'm kinda out of working idea, sorry.

Sep 05 '12 at 08:52 PM by0log1c

@by0log1c Thats a bummer, i appreciate you taking the time to try it out for me. I may close this question and start down the road of 'retrieving serialized data'. Thanks again!

Sep 06 '12 at 06:49 AM OperationDogBird
(comments are locked)
10|3000 characters needed characters left

In C#, you should not have to deal with pointers when using Unity unless you are doing something very complicated and out of the box (and even that I do not believe is possible). C# allows for pointers but the code is required to be marked 'unsafe.' Unity restricts unsafe code. I doubt this is what you need though. What is the type of the object you are working with? This matters because most value types will be structs (Vector2, Vector3, Color, etc) which are all handled by value. So when you do an assignment, it doesn't reference the original object; instead it copies the contents over to the new structure. You can get around this in certain instance with methods using (out and ref) but these are only temporary. There use is primarily so you can change more than one return value of a method.

more ▼

answered Aug 31 '12 at 09:19 PM

Sonic 4305 gravatar image

Sonic 4305
1 1 2

Hi, thanks for the response. So this is a summary of my system:

I have a set of scripts that take a group of objects and recreates them as a single shared mesh for a skinned mesh renderer. The problem lies here, if a script on Object A references Object B, the reference to Object B in Object A's script is lost because i recreate and then destroy Object B's gameObject during the merge process.

My thought was to somehow trace everything that was pointing at Object B and replace it with the newly created Object. There are any number of objects being merged and are possibly referenced elsewhere.

Maybe pointers isnt what i need, but im not quite sure what method i should use to accomplish this. Any ideas?

Sep 01 '12 at 05:16 AM OperationDogBird

If you need to reassign to the same script using the same object type, there should be no reason to delete and then create a new object. That requires allocation and deallocation which is MUCH slower than just reassigning the data. Could you not just reassign the member data of object B?

Sep 01 '12 at 06:15 AM Sonic 4305

I am rebuilding the new skinned mesh using the meshes from the provided filters so i dont think so. I tried rebuilding using the same filters, but it didnt work so i had to first make a copy of each gameObject. This isnt done during gameplay so speed isnt a concern during the merging process.

Sep 01 '12 at 06:44 AM OperationDogBird

Well if speed isn't an issue, you could always give the game object a tag and then call "GameObject.FindWithTag()." I'm not sure how you're going to tell the other objects this happened though. You could do that through ordering or with some statics though I don't think this is the best design. If you're the only one on the project it may not be a big deal. Another thing you could do is write a script that only contains the data that needs recreated. So instead of object A holding a reference to object B, it would hold a reference to object C which contained an object of type object B. Then you could do whatever you wanted with object B and not destroy the reference. That make sense?

Sep 01 '12 at 08:24 PM Sonic 4305

So you have one script that holds all of the suspensions and another script that merges all of these suspensions together, correct? You may have to hold a reference to all the scripts that need access to the suspension parts. So in your merge script, you would combine all the suspension objects together and then you would do the reassignment here. If other people need to pick up and use this, you will want to hide a lot of how this works. Look at http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html. Hiding how a body is connected would be about the same as hiding how a car is connected. I get it's not ideal for you but it's ideal for the end user that they have to do as little as possible.

Sep 01 '12 at 11:50 PM Sonic 4305
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x139
x32

asked: Aug 18 '12 at 06:16 AM

Seen: 365 times

Last Updated: Sep 06 '12 at 06:49 AM