-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd9d440
commit 89126d2
Showing
5 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Chapter 6: User interface (UI) | ||
|
||
Many games rely on showing some kind of UI on top of the 3d game, so let's try adding some basic UI to our game. | ||
|
||
## Showing the players position | ||
|
||
Add the following to `client.rs`: | ||
|
||
```rust | ||
#[element_component] | ||
fn PlayerPosition(hooks: &mut Hooks) -> Element { | ||
let (pos, _) = use_entity_component(hooks, player::get_local(), translation()); | ||
Text::el(format!("Player position: {}", pos.unwrap_or_default())) | ||
} | ||
``` | ||
|
||
> UI in Ambient is losely inspired by React. See [the UI docs for more info](../../reference/ui.md). | ||
> Tip: See [the UI examples](https://github.com/AmbientRun/Ambient/tree/main/guest/rust/examples/ui) to learn | ||
> how to use layout, buttons, editors and much more. | ||
And then add this to the `main` function in `client.rs`: | ||
|
||
```rust | ||
PlayerPosition.el().spawn_interactive(); | ||
``` | ||
|
||
You should now see something like this: | ||
|
||
![UI](ui.png) | ||
|
||
> Challenge: Try adding a `Button`, which when clicked sends a message to the server and moves the characters position somewhere else. | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.