KTX Documentation
repository·master·Indexed 23 days ago
https://github.com/libktx/ktxA 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.
What's inside KTX
- 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.
Overview of KTX modules
masterKTX 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.)Overview of KTX Ashley ECS utilities
masterKTX Ashley provides utility extensions and helper functions for the Ashley Entity Component System (ECS) framework. It is designed to make working with Ashley more idiomatic and less boilerplate-heavy in Kotlin.Overview of KTX Async: Coroutines support and parallelization utilities
masterThektx-asyncmodule provides utilities for Kotlin Coroutines support and parallelization. It is designed to simplify asynchronous programming patterns within the KTX ecosystem.Introduction to KTX
masterKTX 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
Classparameters.
Overview of KTX: Style builders
masterKTX: 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.Use KTX Collection utilities for libGDX
masterKTX provides extension functions and utilities for libGDX collections (
Array,ObjectSet,ObjectMap, etc.). Since libGDX collections do not implementjava.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, andinchecks) 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 forIterable(which can be highly inefficient for types likeObjectSet).Use ktx-vis type-safe builders for VisUI widgets
masterThe
ktx-vismodule provides a Kotlin DSL for creating VisUI widgets, making GUI layouts as readable as markup languages while maintaining the power of Kotlin. It extendsktx-scene2dwith 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.
- Root actors:
Use KTX: VisUI style builders
masterKTX: VisUI style builders (ktx-vis-style) provides type-safe builders for VisUI widget styles. It extends thektx-stylemodule by adding factory methods for most VisUI widget styles directly on theSkinobject. This allows you to define and extend styles for widgets likevisCheckBox,visTextButton,menu,tabbedPane, and many others using a type-safe DSL.What is KTX AssetStorage and why use it?
masterKTX
AssetStorageis a coroutine-based alternative to the libGDXAssetManager. It is designed for developers using Kotlin coroutines who want to avoid the thread-blocking and polling-based API of the standardAssetManager.Key Advantages over libGDX
AssetManager:- True Multi-threading: Unlike
AssetManagerwhich uses a single thread for async operations,AssetStoragecan utilize any number of threads provided by your chosenCoroutineContext. - Non-blocking API: Instead of calling
update()every frame in your render loop, you simply launch a coroutine andawaitorloadassets. The rendering thread remains unblocked. - Better Error Handling: Uses standard Kotlin
try-catchblocks 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
Textureand aPixmapfrom the same PNG), whereasAssetManageruses the path as a unique identifier. - Controlled Loading Order: Users have full control over whether assets load sequentially or in parallel by managing coroutines.
- True Multi-threading: Unlike
Use type aliases to avoid name clashes in gdxAI
masterTo prevent naming conflicts with the Kotlin standard library or other dependencies,
ktx-aiprovides specific type aliases for common gdxAI classes:GdxAiSequence<E>maps tocom.badlogic.gdx.ai.btree.branch.Sequence<E>GdxAiSelector<E>maps tocom.badlogic.gdx.ai.btree.branch.Selector<E>GdxAiRandom<E>maps tocom.badlogic.gdx.ai.btree.decorator.Random<E>
Group assets using AssetGroup
masterThe
AssetGroupclass allows you to group related assets so they can be managed collectively (e.g.,loadAll()orunloadAll()). You typically subclassAssetGroupand define assets as properties.- Immediate Loading: Use
asset<T>(path)within a subclass. These assets are queued for loading immediately when theAssetGroupis instantiated. - Delayed/On-Demand Loading: Use
delayedAsset<T>(path). These are not queued immediately; they are loaded on the first access or whenloadAll()is called manually. - Prefixing: You can provide a
filePrefixto 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()- Immediate Loading: Use