Skip to content

Commit 2ee5d28

Browse files
committed
Merge pull request #77164 from dalexeev/gds-re-add-ord
GDScript: Re-add `ord()` function
2 parents ed98e28 + f864d0c commit 2ee5d28

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

core/variant/variant_call.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ static void _register_variant_builtin_methods_string() {
18961896
bind_static_method(String, num, sarray("number", "decimals"), varray(-1));
18971897
bind_static_method(String, num_int64, sarray("number", "base", "capitalize_hex"), varray(10, false));
18981898
bind_static_method(String, num_uint64, sarray("number", "base", "capitalize_hex"), varray(10, false));
1899-
bind_static_method(String, chr, sarray("char"), varray());
1899+
bind_static_method(String, chr, sarray("code"), varray());
19001900
bind_static_method(String, humanize_size, sarray("size"), varray());
19011901

19021902
/* StringName */

doc/classes/String.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@
118118
</method>
119119
<method name="chr" qualifiers="static">
120120
<return type="String" />
121-
<param index="0" name="char" type="int" />
121+
<param index="0" name="code" type="int" />
122122
<description>
123-
Returns a single Unicode character from the decimal [param char]. You may use [url=https://unicodelookup.com/]unicodelookup.com[/url] or [url=https://www.unicode.org/charts/]unicode.org[/url] as points of reference.
123+
Returns a single Unicode character from the integer [param code]. You may use [url=https://unicodelookup.com/]unicodelookup.com[/url] or [url=https://www.unicode.org/charts/]unicode.org[/url] as points of reference.
124124
[codeblock]
125125
print(String.chr(65)) # Prints "A"
126126
print(String.chr(129302)) # Prints "🤖" (robot face emoji)
127127
[/codeblock]
128+
See also [method unicode_at], [method @GDScript.char], and [method @GDScript.ord].
128129
</description>
129130
</method>
130131
<method name="contains" qualifiers="const">
@@ -1149,6 +1150,7 @@
11491150
<param index="0" name="at" type="int" />
11501151
<description>
11511152
Returns the character code at position [param at].
1153+
See also [method chr], [method @GDScript.char], and [method @GDScript.ord].
11521154
</description>
11531155
</method>
11541156
<method name="uri_decode" qualifiers="const">

doc/classes/StringName.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@
10571057
<param index="0" name="at" type="int" />
10581058
<description>
10591059
Returns the character code at position [param at].
1060+
See also [method String.chr], [method @GDScript.char], and [method @GDScript.ord].
10601061
</description>
10611062
</method>
10621063
<method name="uri_decode" qualifiers="const">

modules/gdscript/doc_classes/@GDScript.xml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@
4848
</method>
4949
<method name="char">
5050
<return type="String" />
51-
<param index="0" name="char" type="int" />
51+
<param index="0" name="code" type="int" />
5252
<description>
53-
Returns a single character (as a [String]) of the given Unicode code point (which is compatible with ASCII code).
53+
Returns a single character (as a [String] of length 1) of the given Unicode code point [param code].
5454
[codeblock]
55-
var upper = char(65) # upper is "A"
56-
var lower = char(65 + 32) # lower is "a"
57-
var euro = char(8364) # euro is "€"
55+
print(char(65)) # Prints "A"
56+
print(char(129302)) # Prints "🤖" (robot face emoji)
5857
[/codeblock]
58+
This is the inverse of [method ord]. See also [method String.chr] and [method String.unicode_at].
5959
</description>
6060
</method>
6161
<method name="convert" deprecated="Use [method @GlobalScope.type_convert] instead.">
@@ -175,6 +175,18 @@
175175
[b]Note:[/b] If [member ProjectSettings.editor/export/convert_text_resources_to_binary] is [code]true[/code], [method @GDScript.load] will not be able to read converted files in an exported project. If you rely on run-time loading of files present within the PCK, set [member ProjectSettings.editor/export/convert_text_resources_to_binary] to [code]false[/code].
176176
</description>
177177
</method>
178+
<method name="ord">
179+
<return type="int" />
180+
<param index="0" name="char" type="String" />
181+
<description>
182+
Returns an integer representing the Unicode code point of the given character [param char], which should be a string of length 1.
183+
[codeblock]
184+
print(ord("A")) # Prints 65
185+
print(ord("🤖")) # Prints 129302
186+
[/codeblock]
187+
This is the inverse of [method char]. See also [method String.chr] and [method String.unicode_at].
188+
</description>
189+
</method>
178190
<method name="preload">
179191
<return type="Resource" />
180192
<param index="0" name="path" type="String" />

modules/gdscript/gdscript_utility_functions.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,17 @@ struct GDScriptUtilityFunctionsDefinitions {
120120
static inline void _char(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
121121
DEBUG_VALIDATE_ARG_COUNT(1, 1);
122122
DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
123-
char32_t result[2] = { *p_args[0], 0 };
124-
*r_ret = String(result);
123+
const int64_t code = *p_args[0];
124+
VALIDATE_ARG_CUSTOM(0, Variant::INT, code < 0 || code > UINT32_MAX, RTR("Expected an integer between 0 and 2^32 - 1."));
125+
*r_ret = String::chr(code);
126+
}
127+
128+
static inline void ord(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
129+
DEBUG_VALIDATE_ARG_COUNT(1, 1);
130+
DEBUG_VALIDATE_ARG_TYPE(0, Variant::STRING);
131+
const String string = *p_args[0];
132+
VALIDATE_ARG_CUSTOM(0, Variant::STRING, string.length() != 1, RTR("Expected a string of length 1 (a character)."));
133+
*r_ret = string.get(0);
125134
}
126135

127136
static inline void range(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
@@ -575,7 +584,8 @@ void GDScriptUtilityFunctions::register_functions() {
575584
REGISTER_FUNC( convert, true, RETVAR, ARGS( ARGVAR("what"), ARGTYPE("type") ), false, varray( ));
576585
#endif // DISABLE_DEPRECATED
577586
REGISTER_FUNC( type_exists, true, RET(BOOL), ARGS( ARG("type", STRING_NAME) ), false, varray( ));
578-
REGISTER_FUNC( _char, true, RET(STRING), ARGS( ARG("char", INT) ), false, varray( ));
587+
REGISTER_FUNC( _char, true, RET(STRING), ARGS( ARG("code", INT) ), false, varray( ));
588+
REGISTER_FUNC( ord, true, RET(INT), ARGS( ARG("char", STRING) ), false, varray( ));
579589
REGISTER_FUNC( range, false, RET(ARRAY), NOARGS, true, varray( ));
580590
REGISTER_FUNC( load, false, RETCLS("Resource"), ARGS( ARG("path", STRING) ), false, varray( ));
581591
#ifndef DISABLE_DEPRECATED

0 commit comments

Comments
 (0)