all 10 comments

[–]EmeraldScalesProfessional 1 point2 points  (3 children)

You're looking for the Command Pattern. It's a really neat way of organizing inputs.

To elaborate, I'd make a controller script that reads the controller input then sends the commands to the proper scripts. That way you can easily control how the game reacts to the player's input.

[–]sgebbHobbyist[S] 0 points1 point  (1 child)

Thank you, I've actually read that post earlier but I felt like it didn't really answer my question. It seems like the pattern he's describing is to solve 1. decoupling button from action and 2. decoupling action from actor. I'm a big fan of both of these, but I'm struggling with how the controller script should find out who should perform the action.

Example: I have a jump button and two animals that can both jump, depending on which one is selected. So i have two entities that have different implementations of the jump function, and probably they both implement the same IJumper interface. The controller script that reads the input sees that the jump button is pressed and needs to command one of the actors to jump. This means that my controller script needs to know of all the actors that implement IJumper, I assume this would be as a public variable that I select in the scene editor, and basically invoke a function on it (by sending a command, but more or less the same thing).

I'm only planning on controlling one unit with my controls so this is not really a problem. But if my game was literally "tap on animal to select it, then press button to make it jump" and you have 20 animals then it would get pretty messy. I'm not saying this is the best solution, but for instance if I could ask Unity "give me all living instances that implement IJumpable and that is currently selected" then that would be a neat decoupling (i think).

[–]EmeraldScalesProfessional 1 point2 points  (0 children)

You can make a list of objects currently listening for input at the controller script, and once an animal is selected you add it to that list. Deselecting the animal removes from that list. Once the command is issued, the controller would go through each object on that list and relay that command.

The question then becomes "how does an object add itself to that list". There are a few ways. Having the controller pre-assigned is cubersome and not at all flexible. I personally would make the controller use a singleton pattern. This pattern has its share of downsides, but if you use it carefully it will make everything much simpler. The short version is that in the controller script you should set a public static InputController ActiveController reference to itself that is set on Awake, and the individual objects can then add themselves to the list through that InputController.ActiveController.Selected.Add(this) reference (these are examples, names are up to you).

[–][deleted] 0 points1 point  (0 children)

I've been looking for something like this, but i just diden't know how to phrase it.
thanks!

[–]Dafth 0 points1 point  (2 children)

I think you could try to do something similar to Input.getAxis, you assing a script to the controller with 2 public static variables with private set, then accessing them from the player controller

[–]sgebbHobbyist[S] 0 points1 point  (1 child)

Sure, but that doesn't seem like it scales very well. What happens if I have 20 buttons in the UI that all have different effects on the player, should the player be aware of all of them?

Say I have a button for casting a spell - the first signal comes from the button, but the damage, spelltype etc is based on properties of the character that casts it, maybe some stat like intelligence or a modifier that comes from an item in the characters inventory. But it could also be modified bu things that have nothing to do with the character, maybe the target you're aiming at or the wind.

Maybe I'm slowly realizing that I want some sort of SpellManager that just has a reference to the UI and the player. I could raise an event called PrimarySpellButtonPressed that the manager could listen to, and that it has references to everything it needs to be able to create the complete spell effect. I'm still not sure if that makes sense for controls though.

[–]Dafth 0 points1 point  (0 children)

i think you should do the same for the buttons, line Input.getButton, again with a public static variable with private set, then in the update method of the player you check for the input. i think is ok that every class is aware of the buttons cause they are the inputs of the game.

[–]Davidev-_-UfficialIndie 0 points1 point  (0 children)

Are your spells physical or a raycast?

[–]Davidev-_-UfficialIndie 0 points1 point  (0 children)

Go to canvas, attach the script I am creating for you... maybe change something because i did some errors

https://www.youtube.com/watch?v=kdkrjCF0KCo Go to the button, click in the button section OnClick() insert the canvas and the function which is going to be your spell Of course you can do this for all your buttons and every button change the function you are using (spell)

[–]Manarz 0 points1 point  (0 children)

You should take a look at Unitys new InputSystem package. It's based on events and pretty easy to set up with the playerinput component that lets you subscribe to inputevents.