It is just a way of saying it is an index that doesn’t exist or is new. My guess as to why it is -1 and not something like null is because the output type is integer list and integer lists cannot contain null values by default ( List< int > numbers = new List< int >(); ) without being a special type of nullable integer list ( List< int? > numbers = new List< int? >(); ). Or maybe the -1 is entirely intentional for some reason.
besides this, working with nulls is highly controversial in programming. What it usually does not is telling you why its null and when it happend. Null can be also because some other things failed, while -1 clearly indicates its not returning an valid index, sort of…
Please. I have to use graft in my example above since in my application I have a tree as input.
I need to keep the tree structure, so don’t tell me to flatten my input!
This is not the problem but the answer if you want the index you must use flatten.
I think something like that is useful with animation and some cases when you want to know the first object collide
@Michael_Pryor and @TomTom have already told you why. GH uses integers, no nullable-integers. Integers are value types (rather than referenced type) an this means that if there is no number in a integer variable, it will have its default value, for integers is 0. The absence of a integer is 0. For convention in programming, index out of range are returned as -1.
But is the same thing to return -1 or return < empty> or < null> because you need to filter anyway. index >= 0 or IsNullOrEmpty(index) will give you the same information, if true then obstacles[index] collide with collider. And if you don’t need to filter and just use that index to select, you won’t be able to select with an empty/null value. But with a -1 it’s simpler.
Thanks for this detailed explanation Dani, makes sense. I have a hard time when someone just tells me it is what it is. Now I understand. Thanks again!
Martin, You can restructure your tree after the collision; replace paths, or culling the tree statistics,item index maybe others – depending on the data structure.
and just to drive the point home to ensure you it is not a fluke, this is a common condition in many of these test and operation components which output index maps of the results.