# Looping Script

Features

* **No state management** - Simple loop-based execution
* **Optional initialization** - Override `onInit()` for setup
* **Direct script logic** - Implement logic directly in `onTick()`
* **Minimal interface** - Cleanest API for simple scripts

### Usage

```kotlin
class MyLoopingScript : LoopingScript() {
    
    override fun onInit() {
        // Optional initialization
        status = "Starting simple loop"
        println("Looping script initialized")
    }
    
    override suspend fun onTick() {
        // Your script logic here - no state management
        println("Looping...")
        
        // Example: Simple woodcutting loop
        if (shouldChopTree()) {
            chopTree()
            awaitIdle() // Wait until player is idle
        } else if (shouldDropLogs()) {
            dropLogs()
            awaitTicks(2) // Wait 2 ticks
        }
    }
    
    private fun shouldChopTree(): Boolean {
        // Your logic to determine if should chop tree
        return true
    }
    
    private fun chopTree() {
        // Your tree chopping logic
    }
    
    private fun shouldDropLogs(): Boolean {
        // Your logic to determine if should drop logs
        return false
    }
    
    private fun dropLogs() {
        // Your log dropping logic
    }
}
```

### When to Use

Use LoopingScript when:

* You don't need state management
* You want the simplest possible API
* You have straightforward script logic
* You prefer direct control over execution flow

### Key Methods

* `onInit()` - Override for optional initialization
* `onTick()` - Override to implement your script logic

### Common Patterns

#### Simple Loop Pattern

```kotlin
class SimpleScript : LoopingScript() {
    override suspend fun onTick() {
        if (shouldDoAction()) {
            doAction()
            awaitIdle()
        } else {
            awaitTicks(1)
        }
    }
}
```

#### Conditional Execution Pattern

```kotlin
class ConditionalScript : LoopingScript() {
    override suspend fun onTick() {
        when {
            shouldGather() -> gather()
            shouldBank() -> bank()
            else -> wait()
        }
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kxapi.gitbook.io/kxapi/scripting/script-types/looping-script.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
