Lich scripts run in Ruby threads, allowing you to perform multiple operations concurrently. Understanding threading is essential for creating sophisticated scripts that can:
Monitor multiple conditions simultaneously
Run background tasks while performing main logic
React to game events in real-time
Coordinate multiple scripts
Scripts share access to game buffers and resources. Improper thread management can cause race conditions and unexpected behavior.
# Start a threadworker = Thread.new do # Thread work pause 10 echo "Thread complete"end# Check if thread is aliveif worker.alive? echo "Worker is still running"end# Wait for thread to completeworker.joinecho "Worker finished"# Kill a threadworker.kill
Each script has its own thread group for managing child threads:
script = Script.current# Add thread to script's groupworker = Thread.new do echo "I belong to the script"endscript.thread_group.add(worker)# List all threads in scriptscript.thread_group.list.each do |t| echo "Thread: #{t.inspect}"end# Check thread membershipif script.has_thread?(Thread.current) echo "Current thread is part of script"end
When a script exits, all threads in its thread group are automatically terminated.
# Monitor health and auto-healwatchdog = Thread.new do loop do if Char.health < 50 echo "Low health! Healing..." fput "drink my potion" waitfor "You feel better" end pause 2 endendscript.thread_group.add(watchdog)# Main script continuesloop do # Main hunting logic fput "attack monster" waitfor "You swing"end
# Death monitordeath_handler = Thread.new do waitfor "You have been slain" echo "You died! Stopping hunting." stop_script("hunting_script")endscript.thread_group.add(death_handler)
workers = []# Create multiple worker threads3.times do |i| worker = Thread.new do loop do # Get work from queue task = @work_queue.pop echo "Worker #{i} processing: #{task}" # Process task end end workers << worker script.thread_group.add(worker)end# Add work to queue@work_queue = Queue.new@work_queue << "task1"@work_queue << "task2"
counter = 0mutex = Mutex.newthreads = 5.times.map do Thread.new do 100.times do mutex.synchronize do counter += 1 end end endendthreads.each(&:join)echo "Counter: #{counter}" # Always 500
# Be careful with shared state@shared_counter = 0@mutex = Mutex.newdef increment @mutex.synchronize do @shared_counter += 1 endend# Use in threadsthreads = 10.times.map do Thread.new { 100.times { increment } }endthreads.each(&:join)echo "Count: #{@shared_counter}" # Should be 1000
# SQLite isn't thread-safe by defaultdb = Script.dbmutex = Mutex.newThread.new do mutex.synchronize do db.execute("INSERT INTO data VALUES (?)", ["value"]) endend