Page Functions
Your game will normally consist of one or more pages, each of which is defined by a page function.
Common pages include:
- The Home page. This serves as the main menu for your game, and is shown first when players visit your game. From your home page, players navigate to other pages or enter the game itself.
- An About page that describes your game, including all its rules, its story and who made it. This is a good place to credit the creators of any third-party assets used in your game.
- A High Scores page that shows the best scores of all players in your game.
Each page of your game lives at its own unique URL. This URL can be bookmarked, shared with friends, or indexed by search engines.
Declaring a page
Home page
The HomePage is the page that is shown when players visit the base URL for your game, e.g.
https://easel.games/@username/gameName.
Declare your HomePage function like this:
pub page fn owner.HomePage() {
ContentScreen {
RaisedButton(AboutPage, backgroundColor=Color:Primary, width=25) { "About" }
}
}
Additional pages
You can declare as many additional pages as you want.
The example below declares an AboutPage that describes the game.
It will be found at a URL like this: https://easel.games/@username/gameName/Page/AboutPage.
pub page fn owner.AboutPage() {
Content {
H1 { "Pie Delight" }
P { "This game is a multiplayer pie throwing bonanza!" }
}
}
Object parameter
Notice that the page functions take an owner parameter as their object
(e.g. owner.HomePage or owner.AboutPage).
The owner is a Player entity, and represents the player who is looking at the page.
It can be used to access the data of the current player (e.g. their PlayerName,
and their preferences and accumulators).
pub page fn owner.GreetingPage() {
ContentScreen {
P { "Welcome, " + owner.PlayerName + "!" }
}
}
Linking to a page
To create a link to a page, you simply call the page function. This creates an Intent to navigate to that page. Attach this intent to a Button to allow the user to navigate to that page:
pub page fn owner.HomePage() {
ContentScreen {
RaisedButton(AboutPage, backgroundColor=Color:Primary, width=25) { "About" }
}
}
Calling the AboutPage function creates an intent to navigate to that page.
It does not execute any code inside the AboutPage function.
It is only once the player clicks the button and navigates to the AboutPage
that the code inside the AboutPage function is executed.
Links
The Button component has multiple variants,
including RaisedButton, FlatButton, and Link.
The Link variant creates a hyperlink to the page.
Links work just like every other hyperlink in your web browser.
That is, your player can right-click on a link and open it in a new tab,
or they can copy the link to share with their friends.
pub page fn owner.HomePage() {
ContentScreen {
Link(AboutPage) { "About" }
}
}
Page Parameters
A page function can take any number of parameters.
Below is an example of a page called ItemPage that takes a parameter called itemId,
which is used to decide what content to show:
pub page fn owner.ItemPage(itemId) {
Content {
P {
match itemId {
"swordOfBravery" => {
"Sword of Bravery: passed down through generations of heroes, this sword is said to grant great courage to its wielder."
},
"flightOfThePhoenix" => {
"Flight of the Phoenix: a legendary bow that is said to never miss its target, and can shoot arrows that burst into flames."
},
}
}
}
}
- Each parameter will be passed in via the URL.
In this example, if the
itemIdisflightOfThePhoenix, the URL would behttps://easel.games/@username/gameName/Pages/ItemPage/flightOfThePhoenix. - Every parameter must be a Sendable value. That is, basic types like numbers or strings can be page parameters, but complex types like entities or functions cannot be used as they cannot be serialized into a URL.
- Because the parameter values are included in the URL, it is easy for players to bookmark or share a link to a specific page in your game.
Linking to pages with parameters
Simply call the page function with the appropriate parameters to create a link to a page with parameters:
pub page fn owner.HomePage() {
ContentScreen {
Link(ItemPage("swordOfBravery")) { "About: Sword of Bravery" }
RaisedButton(ItemPage("flightOfThePhoenix")) { "About: Flight of the Phoenix" }
}
}
Search engine indexing
Search engines like Google can index your pages. That means, when people search for keywords relevant to your game, there is a chance your pages might show up in the search results. This can help players find your game, even if they cannot remember the exact URL.
Easel only allows search engines to index the pages that do not take parameters. This is for a number of reasons, one of them being that pages which take parameters can have an infinite number of possible URLs. This can sometimes confuse crawlers and make them stop indexing the entire site.
// These pages will be indexed by search engines
pub page fn owner.HomePage() { /* ... */ }
pub page fn owner.AboutPage() { /* ... */ }
// This page will not be indexed by search engines, because it takes a parameter
pub page fn owner.ItemPage(itemId) { /* ... */ }
You might not need a new page
Each time your player navigates to a new page, a new instance of your program is created, losing all the state of the previous page. Any state you want to keep, you must send as parameters to the new page.
If you are finding yourself sending a lot of data from one page to another, consider whether you really need a new page. You can always make a pop-up dialog on the same page instead of creating a new page.
Think of a page as being a static URL that can be bookmarked or shared with friends. The URLs for your game should be permanent and long-lasting. The player should be able to bookmark a URL and return to it the next day and still see meaningful content. If you are sending a lot of transient data through page parameters, consider whether you should be using a page at all. Maybe you just need a pop-up dialog on the existing page or within the game itself.
Prim serialization
When you pass in parameters to a page, they are serialized into the URL using a custom encoding called Prim. Prim aims to be human-readable, compact but most-importantly, URL-safe. It makes your parameters prim and proper for the web. Here are some examples of what URLs look like with different types of data:
Numbers:
https://example.easel.games/Pages/ExamplePage/123
Strings:
https://example.easel.games/Pages/ExamplePage/flightOfThePhoenix
https://example.easel.games/Pages/ExamplePage/'#vegetables'
Symbols:
https://example.easel.games/Pages/ExamplePage/$swordOfBravery
Vectors:
https://example.easel.games/Pages/ExamplePage/@(12,34)
Colors:
https://example.easel.games/Pages/ExamplePage/Co:ffcc8833
Arrays:
https://example.easel.games/Pages/ExamplePage/(1,2,3,4,5,6)
Maps:
https://example.easel.games/Pages/ExamplePage/(:$head=1,$legs=2,$hat=3)
How it works
Prim uses the leading character(s) to determine the type of the value, using only URL-safe characters:
- Numbers:
0-9. Also-for negative numbers. - Strings:
A-Z. If a string begins with a non-alphabetic character, it is wrapped in single quotes'. - Symbols:
$ - Vectors:
@using commas,to separate the elements. - Arrays:
( )using commas,to separate the elements. - Maps:
(: )using commas,to separate the key-value pairs, and=to separate keys from values. - There are many more Sendable types and not all of them have a special leading character,
for example
Co:is for colors andFl:is for flags.
Strings are URL-encoded so that any special characters are safe to use in a URL.