Skip to content

Commit

Permalink
Working symmetrically
Browse files Browse the repository at this point in the history
  • Loading branch information
EiTaNBaRiBoA committed Jul 23, 2024
1 parent a919da6 commit 4743015
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
87 changes: 61 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## SignalEvent - Enhanced Signal Handling for Godot

SignalEvent is a Godot plugin designed to streamline and secure your signal connections, making your code more robust and easier to debug.
SignalEvent is a Godot plugin that simplifies and enhances signal connections in your Godot projects. It provides type-safe and debug-friendly signal management with default arguments and easy disconnection capabilities.

### Features

- **Safe Connection with Default Arguments:** Connect signals with optional default arguments that are passed automatically on emission.
- **Simplified Disconnection:** Disconnect signals by callable or disconnect all callables from a specific signal with ease.
- **Debug-Friendly Emission:** Optionally enable debug messages to trace signal emissions, pinpointing their origin for efficient troubleshooting.
- **Argument Validation:** Ensures type safety by validating signal arguments against the connected method's signature, preventing runtime errors.
- **Safe Connections with Default Arguments:** Define default arguments for your signal connections, streamlining signal emissions and reducing boilerplate code.
- **Simplified Disconnection:** Disconnect signal connections by callable or disconnect all callables from a specific signal with ease.
- **Debug-Friendly Signal Emission:** Optionally enable debug messages that trace signal emissions, pinpointing their origin in your code for efficient debugging.
- **Argument Validation:** Ensure type safety by validating signal arguments against the connected method's signature, preventing runtime errors caused by mismatched types.

### Installation

Expand All @@ -26,26 +26,43 @@ signal no_param_signal
func _ready():
# Connecting with default argument and debug emission
SignalEvent.safe_connect_signal(argument_signal, func_arguments, [1], true)
argument_signal.emit([2, "Hello"])
argument_signal.emit([2, "Hello"]) # Outputs: 2, "Hello"
# Disconnecting a callable from a specific signal
SignalEvent.safe_disconnect_callable(argument_signal, func_arguments)
# Connecting with multiple default funcs
SignalEvent.safe_connect_signal(funcs_signal, func_arguments, [1, 2,"Hello"])
SignalEvent.safe_connect_signal(funcs_signal,func_optional)
funcs_signal.emit()
SignalEvent.safe_connect_signal(funcs_signal, func_optional)
funcs_signal.emit() # Outputs: 1,2,"Hello" and 1
# Disconnecting all callables from a signal
SignalEvent.safe_disconnect_all_callables(funcs_signal)
# Simple connection to a func with optional parameter
# Simple connection to a func with an optional parameter
SignalEvent.safe_connect_signal(no_param_signal, func_optional)
no_param_signal.emit(5)
# Disconnecting all signals that are connect to a specific func
no_param_signal.emit(5) # Outputs: 5
# Disconnecting all signals that are connected to a specific func
SignalEvent.safe_disconnect_all_signals(func_optional)
# Would not Emit
no_param_signal.emit(5)
# This emission won't reach func_optional because it's disconnected
no_param_signal.emit(5)
# Symmetrically connect signals and callables
var signals_to_connect = [argument_signal, funcs_signal]
var callables_to_connect = [func_arguments, func_optional]
SignalEvent.safe_connect_symmetrically(signals_to_connect, callables_to_connect)
# Emit the signals (now connected symmetrically)
argument_signal.emit([10, 20, "Symmetric"])
funcs_signal.emit()
# Symmetrically disconnect signals and callables
SignalEvent.safe_disconnect_symmetrically(signals_to_connect, callables_to_connect)
func func_arguments(a : int, b : int, c : String) -> void:
print(a)
Expand All @@ -56,36 +73,54 @@ func func_optional(a : int = 1) -> void:
print(a)
```

### API
### API Reference

#### `safe_connect_signal(signalRef : Signal, callable : Callable, defaultArgs : Variant = [], debugEmit : bool = false, connectFlags : int = 0) -> void`

Connects a signal to a callable.
Connects a signal to a callable with optional default arguments and debug messages.

- `signalRef`: The signal to connect to.
- `callable`: The callable method to connect.
- `defaultArgs`: An optional array of default arguments.
- `debugEmit`: Enables debug messages on signal emission.
- `connectFlags`: Optional connection flags (see Godot documentation).
- **`signalRef`:** The signal to connect to.
- **`callable`:** The callable method to connect.
- **`defaultArgs`:** (Optional) An array of default arguments to pass to the callable.
- **`debugEmit`:** (Optional) Enables debug messages on signal emission.
- **`connectFlags`:** (Optional) Connection flags (see Godot documentation).

#### `safe_disconnect_callable(signalRef : Signal, callable : Callable) -> void`

Disconnects a specific callable from a signal.

- `signalRef`: The signal to disconnect from.
- `callable`: The callable to disconnect.
- **`signalRef`:** The signal to disconnect from.
- **`callable`:** The callable to disconnect.

#### `safe_disconnect_all_callables(signalRef : Signal) -> void`

Disconnects all callables from a signal.

- `signalRef`: The signal to disconnect from.
- **`signalRef`:** The signal to disconnect from.

#### `safe_disconnect_all_signals(callable : Callable) -> void`

Disconnects a callable from all connected signals.

- `callable`: The callable to disconnect.
- **`callable`:** The callable to disconnect.

#### `safe_connect_symmetrically(signalsRef : Array[Signal], callables : Array[Callable], defaultArgs : Variant = [], debugEmit: bool=false, connectFlags: int=0) -> void`

Connects an array of signals to an array of callables.

- **`signalsRef`:** An array of signals to connect.
- **`callables`:** An array of callables to connect.
- **`defaultArgs`:** (Optional) An array of default arguments to pass to the callables.
- **`debugEmit`:** (Optional) Enables debug messages on signal emission.
- **`connectFlags`:** (Optional) Connection flags (see Godot documentation).


#### `safe_disconnect_symmetrically(signalsRef : Array[Signal], callables : Array[Callable]) -> void`

Disconnects an array of callables from an array of signals.

- **`signalsRef`:** An array of signals to disconnect from.
- **`callables`:** An array of callables to disconnect.


### Contributing
Expand All @@ -94,4 +129,4 @@ Contributions are welcome! Feel free to open issues or pull requests.

### License

This plugin is released under the MIT License.
This plugin is released under the MIT License.
1 change: 0 additions & 1 deletion addons/SignalEvent/SignalEvent.gd
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ static func _extractArgs(args: Variant) -> Array:
return [args]
static func _get_scriptName(signalRef : Signal) -> String:
if signalRef.get_object().get_script() == null:
var obj = signalRef.get_object()
return signalRef.get_object().get_class()
else:
return signalRef.get_object().get_script().get_path().get_file() # # get_script() returns node , get_path() returns NodePath, get_file() returns script name
Expand Down

0 comments on commit 4743015

Please sign in to comment.