A downloadable API wrapper

Download NowName your own price

An API wrapper of PokéApi v2 for Godot 4

This boilerplate is for setting up Pokemon data easier in Godot.
For more info visit the Github repository or read the included README in the download.


Setup

  1. Make a new Godot 4 project.
  2. Copy the Data folder, PokeDotClient.gd, and PokeDotClient.tscn into the new project directory.
  3. Instance PokeDotClient and add it as a child of your main scene.


Usage

This is the main request function which is used by different methods for various data. It uses the main_url and endpoint parameters for HTTPRequest.request(), while the method shows what the request is and its status or error.

func PokeDotClient(main_url: String = url, endpoint: String = "", method: String = "") -> void:
    print("Request endpoint: %s" % main_url + endpoint + "\n")
  
    if not main_url == "" or not endpoint == "":
        var error: int = 0
        error = request(main_url + endpoint, ["Accept: application/json"], HTTPClient.METHOD_GET)
        if error == OK: print("✓ %s() run successfully." % method)
        else: print("❌ %s() failed." % method)
  
    else:
        print("❌ PokeDotClient() failed. The main_url or endpoint cannot be empty.")

The get_pokemon_pagination() the getter for the Pokemon pagination which requests and stores them to PokemonPagination.

func get_pokemon_pagination(endpoint: String = "pokemon", limit: int = 20, offset: int = 0) -> void:
    query = Get.POKEMON_PAGINATION
    PokeDotClient(url, "%s/?limit=%s&offset=%s" % [endpoint, limit, offset], "get_pokemon_pagination")

Together it works by simply calling the get_pokemon_pagination method, which then sets its parameters to PokeDotClient() to make a request.

func _ready() -> void:
    get_pokemon_pagination("pokemon", 20, 0)


Examples

Note: For this example, if you're following this guide, then kindly go to PokeDotClient.gd or simply click the PokeDotClient.tscn main scene and disable the Print Data, Print Result, and Print Response in the properties tab i.e. Inspector.

By default, every requests prints the following properties to the terminal, but by turning them off we'll only print specifically when we need to.

Requesting multiple data

extends Node
var pokeapi = preload("res://PokeDotClient.tscn").instantiate()
  
func _ready() -> void:
    add_child(pokeapi)
    pokeapi.get_ability(1)
    await pokeapi.request_completed
    pokeapi.get_pokemon("ditto")

So what happens here is that after instance and adding PokeDotClient as a child, we used its get_ability and get_pokemon methods.

As you can see, we await the request_completed signal because the first method here, get_ability, request is still running. It basically tells here to wait for get_ability to complete first before starting get_pokemon request.


Accessing the received data

After the requests, the data from get_ability and get_pokemon are then stored in Ability and Pokemon respectively.

These are the objects in Data directory, e.g. /Data/Ability.gd and /Data/Pokemon.gd, and are handled by PokeDotClient. Therefore, the data can be accessed as PokeDotClient.ability and PokeDotClient.pokemon respectively.

extends Node
var pokeapi = preload("res://PokeDotClient.tscn").instantiate()
  
func _ready() -> void:
    add_child(pokeapi)
    pokeapi.get_ability(1)
    await pokeapi.request_completed
    var ability: Dictionary = pokeapi.ability.data
    print("\n"+JSON.stringify(ability, "\t")+"\n")
  
    pokeapi.get_pokemon("ditto")
    await pokeapi.request_completed
    var pokemon: Dictionary = pokeapi.pokemon.get_data()
    print("\n"+JSON.stringify(pokemon, "\t")+"\n")

The data can be accessed as a member, e.g. ability.data, or with the objects' getter function pokeapi.ability.get_data().

Here we assigned the received data to variables ability and pokemon after the request was completed. Then we simply print it on them on the console in a prettified JSON format.


Output:

Request endpoint: https://pokeapi.co/api/v2/ability/1/
✓ get_ability() run successfully.
{
    "effect_changes": [
        {
            "effect_entries": [
                {
                    "effect": "N'a aucun effet en combat.",
                    "language": {
                    "name": "fr",
                    "url": "https://pokeapi.co/api/v2/language/5/" ...
  
Request endpoint: https://pokeapi.co/api/v2/pokemon/ditto/
✓ get_pokemon() run successfully.
{
    "abilities": [
        {
            "ability": {
                "name": "limber",
                "url": "https://pokeapi.co/api/v2/ability/7/"
        },
    "is_hidden": false,
    "slot": 1.0 ...

  

Creating an object to hold data

Just as we can instance and add a PokeDotClient as a child to our main scene, we can also do the same to our data containers in res://Data.

For this example we'll create a new Pokemon object for Pikachu.

extends Node
var pokeapi = preload("res://PokeDotClient.tscn").instantiate()
  
func _ready() -> void:
    add_child(pokeapi)
    pokeapi.get_pokemon("pikachu")
    await pokeapi.request_completed
  
    var pikachu: Pokemon = Pokemon.new()
    pikachu.name = "Pikachu"
    add_child(pikachu, true)
  
    var pokedex: Dictionary = pokeapi.pokemon.get_data()
    pikachu.data = pokedex
    print("\nPikachu's info:\n"+JSON.stringify(pikachu.data, "\t")+"\n")

Here we used get_pokemon("pikachu") to get their data, then we made a new Pokemon set to a pikachu variable. After setting its name, we used add_child(pikachu, true) where its 2nd parameter is set to true so its name is Pikachu in the SceneTree.

Node               - main scene
├─ PokeDotClient   - pokeapi wrapper instance
└─ Pikachu         - the new Pokemon

Then we get the actual data with pokemon.get_data() and set its value to pikachu.data by making a new Dictionary.

Finally in this example we just accessed Pikachu.data and printed them on terminal.

Request endpoint: https://pokeapi.co/api/v2/pokemon/pikachu/
✓ get_pokemon() run successfully.
  
Pikachu's info: {
    "abilities": [
        {
            "ability": {
                "name": "static",
                "url": "https://pokeapi.co/api/v2/ability/9/"
            },
            "is_hidden": false,
            "slot": 1.0
        },
        {
            "ability": {
                "name": "lightning-rod",
                "url": "https://pokeapi.co/api/v2/ability/31/"
            },
            "is_hidden": true,
            "slot": 3.0
        }
    ],
    "base_experience": 112,
    "cries": {
        "latest": "https://raw.githubusercontent.com/PokeAPI/cries/main/cries/pokemon/latest/25.ogg",
        "legacy": "https://raw.githubusercontent.com/PokeAPI/cries/main/cries/pokemon/legacy/25.ogg"
    },
    "forms": [
        {
            "name": "pikachu",
            "url": "https://pokeapi.co/api/v2/pokemon-form/25/"
        }
    ], ...

For a quick test you can also check out PokeDotTest.


Structure

This is the structure of the entire project. This only shows the relevant directories and files for this API wrapper.

# Directory
res:// (root)
├─ Data                  - contains the objects that hold various data.
│   ├─ Ability.gd        - stores get_ability() received data.
│   ├─ ...
│   └─ VersionGroup.gd   - stores get_version_group() received data.
├─ PokeDotClient.gd      - is the script of the pokeapi wrapper.
└─ PokeDotClient.tscn    - is the pokeapi wrapper that handles all http requests.
  
# Main scene
Node                     - is the main scene of the new project.
└─ PokeDotClient         - is the instanced pokeapi wrapper.
Published 8 days ago
StatusReleased
CategoryTool
AuthorUbeJelly
Made withGodot
Tagsapi, api-wrapper, boilerplate, pokeapi, pokemon
Code licenseMIT License
Average sessionA few minutes
LanguagesEnglish
InputsKeyboard, Mouse
LinksGitHub
ContentNo generative AI was used

Download

Download NowName your own price

Click download now to get access to the following files:

PokeDot.zip 193 kB

Leave a comment

Log in with itch.io to leave a comment.