Skip to content

Card Paging & Search System (CardManager)

DuncanSzabaga edited this page Nov 17, 2025 · 1 revision

Card Deck Paging & Search

REMEMBER: Don't adjust global.cards, if you want to filter cards for showing, adjust global.show_cards instead

Summary

  • Loading a JSON file of cards (sample.json)
  • Storing it in global.cards
  • Paging through cards
  • Searching cards by different fields
  • Getting the total number of pages for a given card list

Overview

The script does the following:

  1. Paging setup

    global.page_size = 3;    // how many items to read per page
    global.page_index = 0;   // current page index (0-based)
  2. File loading & JSON parsing

    It looks for a file called sample.json, reads it into a string, parses it as JSON, and assigns the cards array to global.cards:

Image
  1. Paging function: read_page(page_index, deck)
  • The purpose of creating this function because I think it's better to access data page by page, not all at once even though all cards are already loaed into global.cards already. Also, it is useful for paging in Card Encyclopedia page.

  • I initailly made it have three cards per page as it's defined in global.page_size

    function read_page(page_index, deck) {
        var start = page_index * global.page_size;
        var end_ = min(start + global.page_size, array_length(deck));
        
        var page_cards = [];
        for (var i = start; i < end_; i++) {
            var card = deck[i];
            array_push(page_cards, card);
        }
        return page_cards;
    }
    • page_index: 0-based index of the page you want.
    • deck: an array of card structs (for example global.cards or search results).
    • Returns: an array page_cards with all cards on that page.
    • example of usage global.card_read = read_page(global.page_index, global.show_cards); (in PagingManager)
  1. Search function: search_cards(_query, _field)
  • This function is mainly used in Card Encyclopedia page. It accepts a variety of fields, but we're only using type right now since in Card Encyclopedia page we only need to distinguish them by types (active, passive, movement)

    function search_cards(_query, _field) {
        var results = [];
        var total = array_length(global.cards);
        var query_lower = string_lower(_query);
    
        for (var i = 0; i < total; i++) {
            var card = global.cards[i];
            var value = "";
    
            switch (_field) {
                case "id":     value = card.id;     break;
                case "name":   value = card.name;   break;
                case "rarity": value = card.rarity; break; //-----------> not existing anymore
                case "type":   value = card.type;   break;
                default:
                    show_debug_message("Invalid search field: " + _field);
                    return [];
            }
    
            if (string_pos(query_lower, string_lower(value)) > 0) {
                array_push(results, card);
            }
        }
    
        return results; // list (array) of matching cards
    }
    • Supported fields: "id", "name", "rarity", "type".
    • Case-insensitive substring search.
    • Returns an array of card structs matching the query.
    • example:
Image
  1. Page count helper: get_page_total_number()

    function get_page_total_number() {
        // make sure global.show_cards exists AND is an array
        if (!variable_global_exists("show_cards") || !is_array(global.show_cards)) {
            show_debug_message("Error: global.show_cards is not defined or not an array.");
            return 0;
        }
    
        var total_cards = array_length(global.show_cards);
        if (total_cards <= 0) {
            return 0;
        }
    
        return ceil(total_cards / global.page_size);
    }
    • Show the total page number of global.show_cards
    • Returns the total number of pages based on global.page_size.

Clone this wiki locally