BehaviorTree.CPP
BehaviorTree.CPP copied to clipboard
Add support to concat strings and numbers in scripting
Solves #766
The potential issue I see with this is that there are two contraddicting use case:
- Someone may want to concatenate string with numbers, so for instance
"path_" + 4 -> "path_4"
- Someone may want strings to be interpreted as numbers
"3" + 4 -> 7
What would be probably "wrong" is:
"3" + 4 -> "37"
So, the only sane way to do this, in my opinion is including functions to_number()
and to_string()
:
"path" + to_string(4) -> "path_4"
to_number("3") + 4 -> 37
This is considerably more work, but the only way to avoid technical debt from being created, in my opinion.
The potential issue I see with this is that there are two contraddicting use case:
- Someone may want to concatenate string with numbers, so for instance
"path_" + 4 -> "path_4"
- Someone may want strings to be interpreted as numbers
"3" + 4 -> 7
What would be probably "wrong" is:
"3" + 4 -> "37"
So, the only sane way to do this, in my opinion is including functions
to_number()
andto_string()
:"path" + to_string(4) -> "path_4" to_number("3") + 4 -> 37
This is considerably more work, but the only way to avoid technical debt from being created, in my opinion.
It would be nice, but I guess quite a large amount of work to make it work properly, as your outlined approach also has some potential pitfalls, i.e. "3,14" vs "3.14" vs "8'000". However, in the meantime, this could still be a good enough intermediate solution as it does at least allow for the usecase 1 to be solved.
However, in the meantime, this could still be a good enough intermediate solution as it does at least allow for the usecase 1 to be solved.
Sorry, not adding this feature yet. I can't afford to have side effects that haven't been considered carefully
Would a better solution for this be: to add a new operator specifically for string concatenation? Similar to how it is done in Lua. e.g.
value := 10
message := "The value is " .. value
would result in The value is 10
I can see this avoiding any issues with people wanting to use strings converted to numbers as it is explicitly for string concatenation.
EDIT: I've made a new PR #802 that works for my needs but can add some changes if required.
closing in favor of https://github.com/BehaviorTree/BehaviorTree.CPP/pull/802