Returning an array of strings doesn't work in custom functions
iridiumlynx opened this issue ยท 13 comments
Issue Description:
Returning a string[] doesn't work in custom functions. For some unknown reason an array of strings is treated as a string by return operator, causing 'bad return type' in crafttweaker.
What happens:
string[] is recognised as string
What you expected to happen:
string[] is recognised as string[]
Script used (Please Pastebin or gist your script, posting an unpasted or ungist'd script will automatically close this issue):
crafttweaker.log file (Please Pastebin or gist your file, posting an unpasted or ungist'd file will automatically close this issue):
Affected Versions (Do not use "latest"):
- Minecraft: 1.12.2
- Forge: 1.12.2-14.23.5.2768
- Crafttweaker: 1.12-4.1.14
- Using a server: no
Line 22~25:
if isNull(szString) | isNull(szDelim)
{
return "";
}
You return an empty string, which is not a string[]
for i in "abcabcabc".split("a") {
print(i);
}
Prints:
[INITIALIZATION][CLIENT][INFO]
[INITIALIZATION][CLIENT][INFO] bc
[INITIALIZATION][CLIENT][INFO] bc
[INITIALIZATION][CLIENT][INFO] bc
if I change
if isNull(szString) | isNull(szDelim)
{
return "";
}
to
if isNull(szString) | isNull(szDelim)
{
return [] as string[];
}
that doesn't work.
Yes, because you access invalid array indices.
The array you initialize has a length of 0, so you cannot set any of it's indices the way you do it.
Thanks.
This:
for i in "abcabcabc".split("a") {
print(i);
}
works fine.
Sorry, I just didn't read the log correctly(for approximately two days, maybe I should sleep more and go for a walk).
But split function is undocumented! Should I post this issue to a documentation repository?
Is there a function to parse a string using regular expressions?
And when the documentation will be upgraded?
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
You can use all of the methods, apart from the ones returning, or requiring a parameter of type char
The documentation page https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Variable_Types/Basic_Variables_Functions/ doesn't mention methods with parameters, only parameterless methods.