Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Port Code Examples to C# (R, S, T, U) #43929

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions doc/classes/RenderingServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2704,11 +2704,14 @@
<description>
Copies the viewport to a region of the screen specified by [code]rect[/code]. If [method viewport_set_render_direct_to_screen] is [code]true[/code], then the viewport does not use a framebuffer and the contents of the viewport are rendered directly to screen. However, note that the root viewport is drawn last, therefore it will draw over the screen. Accordingly, you must set the root viewport to an area that does not cover the area that you have attached this viewport to.
For example, you can set the root viewport to not render at all with the following code:
[codeblock]
FIXME: The method seems to be non-existent.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this method doesn't exist. It exists in 3.2 though. Was it renamed, or removed? @akien-mga

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@HaSa1002 HaSa1002 Nov 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like the whole description is not up to date since the window split. Maybe I can dig up some infos how it is done now or if it is even valid today.

Copy link
Contributor Author

@HaSa1002 HaSa1002 Nov 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I almost wasted 1,5 hours into that. I feel like this is not supported anymore. There has to be a reason it was removed from viewport. I guess the RenderingServerViewport was not updated with this change. (It even is guarded as VisualServer). I tried

func _ready():
	RenderingServer.viewport_attach_to_screen(get_viewport_rid(), Rect2(0,0,600,600))

as indirectly suggested in this description and it does not work. I guess this is the reason, why it was removed in the linked commit. Someone with a deeper understanding of the RenderingServer might be required to update the docs at this point.

[codeblocks]
[gdscript]
func _ready():
get_viewport().set_attach_to_screen_rect(Rect2())
$Viewport.set_attach_to_screen_rect(Rect2(0, 0, 600, 600))
[/codeblock]
[/gdscript]
[/codeblocks]
Using this can result in significant optimization, especially on lower-end devices. However, it comes at the cost of having to manage your viewports manually. For a further optimization see, [method viewport_set_render_direct_to_screen].
</description>
</method>
Expand Down
10 changes: 8 additions & 2 deletions doc/classes/RichTextEffect.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
<description>
A custom effect for use with [RichTextLabel].
[b]Note:[/b] For a [RichTextEffect] to be usable, a BBCode tag must be defined as a member variable called [code]bbcode[/code] in the script.
[codeblock]
[codeblocks]
[gdscript]
# The RichTextEffect will be usable like this: `[example]Some text[/example]`
var bbcode = "example"
[/codeblock]
[/gdscript]
[csharp]
// The RichTextEffect will be usable like this: `[example]Some text[/example]`
public string bbcode = "example";
[/csharp]
[/codeblocks]
[b]Note:[/b] As soon as a [RichTextLabel] contains at least one [RichTextEffect], it will continuously process the effect unless the project is paused. This may impact battery life negatively.
</description>
<tutorials>
Expand Down
14 changes: 12 additions & 2 deletions doc/classes/SceneTree.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,22 @@
<description>
Returns a [SceneTreeTimer] which will [signal SceneTreeTimer.timeout] after the given time in seconds elapsed in this [SceneTree]. If [code]process_always[/code] is set to [code]false[/code], pausing the [SceneTree] will also pause the timer.
Commonly used to create a one-shot delay timer as in the following example:
[codeblock]
[codeblocks]
[gdscript]
func some_function():
print("start")
yield(get_tree().create_timer(1.0), "timeout")
print("end")
[/codeblock]
[/gdscript]
[csharp]
public async void SomeFunction()
{
GD.Print("start");
await ToSignal(GetTree().CreateTimer(1.0f), "timeout");
GD.Print("end");
}
[/csharp]
[/codeblocks]
The timer will be automatically freed after its time elapses.
</description>
</method>
Expand Down
14 changes: 12 additions & 2 deletions doc/classes/SceneTreeTimer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@
<description>
A one-shot timer managed by the scene tree, which emits [signal timeout] on completion. See also [method SceneTree.create_timer].
As opposed to [Timer], it does not require the instantiation of a node. Commonly used to create a one-shot delay timer as in the following example:
[codeblock]
[codeblocks]
[gdscript]
func some_function():
print("Timer started.")
yield(get_tree().create_timer(1.0), "timeout")
print("Timer ended.")
[/codeblock]
[/gdscript]
[csharp]
public async void SomeFunction()
{
GD.Print("Timer started.");
await ToSignal(GetTree().CreateTimer(1.0f), "timeout");
GD.Print("Timer ended.");
}
[/csharp]
[/codeblocks]
</description>
<tutorials>
</tutorials>
Expand Down
20 changes: 16 additions & 4 deletions doc/classes/ScriptCreateDialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
</brief_description>
<description>
The [ScriptCreateDialog] creates script files according to a given template for a given scripting language. The standard use is to configure its fields prior to calling one of the [method Window.popup] methods.
[codeblock]
[codeblocks]
[gdscript]
func _ready():
dialog.config("Node", "res://new_node.gd") # For in-engine types
dialog.config("\"res://base_node.gd\"", "res://derived_node.gd") # For script types
var dialog = ScriptCreateDialog.new();
dialog.config("Node", "res://new_node.gd") # For in-engine types.
dialog.config("\"res://base_node.gd\"", "res://derived_node.gd") # For script types.
dialog.popup_centered()
[/codeblock]
[/gdscript]
[csharp]
public override void _Ready()
{
var dialog = new ScriptCreateDialog();
dialog.Config("Node", "res://NewNode.cs"); // For in-engine types.
dialog.Config("\"res://BaseNode.cs\"", "res://DerivedNode.cs"); // For script types.
dialog.PopupCentered();
}
[/csharp]
[/codeblocks]
</description>
<tutorials>
</tutorials>
Expand Down
13 changes: 11 additions & 2 deletions doc/classes/SpinBox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
<description>
SpinBox is a numerical input text field. It allows entering integers and floats.
[b]Example:[/b]
[codeblock]
[codeblocks]
[gdscript]
var spin_box = SpinBox.new()
add_child(spin_box)
var line_edit = spin_box.get_line_edit()
line_edit.context_menu_enabled = false
spin_box.align = LineEdit.ALIGN_RIGHT
[/codeblock]
[/gdscript]
[csharp]
var spinBox = new SpinBox();
AddChild(spinBox);
var lineEdit = spinBox.GetLineEdit();
lineEdit.ContextMenuEnabled = false;
spinBox.Align = LineEdit.AlignEnum.Right;
[/csharp]
[/codeblocks]
The above code will create a [SpinBox], disable context menu on it and set the text alignment to right.
See [Range] class for more options over the [SpinBox].
[b]Note:[/b] [SpinBox] relies on an underlying [LineEdit] node. To theme a [SpinBox]'s background, add theme items for [LineEdit] and customize them.
Expand Down
21 changes: 19 additions & 2 deletions doc/classes/Sprite2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,29 @@
</return>
<description>
Returns a [Rect2] representing the Sprite2D's boundary in local coordinates. Can be used to detect if the Sprite2D was clicked. Example:
[codeblock]
[codeblocks]
[gdscript]
func _input(event):
if event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_LEFT:
if get_rect().has_point(to_local(event.position)):
print("A click!")
[/codeblock]
[/gdscript]
[csharp]
public override void _Input(InputEvent inputEvent)
{
if (inputEvent is InputEventMouseButton inputEventMouse)
{
if (inputEventMouse.Pressed &amp;&amp; inputEventMouse.ButtonIndex == (int)ButtonList.Left)
{
if (GetRect().HasPoint(ToLocal(inputEventMouse.Position)))
{
GD.Print("A click!");
}
}
}
}
[/csharp]
[/codeblocks]
</description>
</method>
<method name="is_pixel_opaque" qualifiers="const">
Expand Down
18 changes: 14 additions & 4 deletions doc/classes/StreamPeer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,14 @@
<description>
Puts a zero-terminated ASCII string into the stream prepended by a 32-bit unsigned integer representing its size.
Note: To put an ASCII string without prepending its size, you can use [method put_data]:
[codeblock]
[codeblocks]
[gdscript]
put_data("Hello world".to_ascii())
[/codeblock]
[/gdscript]
[csharp]
PutData("Hello World".ToAscii());
[/csharp]
[/codeblocks]
</description>
</method>
<method name="put_u16">
Expand Down Expand Up @@ -261,9 +266,14 @@
<description>
Puts a zero-terminated UTF-8 string into the stream prepended by a 32 bits unsigned integer representing its size.
Note: To put an UTF-8 string without prepending its size, you can use [method put_data]:
[codeblock]
[codeblocks]
[gdscript]
put_data("Hello world".to_utf8())
[/codeblock]
[/gdscript]
[csharp]
PutData("Hello World".ToUTF8());
[/csharp]
[/codeblocks]
</description>
</method>
<method name="put_var">
Expand Down
48 changes: 35 additions & 13 deletions doc/classes/String.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,15 @@
<description>
Returns the index of the [b]first[/b] case-sensitive occurrence of the specified string in this instance, or [code]-1[/code]. Optionally, the starting search index can be specified, continuing to the end of the string.
[b]Note:[/b] If you just want to know whether a string contains a substring, use the [code]in[/code] operator as follows:
[codeblock]
# Will evaluate to `false`.
if "i" in "team":
pass
[/codeblock]
[codeblocks]
[gdscript]
print("i" in "team") # Will print `false`.
[/gdscript]
[csharp]
// C# has no in operator, but we can use `Contains()`.
GD.Print("team".Contains("i")); // Will print `false`.
[/csharp]
[/codeblocks]
</description>
</method>
<method name="findn">
Expand Down Expand Up @@ -354,9 +358,14 @@
<description>
Return a [String] which is the concatenation of the [code]parts[/code]. The separator between elements is the string providing this method.
Example:
[codeblock]
[codeblocks]
[gdscript]
print(", ".join(["One", "Two", "Three", "Four"]))
[/codeblock]
[/gdscript]
[csharp]
GD.Print(String.Join(",", new string[] {"One", "Two", "Three", "Four"}));
[/csharp]
[/codeblocks]
</description>
</method>
<method name="json_escape">
Expand Down Expand Up @@ -654,13 +663,18 @@
The splits in the returned array are sorted in the same order as the original string, from left to right.
If [code]maxsplit[/code] is specified, it defines the number of splits to do from the right up to [code]maxsplit[/code]. The default value of 0 means that all items are split, thus giving the same result as [method split].
Example:
[codeblock]
[codeblocks]
[gdscript]
var some_string = "One,Two,Three,Four"
var some_array = some_string.rsplit(",", true, 1)
print(some_array.size()) # Prints 2
print(some_array[0]) # Prints "Four"
print(some_array[1]) # Prints "Three,Two,One"
[/codeblock]
[/gdscript]
[csharp]
// There is no Rsplit.
[/csharp]
[/codeblocks]
</description>
</method>
<method name="rstrip">
Expand Down Expand Up @@ -723,13 +737,21 @@
Splits the string by a [code]delimiter[/code] string and returns an array of the substrings. The [code]delimiter[/code] can be of any length.
If [code]maxsplit[/code] is specified, it defines the number of splits to do from the left up to [code]maxsplit[/code]. The default value of [code]0[/code] means that all items are split.
Example:
[codeblock]
[codeblocks]
[gdscript]
var some_string = "One,Two,Three,Four"
var some_array = some_string.split(",", true, 1)
print(some_array.size()) # Prints 2
print(some_array[0]) # Prints "One"
print(some_array[1]) # Prints "Two,Three,Four"
[/codeblock]
print(some_array[0]) # Prints "Four"
print(some_array[1]) # Prints "Three,Two,One"
[/gdscript]
[csharp]
var someString = "One,Two,Three,Four";
var someArray = someString.Split(",", true); // This is as close as it gets to Godots API.
GD.Print(someArray[0]); // Prints "Four"
GD.Print(someArray[1]); // Prints "Three,Two,One"
[/csharp]
[/codeblocks]
If you need to split strings with more complex rules, use the [RegEx] class instead.
</description>
</method>
Expand Down
13 changes: 11 additions & 2 deletions doc/classes/SurfaceTool.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
</brief_description>
<description>
The [SurfaceTool] is used to construct a [Mesh] by specifying vertex attributes individually. It can be used to construct a [Mesh] from a script. All properties except indices need to be added before calling [method add_vertex]. For example, to add vertex colors and UVs:
[codeblock]
[codeblocks]
[gdscript]
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.set_color(Color(1, 0, 0))
st.set_uv(Vector2(0, 0))
st.set_vertex(Vector3(0, 0, 0))
[/codeblock]
[/gdscript]
[csharp]
var st = new SurfaceTool();
st.Begin(Mesh.PrimitiveType.Triangles);
st.SetColor(new Color(1, 0, 0));
st.SetUv(new Vector2(0, 0));
st.SetVertex(new Vector3(0, 0, 0));
[/csharp]
[/codeblocks]
The above [SurfaceTool] now contains one vertex of a triangle which has a UV coordinate and a specified [Color]. If another vertex were added without calling [method set_uv] or [method set_color], then the last values would be used.
Vertex attributes must be passed [b]before[/b] calling [method add_vertex]. Failure to do so will result in an error when committing the vertex information to a mesh.
Additionally, the attributes used before the first vertex is added determine the format of the mesh. For example, if you only add UVs to the first vertex, you cannot add color to any of the subsequent vertices.
Expand Down
19 changes: 15 additions & 4 deletions doc/classes/TextEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,24 @@
<description>
Perform a search inside the text. Search flags can be specified in the [enum SearchFlags] enum.
Returns an empty [code]Dictionary[/code] if no result was found. Otherwise, returns a [code]Dictionary[/code] containing [code]line[/code] and [code]column[/code] entries, e.g:
[codeblock]
var result = search(key, flags, line, column)
if !result.is_empty():
[codeblocks]
[gdscript]
var result = search("print", SEARCH_WHOLE_WORDS, 0, 0)
if !result.empty():
# Result found.
var line_number = result.line
var column_number = result.column
[/codeblock]
[/gdscript]
[csharp]
int[] result = Search("print", (uint)TextEdit.SearchFlags.WholeWords, 0, 0);
if (result.Length > 0)
{
// Result found.
int lineNumber = result[(int)TextEdit.SearchResult.Line];
int columnNumber = result[(int)TextEdit.SearchResult.Column];
}
[/csharp]
[/codeblocks]
</description>
</method>
<method name="select">
Expand Down
14 changes: 12 additions & 2 deletions doc/classes/TileMap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,22 @@
[b]Note:[/b] Data such as navigation polygons and collision shapes are not immediately updated for performance reasons.
If you need these to be immediately updated, you can call [method update_dirty_quadrants].
Overriding this method also overrides it internally, allowing custom logic to be implemented when tiles are placed/removed:
[codeblock]
[codeblocks]
[gdscript]
func set_cell(x, y, tile, flip_x=false, flip_y=false, transpose=false, autotile_coord=Vector2())
# Write your custom logic here.
# To call the default method:
.set_cell(x, y, tile, flip_x, flip_y, transpose, autotile_coord)
[/codeblock]
[/gdscript]
[csharp]
public void SetCell(int x, int y, int tile, bool flipX = false, bool flipY = false, bool transpose = false, Vector2 autotileCoord = new Vector2())
{
// Write your custom logic here.
// To call the default method:
base.SetCell(x, y, tile, flipX, flipY, transpose, autotileCoord);
}
[/csharp]
[/codeblocks]
</description>
</method>
<method name="set_cellv">
Expand Down
Loading