Created
September 14, 2026 05:44
-
-
Save redxdev/4bc56c795d01822b211b47704558c9c7 to your computer and use it in GitHub Desktop.
source engine-style light flicker patterns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @tool | |
| extends Light3D | |
| ## A string of letters that determine the light's brightness. a is 0%, m is 100%, z is 200%. | |
| ## All other characters will result in the brightness staying the same for that tick. | |
| ## Updates 10 times per second (10hz). | |
| @export var flicker_pattern: String: | |
| get: return flicker_pattern | |
| set(value): | |
| flicker_pattern = value | |
| if not Engine.is_editor_hint(): | |
| _update_pattern_timer() | |
| var _pattern_timer: Timer | |
| var _pattern_index := -1 | |
| var _base_energy: float | |
| var _set_energy: float | |
| const PATTERN_UPDATE_TIME := 1.0 / 10.0 | |
| const PATTERN_LETTER_START := "a" | |
| const PATTERN_LETTER_END := "z" | |
| func _update_pattern_timer() -> void: | |
| _pattern_index = -1 | |
| var wants_timer := not flicker_pattern.is_empty() | |
| if wants_timer and not _pattern_timer: | |
| _pattern_timer = Timer.new() | |
| _pattern_timer.autostart = true | |
| _pattern_timer.wait_time = PATTERN_UPDATE_TIME | |
| _pattern_timer.timeout.connect(_play_next_pattern_index) | |
| add_child(_pattern_timer) | |
| _play_next_pattern_index() | |
| elif not wants_timer and _pattern_timer: | |
| _pattern_timer.queue_free() | |
| _pattern_timer = null | |
| light_energy = _base_energy | |
| _set_energy = _base_energy | |
| func _play_next_pattern_index() -> void: | |
| _pattern_index = _pattern_index + 1 | |
| if _pattern_index >= flicker_pattern.length(): | |
| _pattern_index = 0 | |
| if _pattern_index >= flicker_pattern.length(): | |
| push_warning("Light %s tried to play a flicker pattern of 0 size" % self) | |
| _update_pattern_timer() | |
| return | |
| if not is_equal_approx(light_energy, _set_energy): | |
| _base_energy = light_energy # Energy was updated externally | |
| _set_energy = _base_energy | |
| var pct := _calc_letter_pct(flicker_pattern.unicode_at(_pattern_index)) | |
| _set_energy = pct * _base_energy | |
| light_energy = _set_energy | |
| func _calc_letter_pct(letter: int) -> float: | |
| var lstart := ord(PATTERN_LETTER_START) | |
| var lend := ord(PATTERN_LETTER_END) | |
| if letter >= lstart and letter <= lend: | |
| return lerpf(0.0, 2.0, float(letter - lstart) / float(lend - lstart)) | |
| else: | |
| return 1.0 | |
| func _ready() -> void: | |
| if not Engine.is_editor_hint(): | |
| _base_energy = light_energy | |
| _set_energy = _base_energy | |
| _update_pattern_timer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment