How to expose mutable list as a readonly property in a MonoBehaviour
up vote
0
down vote
favorite
Let's say I have something like this
public class BodyPart : MonoBehaviour {
private BodyPart parent;
private List<BodyPart> children;
}
and I want to be able to maintain both links when adding/removing children and changing parents. Ideally I would just create properties, and ensure parent gets updated when I add children and vice versa. But exposing list as a property (even a readonly property) - allows me to do
bodyPart.Children.Add(new BodyPart());
which won't call the setter, so parent field won't be updated properly.
I've seen people exposing List<...> as a property of type ReadOnlyCollection<...>, but I'm not sure if unity component system will work with such a property, and maybe there is some better way? Also - maybe IReadOnlyCollection is better?
I know I could do a hierarchy of GameObjects instead, and use the builtin parent-child relationship of unity, but I don't want my BodyParts to be full-blown GameObjects with transforms and everything, it's just a model for RPG attributes and inventory system. Also the example is simplified and ultimately I want to have more logic and several kinds of relationships between Components encoded in that model.
c# unity3d entity-component-system
add a comment |
up vote
0
down vote
favorite
Let's say I have something like this
public class BodyPart : MonoBehaviour {
private BodyPart parent;
private List<BodyPart> children;
}
and I want to be able to maintain both links when adding/removing children and changing parents. Ideally I would just create properties, and ensure parent gets updated when I add children and vice versa. But exposing list as a property (even a readonly property) - allows me to do
bodyPart.Children.Add(new BodyPart());
which won't call the setter, so parent field won't be updated properly.
I've seen people exposing List<...> as a property of type ReadOnlyCollection<...>, but I'm not sure if unity component system will work with such a property, and maybe there is some better way? Also - maybe IReadOnlyCollection is better?
I know I could do a hierarchy of GameObjects instead, and use the builtin parent-child relationship of unity, but I don't want my BodyParts to be full-blown GameObjects with transforms and everything, it's just a model for RPG attributes and inventory system. Also the example is simplified and ultimately I want to have more logic and several kinds of relationships between Components encoded in that model.
c# unity3d entity-component-system
1
I don't know if Unity3D will eat this, but you could implement your own (children) list type based on theIList<T>
interface. Such a list would then simply keep your "normal"List<BodyPart>
as a private object inside, and the respective methods of your custom list type (such as Add, Insert, Remove, the indexer setter, etc...) would not only manipulate the internal body parts list accordingly, but also manipulate the Parent property of the BodyPart objects added/removed to this list. This way you can expose it safely while keeping it mutable...
– elgonzo
Nov 10 at 19:41
1
Something along the lines ofpublic class ChildrenList : IList<BodyPart> { private List<BodyPart> _bodyPartList; ... ... interface implementation ... ... }
– elgonzo
Nov 10 at 19:42
1
List<T>.AsReadOnly()
– Draco18s
Nov 10 at 20:22
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Let's say I have something like this
public class BodyPart : MonoBehaviour {
private BodyPart parent;
private List<BodyPart> children;
}
and I want to be able to maintain both links when adding/removing children and changing parents. Ideally I would just create properties, and ensure parent gets updated when I add children and vice versa. But exposing list as a property (even a readonly property) - allows me to do
bodyPart.Children.Add(new BodyPart());
which won't call the setter, so parent field won't be updated properly.
I've seen people exposing List<...> as a property of type ReadOnlyCollection<...>, but I'm not sure if unity component system will work with such a property, and maybe there is some better way? Also - maybe IReadOnlyCollection is better?
I know I could do a hierarchy of GameObjects instead, and use the builtin parent-child relationship of unity, but I don't want my BodyParts to be full-blown GameObjects with transforms and everything, it's just a model for RPG attributes and inventory system. Also the example is simplified and ultimately I want to have more logic and several kinds of relationships between Components encoded in that model.
c# unity3d entity-component-system
Let's say I have something like this
public class BodyPart : MonoBehaviour {
private BodyPart parent;
private List<BodyPart> children;
}
and I want to be able to maintain both links when adding/removing children and changing parents. Ideally I would just create properties, and ensure parent gets updated when I add children and vice versa. But exposing list as a property (even a readonly property) - allows me to do
bodyPart.Children.Add(new BodyPart());
which won't call the setter, so parent field won't be updated properly.
I've seen people exposing List<...> as a property of type ReadOnlyCollection<...>, but I'm not sure if unity component system will work with such a property, and maybe there is some better way? Also - maybe IReadOnlyCollection is better?
I know I could do a hierarchy of GameObjects instead, and use the builtin parent-child relationship of unity, but I don't want my BodyParts to be full-blown GameObjects with transforms and everything, it's just a model for RPG attributes and inventory system. Also the example is simplified and ultimately I want to have more logic and several kinds of relationships between Components encoded in that model.
c# unity3d entity-component-system
c# unity3d entity-component-system
edited Nov 10 at 19:14
asked Nov 10 at 19:01
ajuc
482411
482411
1
I don't know if Unity3D will eat this, but you could implement your own (children) list type based on theIList<T>
interface. Such a list would then simply keep your "normal"List<BodyPart>
as a private object inside, and the respective methods of your custom list type (such as Add, Insert, Remove, the indexer setter, etc...) would not only manipulate the internal body parts list accordingly, but also manipulate the Parent property of the BodyPart objects added/removed to this list. This way you can expose it safely while keeping it mutable...
– elgonzo
Nov 10 at 19:41
1
Something along the lines ofpublic class ChildrenList : IList<BodyPart> { private List<BodyPart> _bodyPartList; ... ... interface implementation ... ... }
– elgonzo
Nov 10 at 19:42
1
List<T>.AsReadOnly()
– Draco18s
Nov 10 at 20:22
add a comment |
1
I don't know if Unity3D will eat this, but you could implement your own (children) list type based on theIList<T>
interface. Such a list would then simply keep your "normal"List<BodyPart>
as a private object inside, and the respective methods of your custom list type (such as Add, Insert, Remove, the indexer setter, etc...) would not only manipulate the internal body parts list accordingly, but also manipulate the Parent property of the BodyPart objects added/removed to this list. This way you can expose it safely while keeping it mutable...
– elgonzo
Nov 10 at 19:41
1
Something along the lines ofpublic class ChildrenList : IList<BodyPart> { private List<BodyPart> _bodyPartList; ... ... interface implementation ... ... }
– elgonzo
Nov 10 at 19:42
1
List<T>.AsReadOnly()
– Draco18s
Nov 10 at 20:22
1
1
I don't know if Unity3D will eat this, but you could implement your own (children) list type based on the
IList<T>
interface. Such a list would then simply keep your "normal" List<BodyPart>
as a private object inside, and the respective methods of your custom list type (such as Add, Insert, Remove, the indexer setter, etc...) would not only manipulate the internal body parts list accordingly, but also manipulate the Parent property of the BodyPart objects added/removed to this list. This way you can expose it safely while keeping it mutable...– elgonzo
Nov 10 at 19:41
I don't know if Unity3D will eat this, but you could implement your own (children) list type based on the
IList<T>
interface. Such a list would then simply keep your "normal" List<BodyPart>
as a private object inside, and the respective methods of your custom list type (such as Add, Insert, Remove, the indexer setter, etc...) would not only manipulate the internal body parts list accordingly, but also manipulate the Parent property of the BodyPart objects added/removed to this list. This way you can expose it safely while keeping it mutable...– elgonzo
Nov 10 at 19:41
1
1
Something along the lines of
public class ChildrenList : IList<BodyPart> { private List<BodyPart> _bodyPartList; ... ... interface implementation ... ... }
– elgonzo
Nov 10 at 19:42
Something along the lines of
public class ChildrenList : IList<BodyPart> { private List<BodyPart> _bodyPartList; ... ... interface implementation ... ... }
– elgonzo
Nov 10 at 19:42
1
1
List<T>.AsReadOnly()
– Draco18s
Nov 10 at 20:22
List<T>.AsReadOnly()
– Draco18s
Nov 10 at 20:22
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242406%2fhow-to-expose-mutable-list-as-a-readonly-property-in-a-monobehaviour%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I don't know if Unity3D will eat this, but you could implement your own (children) list type based on the
IList<T>
interface. Such a list would then simply keep your "normal"List<BodyPart>
as a private object inside, and the respective methods of your custom list type (such as Add, Insert, Remove, the indexer setter, etc...) would not only manipulate the internal body parts list accordingly, but also manipulate the Parent property of the BodyPart objects added/removed to this list. This way you can expose it safely while keeping it mutable...– elgonzo
Nov 10 at 19:41
1
Something along the lines of
public class ChildrenList : IList<BodyPart> { private List<BodyPart> _bodyPartList; ... ... interface implementation ... ... }
– elgonzo
Nov 10 at 19:42
1
List<T>.AsReadOnly()
– Draco18s
Nov 10 at 20:22