Dev Guide
WebsiteGitHubGitBook
  • 📝Dev Intro
    • Using GitHub
    • Using Unity
    • Licensing
    • File Naming & Organization
      • File Naming
      • File Organization
        • Object Type
  • 📡Networking
    • Introduction to Game Networking
    • FishNet Networking
      • Server
      • Client
      • Server RPC
      • ObserversRPC
      • Network Message
  • 🖊️Guidelines
    • The C# Style Guide
      • Nomenclature
        • Namespaces
        • Classes & Interfaces
        • Methods
        • Fields
        • Parameters
        • Delegates
        • Events
        • Misc
      • Declarations
        • One declaration per source file
        • General class structure
        • Access Level Modifiers
        • Spacing
        • Brace Style
        • Switch Statements
        • Language
        • Common Patterns and Structure
          • Applying attributes on all network related methods.
    • Code Design Definitions
      • Actor
      • System
      • View
      • Events
        • Event Bus
        • Action
      • System Locator
      • Tweening
    • Asset Criteria
      • External Criteria
        • Animations
        • Fonts
        • Models
        • Textures
        • Graphics
        • Audio
      • Importing Criteria
        • 3D Models
        • 3D Animations
        • Textures
        • Graphics
        • Audio
      • Internal Criteria
        • 3D Models
        • 3D Animations
        • Textures
        • Graphics
        • Audio
    • SS3D's coder good practices
    • Code design patterns
  • 📖Guides
    • Application Settings
    • Running the Project
      • Configure your firewall and antivirus
      • Building the game
      • Joining a server
      • Hosting a server
      • Setting up a dedicated server
    • Debugging SS3D
    • Maintainer Guide
      • Pull request review process
    • Testing SS3D
      • Assets audit tests
      • Edit mode tests
      • Play mode tests
        • Inventory test
        • Health tests
    • Working with animations
  • 🛣️Roadmap
    • Releases
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
Export as PDF
  1. Guidelines
  2. Code Design Definitions
  3. Events

Action

An Action is an event that depends on an object to exist.

The Action event should be used when we don't need something to be accessed globally, like a button being pressed, or when we need to listen from something from a specific object.

Declaring

Actions have to be declared as the first thing inside a object with the event keyword.

public class ButtonView : Actor 
{
    public event Action OnButtonPressed;
}

Only the object that declares the event can invoke it.

It is noted that you can use an Action to send data too:

public event Action<int> OnNumberUpdated;
public event Action<Item> OnItemUpdated;

Using an Action

Let's imagine you have a granade in your character's hand. Pretty common sight in a Low RP server. In the code, usually we would have something like:

public class Granade : Explosive 
{
    public event Action OnPinRemoved;
    
    public void RemovePin() 
    {
        // Do pin removal logic.
    
        OnPinRemoved?.Invoke();
    }
}

The OnPinRemoved event will be used to to tell listeners that subscribed to that granade's event that the granade's pin was removed. The relevance of that OnPinRemoved event is usually for UI related purposes, or running a local animation of a granade pin flying through your screen.

Something like:

public class GranadePinAnimationView : Actor 
{
    private GranadePinAnimation _granadePinAnimation;

    private Granade _granade;
    
    protected override OnAwake() 
    {
        _granade.OnPinRemoved += HandlePinRemoved;
    }
    
    protected override OnDestroyed() 
    {
        _granade.OnPinRemoved -= HandlePinRemoved;
    }
    
    private void HandlePinRemoved() 
    {
        _granadePinAnimation.RunAnimation();
    }
}

You might even think it would be easier to call it directly, and depending on your logic it might be, but we are assuming that in this case its better to keep the classes decoupled.

PreviousEvent BusNextSystem Locator

Last updated 2 years ago

Was this helpful?

🖊️