JSON Spell Guide

This guide covers working with JSON spell definitions in OpenRoads, which are primarily used for hardcoded spells that require custom Go code logic.

📚 Table of Contents

🎯 When to Use JSON Spells

Use JSON spells when you need:

✅ Custom Logic

  • Complex spell behaviors that can't be achieved with standard effects

  • Integration with external systems

  • Dynamic spell behavior based on game state

  • Advanced AI or procedural generation

✅ Performance Critical Spells

  • Spells that need to run very frequently

  • Spells with complex calculations

  • Spells that interact with multiple game systems

✅ Legacy Support

  • Existing hardcoded spells (like Aerial Servant)

  • Maintaining backward compatibility

  • Gradual migration from hardcoded to YAML

❌ Don't Use JSON For

  • Simple damage/heal/teleport spells (use YAML instead)

  • Standard spell effects (YAML is easier to maintain)

  • Spells that non-programmers need to modify

📊 JSON vs YAML Comparison

Feature
YAML
JSON

Ease of Use

✅ Very Easy

⚠️ Moderate

Human Readable

✅ Excellent

⚠️ Good

Custom Logic

❌ Limited

✅ Full Control

Performance

⚠️ Good

✅ Excellent

Maintenance

✅ Easy

⚠️ Requires Programming

Hot Reload

✅ Planned

⚠️ Requires Restart

Non-Programmer Friendly

✅ Yes

❌ No

🏗️ JSON Spell Structure

JSON spells are defined in world/spells.json:

Key Differences from YAML

  • Uses "custom_handler" field to specify Go function

  • effects array is usually empty (handled by custom code)

  • All strings must be in double quotes

  • No comments allowed

  • Stricter syntax requirements

🔧 Custom Handlers

The custom_handler field tells the system which Go function to call:

This calls the cast_servant function in the Go code.

Handler Function Signature

Custom handlers are implemented in lib/spell_engine.go:

📝 JSON Examples

Simple Custom Spell

Complex Admin Spell

Legacy Spell (Aerial Servant)

🛠️ Creating Custom Handlers

Step 1: Define Your Spell in JSON

Add your spell to world/spells.json with a unique custom_handler name.

Step 2: Implement the Handler Function

Create your custom function in the appropriate Go file:

Step 3: Register the Handler

Add your handler to the switch statement in handleCustomSpell:

Step 4: Test Your Spell

  1. Compile the server: go build

  2. Start the server

  3. Test your spell in-game

📋 Best Practices

JSON Formatting

  • Use proper indentation (2 or 4 spaces)

  • Validate JSON syntax before committing

  • Use meaningful handler names

  • Include comprehensive messages

Code Organization

  • Keep custom handlers in appropriate files

  • Use descriptive function names

  • Add proper error handling

  • Include logging for debugging

Performance

  • Minimize database operations in handlers

  • Cache frequently accessed data

  • Use efficient algorithms

  • Consider concurrency implications

Security

  • Validate all input parameters

  • Check player permissions

  • Prevent resource exhaustion

  • Sanitize user input

🔄 Migration Guide

From Hardcoded to JSON

  1. Extract spell properties into JSON format

  2. Create custom handler function

  3. Register handler in switch statement

  4. Test thoroughly to ensure behavior matches

  5. Remove old hardcoded logic once confirmed working

From JSON to YAML

  1. Analyze custom logic - can it be replaced with standard effects?

  2. Create YAML equivalent if possible

  3. Test YAML version thoroughly

  4. Remove JSON version once YAML is confirmed working

  5. Update documentation to reflect changes

Example Migration: Simple Damage Spell

Before (JSON with custom handler):

After (YAML with standard effects):

🎯 Advanced Patterns

Conditional Effects

Multi-Stage Spells

Integration with Game Systems

🎓 Conclusion

JSON spells provide powerful customization capabilities for complex spell behaviors that can't be achieved with standard YAML effects. Use them wisely for spells that truly need custom logic, and prefer YAML for standard spell effects.

Next Steps

  1. Spell Effects Reference - Learn about standard effects

  2. YAML Spell Tutorial - Master YAML spell creation

  3. Custom Spells Directoryarrow-up-right - See practical examples

  4. Experiment - Try creating your own custom handlers!

Remember: With great power comes great responsibility. JSON spells require programming knowledge and careful testing. When in doubt, try to achieve your goals with YAML first! ✨

Last updated

Was this helpful?