KTX Documentation

repository·master·Indexed 23 days ago

https://github.com/libktx/ktx

A Kotlin-first extension framework for libGDX that provides idiomatic Kotlin utilities, type-safe builders, and coroutine support. KTX enhances the development experience without replacing the core engine, offering specialized modules for Scene2D actor management, gdxAI behavior tree DSLs (ktx-ai), application lifecycle and platform utilities (ktx-app), and Artemis-ODB entity management.

Tokens
51.5K
Snippets
117
Records
234
Agent score
79%

What's inside KTX

  1. Overview of KTX: Assets management

    master
    KTX: Assets is a library designed for managing assets within Kotlin projects. It provides a structured way to handle asset loading and management, addressing common complexities in asset handling.
  2. Overview of KTX modules

    master

    KTX is a modular library designed to provide Kotlin extensions for libGDX. You can include only the specific modules required for your application. Common modules include:

    • ktx-actors: Scene2D GUI extensions.
    • ktx-ai: Type-safe builders for gdxAI.
    • ktx-app: ApplicationListener implementations.
    • ktx-async: Coroutine context based on libGDX threading.
    • ktx-assets-async: Non-blocking asset loading via coroutines.
    • ktx-scene2d: Type-safe builders for Scene2D GUI.
    • ktx-math: Operator functions for libGDX math.
    • ktx-inject: Low-overhead dependency injection.
    • ktx-graphics: Rendering and graphics utilities.

    (See the full list in the documentation for all available modules like ktx-box2d, ktx-json, ktx-log, etc.)

  3. Overview of KTX Async: Coroutines support and parallelization utilities

    master
    The ktx-async module provides utilities for Kotlin Coroutines support and parallelization. It is designed to simplify asynchronous programming patterns within the KTX ecosystem.
  4. Introduction to KTX

    master

    KTX is a Kotlin game framework designed to extend libGDX and make it more Kotlin-friendly. Instead of rewriting the libGDX API, KTX provides modular utilities and extensions for parts of libGDX that lack idiomatic Kotlin support. It leverages several Kotlin language features to improve usability, performance, and readability, including:

    • Operator overloads for collections and mathematical operations.
    • Extension methods to expand libGDX APIs without inheritance.
    • Inline methods for reduced overhead in listeners, builders, and loggers.
    • Nullable types for improved typing in interfaces and functions.
    • Default parameters to reduce boilerplate.
    • Type-safe builders for GUI, interface styling, ECS, and physics engine setup.
    • Default interface methods to simplify implementations.
    • Coroutines context for concurrency and non-blocking asset loading.
    • Reified types to simplify methods that typically require Class parameters.
  5. Overview of KTX: Style builders

    master
    KTX: Style builders is a library designed to provide a DSL (Domain Specific Language) for building styles in a more expressive and readable way. It aims to solve the verbosity and complexity often associated with traditional styling approaches by providing structured builders.
  6. Use KTX Collection utilities for libGDX

    master

    KTX provides extension functions and utilities for libGDX collections (Array, ObjectSet, ObjectMap, etc.). Since libGDX collections do not implement java.util.Collection, standard Kotlin library features often cannot be used with them. KTX bridges this gap by providing Kotlin-idiomatic syntax (like square brackets, +/- operators, and in checks) and factory methods while maintaining the performance benefits of libGDX's iterator-reusing collections.

    Important: It is highly advised to use import ktx.collections.* to ensure you are using the optimized KTX extension methods rather than the unoptimized Kotlin standard library methods for Iterable (which can be highly inefficient for types like ObjectSet).

  7. Use ktx-vis type-safe builders for VisUI widgets

    master

    The ktx-vis module provides a Kotlin DSL for creating VisUI widgets, making GUI layouts as readable as markup languages while maintaining the power of Kotlin. It extends ktx-scene2d with factory methods for various widget categories:

    • Root actors: visDialog, visWindow, toastTable.
    • Parent actors (Layout/Control): visTable, visTree, gridGroup, floatingGroup, flowGroup, dragPane, visScrollPane, visSplitPane, multiSplitPane, collapsible, horizontalCollapsible.
    • Parent actors (Can store actors): visTextButton, visImageButton, visImageTextButton, visCheckBox, visRadioButton, basicColorPicker, extendedColorPicker, spinner.
    • Child actors: visLabel, linkLabel, visImage, visList, visListOf, visProgressBar, visSelectBox, visSelectBoxOf, visSlider, visTextArea, highlightTextArea, scrollableTextArea, visTextField, visValidatableTextField, busyBar, separator.
    • Widget managers: buttonBar, listView, tabbedPane.
  8. Use KTX: VisUI style builders

    master
    KTX: VisUI style builders (ktx-vis-style) provides type-safe builders for VisUI widget styles. It extends the ktx-style module by adding factory methods for most VisUI widget styles directly on the Skin object. This allows you to define and extend styles for widgets like visCheckBox, visTextButton, menu, tabbedPane, and many others using a type-safe DSL.
  9. What is KTX AssetStorage and why use it?

    master

    KTX AssetStorage is a coroutine-based alternative to the libGDX AssetManager. It is designed for developers using Kotlin coroutines who want to avoid the thread-blocking and polling-based API of the standard AssetManager.

    Key Advantages over libGDX AssetManager:

    • True Multi-threading: Unlike AssetManager which uses a single thread for async operations, AssetStorage can utilize any number of threads provided by your chosen CoroutineContext.
    • Non-blocking API: Instead of calling update() every frame in your render loop, you simply launch a coroutine and await or load assets. The rendering thread remains unblocked.
    • Better Error Handling: Uses standard Kotlin try-catch blocks for asynchronous errors, providing more granular control than global listeners.
    • Flexible Identification: Supports loading multiple assets of different types from the same file path (e.g., a Texture and a Pixmap from the same PNG), whereas AssetManager uses the path as a unique identifier.
    • Controlled Loading Order: Users have full control over whether assets load sequentially or in parallel by managing coroutines.
  10. Use type aliases to avoid name clashes in gdxAI

    master

    To prevent naming conflicts with the Kotlin standard library or other dependencies, ktx-ai provides specific type aliases for common gdxAI classes:

    • GdxAiSequence<E> maps to com.badlogic.gdx.ai.btree.branch.Sequence<E>
    • GdxAiSelector<E> maps to com.badlogic.gdx.ai.btree.branch.Selector<E>
    • GdxAiRandom<E> maps to com.badlogic.gdx.ai.btree.decorator.Random<E>
  11. Group assets using AssetGroup

    master

    The AssetGroup class allows you to group related assets so they can be managed collectively (e.g., loadAll() or unloadAll()). You typically subclass AssetGroup and define assets as properties.

    • Immediate Loading: Use asset<T>(path) within a subclass. These assets are queued for loading immediately when the AssetGroup is instantiated.
    • Delayed/On-Demand Loading: Use delayedAsset<T>(path). These are not queued immediately; they are loaded on the first access or when loadAll() is called manually.
    • Prefixing: You can provide a filePrefix to the constructor to allow all assets in the group to be loaded from a specific subdirectory.
    /** Groups UI-related assets. */
    class UIAssets(manager: AssetManager) : AssetGroup(manager, filePrefix = "ui/") {
      val skin by asset<Skin>("skin.json")
      val clickSound by asset<Sound>("mapScreen.wav")
    }
    
    val uiAssets = UIAssets(manager)
    uiAssets.finishLoading()
    // Accessing assets:
    val mySkin = uiAssets.skin
    
    // Disposing:
    uiAssets.unloadAll()