At assigning one field to another, does the C# just copy data over, or actually creates link?
In this article there’s an example of game engine structure. The coder there has components contain their parent. In C#, do they just contain the parent copy, or they refer to it?
Example code:
class World
{
...
public void Update()
{
...
ent.OnAttach(this);
...
}
...
}
class Entity
{
...
public void OnAttach(World world)
{
world_ = world;
}
...
}
Could the Entity object now access World object and have access to it’s fields and methods, like in the artice? (or I misunderstood the code?)
Because your data type
Worldis defined as aclassand not astructthat means that when you assign a variable of that type, only a reference to the same data is copied.In other wrods, whether you then use
world.SomeProperty = somethingorworld_.someProperty = somethingthey will be editing the same object in memory.If you change your data type to be a
structthen the entire data structure will be copied and you will have two copies of the same data.Regardless of how you defined your data, once you have a reference to the data you can then access its methods or properties. So, yes, once your Entity object has a reference to the world object it can access any methods or properties on it (as long as they are not private).