Forums › 💬 NodeCanvas › 🗨️ General Discussion › Getter and Setter extension
Hi, i have been using node canvas for some time and im having a very good time with it.
Right now i need to reduce the ammount of nodes to increase the speed of productivity for my team and i need to do some stuff like automatic casting for some custom class types to Vector3 or Transform but the only way to do this right now is by extending the GetGetConverter/GetSetConverter methods from Variable class.
I dont want to modify node canvas classes because the changes will dissapear when updating to the next version (and there are chances that i wont be the one that will be updating it).
So my questions are:
– Is there any other way of doing this?
– Can we get this feature on the future?
– Can i help implement it?
Greetings!
Hello.
Sorry for the late reply due to summer vacation and thank you. I am glad you enjoy NodeCanvas.
Unfortunately right now there is no easy way to implement custom “auto conversions”. One solution to make this more modular that I am thinking, is to make the Variable class a partial class along with a partial method which one can implement to provide further conversions.
Let me know what you think.
Thanks!
Hey, thanks for the reply!
Making Variable a partial class sounds like a good idea for something that specific, sadly partial methods must return a void so some additional work will be needed.
Hey,
You are welcome 🙂
That is true. I have just added this in the roadmap and will see how it can best be implemented. Making the class partial is one possible solution, but I might come up with something better 🙂
Thanks again for the suggestion!
Good, im looking forward to it. Thanks for the support 🙂
HeyGavalakis,
I need getter/setter for my custom types so I implement a simple way to add it.
Add this lines at the beginning of TypeConverter.cs:
|
1 2 |
public delegate Func<object, object> CustomConverter(Type fromType, Type toType); public static CustomConverter customConverter; |
And in public static Func<object, object> Get(Type fromType, Type toType) method:
|
1 2 3 4 5 6 |
if (customConverter != null){ Func<object, object> converter = customConverter(fromType, toType); if (converter != null){ return converter; } } |
Now, in your custom classes you can add this to auto convert your custom types:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[InitializeOnLoadMethod] [RuntimeInitializeOnLoadMethod] public static void InitConverter() { ParadoxNotion.TypeConverter.customConverter += Converter; } private static Func<object, object> Converter(Type _fromType, Type _toType) { if (_fromType == typeof(MyCustomType)) { if (_toType == typeof(Vector3)) { return (value) => { MyCustomType myType = (MyCustomType) value; return myType!=null?myType.position:Vector3.zero; }; } //... } else if (_toType == typeof(MyCustomType)) { if (_fromType == typeof(Vector3)) { //Warning: This can generate garbage if MyCustomType is a class return (value) => new MyCustomType((Vector3)value); } //... } return null; } |
Sounds good to you ?
Hey Psykaw,
This is definetely a good solution 🙂
I’ve just added this in for the next version.
Thanks!
Hi kinerius, are you using revision control like Git? If so, you could make a separate branch of your code that has the unaltered NodeCanvas. Every time an official update is released, you update this branch and then merge it to your master branch. This way your changes and the official changes can be merged. Sometimes it’s a bit hard to get everything right though if many files get moved around between versions, but in general I’m quite happy with this approach.
