The Trigger System is a flexible and powerful system for managing game triggers and events. It supports various trigger types, conditions, and actions, making it ideal for creating interactive game mechanics.
- 🎯 Multiple Trigger Types: Support for immediate, event-based, and periodic triggers
- 🔄 Conditional Execution: Flexible condition system for trigger activation
- 📊 Probability Control: Built-in trigger chance control
- ⏱️ Periodic Triggers: Support for time-based periodic triggers
- 🔌 Event Bus Integration: Optional integration with the event bus system
- 💾 Persistence: Support for trigger state serialization
var trigger_manager = CoreSystem.trigger_manager# Create an event trigger
var trigger = GameplayTrigger.new({
"trigger_type": GameplayTrigger.TRIGGER_TYPE.ON_EVENT,
"trigger_event": "player_entered",
"conditions": [
{
"type": "state_trigger_condition",
"state_name": "player_in_area",
"required_state": "true"
}
]
})
# Connect to trigger signal
trigger.triggered.connect(_on_trigger_activated)
# Activate trigger
trigger.activate()# Create a periodic trigger
var periodic_trigger = GameplayTrigger.new({
"trigger_type": GameplayTrigger.TRIGGER_TYPE.PERIODIC,
"period": 2.0, # Trigger every 2 seconds
"trigger_chance": 0.5 # 50% chance to trigger
})
periodic_trigger.triggered.connect(_on_periodic_trigger)
periodic_trigger.activate()# Send event to trigger system
trigger_manager.handle_event("player_entered", {
"state_name": "player_in_area",
"state_value": "true"
})Check the trigger_demo directory for a complete example project.
# Create area trigger
var area_trigger = GameplayTrigger.new({
"trigger_type": GameplayTrigger.TRIGGER_TYPE.ON_EVENT,
"trigger_event": "enter_area",
"conditions": [
{
"type": "state_trigger_condition",
"state_name": "player_in_area",
"required_state": "true"
}
]
})
# Handle area events
func _on_area_entered(body: Node) -> void:
trigger_manager.handle_event("enter_area", {
"state_name": "player_in_area",
"state_value": "true"
})Global trigger manager responsible for trigger registration and event handling.
handle_event(trigger_type: StringName, context: Dictionary) -> void: Handle trigger eventregister_event_trigger(trigger_type: StringName, trigger: GameplayTrigger) -> void: Register event triggerregister_periodic_trigger(trigger: GameplayTrigger) -> void: Register periodic triggercreate_condition(config: Dictionary) -> TriggerCondition: Create trigger condition
Core trigger class that handles trigger logic and conditions.
trigger_type: TRIGGER_TYPE: Type of trigger (IMMEDIATE, ON_EVENT, PERIODIC)conditions: Array[TriggerCondition]: List of conditionspersistent: bool: Whether trigger state should persistmax_triggers: int: Maximum number of times trigger can activatetrigger_count: int: Current trigger counttrigger_event: StringName: Event name for event-based triggersperiod: float: Time between triggers for periodic triggerstrigger_chance: float: Probability of trigger activation
activate(initial_context: Dictionary = {}) -> void: Activate triggerdeactivate() -> void: Deactivate triggerexecute(context: Dictionary) -> void: Execute triggershould_trigger(context: Dictionary) -> bool: Check if trigger should activatereset() -> void: Reset trigger state
Base class for trigger conditions.
evaluate(context: Dictionary) -> bool: Evaluate condition
-
Choose Appropriate Trigger Type
- Use ON_EVENT for event-driven triggers
- Use PERIODIC for time-based triggers
- Use IMMEDIATE for one-shot triggers
-
Manage Trigger Lifecycle
- Activate triggers when needed
- Deactivate triggers when not needed
- Reset triggers when reusing
-
Use Conditions Effectively
- Keep conditions simple and focused
- Combine conditions for complex logic
- Use appropriate condition types
-
Performance Considerations
- Limit number of active triggers
- Use appropriate update intervals for periodic triggers
- Clean up unused triggers
-
Trigger Not Firing
- Check if trigger is activated
- Verify event names match exactly
- Check condition logic
- Ensure trigger chance is appropriate
-
Performance Problems
- Too many active triggers
- Too frequent periodic updates
- Complex condition evaluations
-
Event Integration Issues
- Check event bus subscription setting
- Verify event names and parameters
- Check event handler connections