Search Unity

Call action by numer or id

Discussion in 'Scripting' started by gtarnowskiweb, Jan 19, 2020.

  1. gtarnowskiweb

    gtarnowskiweb

    Joined:
    Aug 16, 2018
    Posts:
    9
    Hello.
    I have a game object prefab. It's a Terminal that allows Player to call an action to launch a specific effect, like door openings, adding items to inventory, hiding other objects, etc. My idea is to call a specific action/task by ID attached to each terminal. Id will be uniq. But I dont know how to call specific action, because it will be custom one.
    What should I do? Add to each terminal different script/action? How to call it?
    Im attaching a link to basic diagram that represent my point of view
    https://docs.google.com/drawings/d/13S0m0LQ-eAMPSUtSuAT7qZonNeMmuCSuaqwTOZWaLNA/edit?usp=sharing
     

    Attached Files:

  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    You could make a class with a
    public List<UnityEvent> actions;

    You can configure the actions then in the inspector and if you need action 1 you get from that event class action[1] and run it with Invoke().
     
    gtarnowskiweb likes this.
  3. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    224
    Well a bad but easy way is to let your Terminal call SendMessage() on target gameobjects. So if the player wants to open a door, they interact with the specific terminal, and that terminal has a reference to the door gameobject and calls targetGO.SendMessage(actionString) where actionstring is set via the inspector in the scene, and in this case is "OpenDoor". Now any scripts on the door gameobject that have a public function called OpenDoor will be run. Like I said its bad, but easy.

    A less bad way would be to have different components also on the Terminal. When the player interacts with the terminal, it uses GetComponents<TerminalAction>() to go through and do all the actions. So OpenDoor would inherit from TerminalAction, and TurnOffLights too, and implement their functionality in an overridden OnTerminalInteracted() function. Or you could use an interface. Or use events... lots and lots of ways to do it.
     
    gtarnowskiweb likes this.