Advertisement

Moving another actor on keyboard input

Started by August 17, 2017 08:47 AM
4 comments, last by Net_ 7 years, 3 months ago

Hi all,

i'm just starting to use and learn UE4 for game development. I have a lot of question of course but at the moment i'm stuck with some essential one.

Just to mention that i come from Unity background so if i think in Unity way, please correct me :) .

 

Any way, my problem is with getting input from keyboard and moving another actor (not the possessed pawn which is my main player). I have a simple cube as main player that is possessed and can get input from keyboard and act upon it. I have simple sphere as the ball in the game that can bounce around and collide with the player and the walls. I implemented when the ball hits the player, the ball will stop simulating physics and the her world position will be the same as the cube position, i.e. the cube will "carry" the ball around. Now i want to stop "carrying" the ball when i press the spacebar on my keyboard. My cube (player) class is ACollidingPawn class (.h and .cpp) and my ball class is ABall (.h and .cpp). In ACollidingPawn class i can react to player inputs and do whatever i want on the cube actor (in ACollidingPawn class). But i want to do something with the ball actor when i press the spacebar on my keyboard, i.e. need a way to do some logic in ABall class...

 

In essence, i want to "do something on the ball object" after the keyboard was pressed without possessing the ball object. ( as i understood, you can only process input on possessed actor)

 

I hope you understand my problem :)

 

Thanks in advance!

Im newbee with UE4, but I think you are right with the stuff related to possessing actors. The thing is that in the human player entity ingame is the PlayerController, and it posseses actors as will, therefore all the input is received and managed by the PlayerController/InputManager. The thing you could do is:

  1. When the collision is detected, store the ball actor in a pointer in your character class
  2. Create a function in your ball class with the desired behaviour
  3. Create a wrapper function in your input manager which calls function created in 2
  4. Config the input key and bind the function in 3 in your inputmanager
Advertisement
56 minutes ago, NajeNDa said:

Im newbee with UE4, but I think you are right with the stuff related to possessing actors. The thing is that in the human player entity ingame is the PlayerController, and it posseses actors as will, therefore all the input is received and managed by the PlayerController/InputManager. The thing you could do is:

  1. When the collision is detected, store the ball actor in a pointer in your character class
  2. Create a function in your ball class with the desired behaviour
  3. Create a wrapper function in your input manager which calls function created in 2
  4. Config the input key and bind the function in 3 in your inputmanager

Tnx for the answer, nice idea. 

But, is it possible to do it without having a reference of the ball in my player actor.

How is intended in UE4 to receive the input and do some functionality in other actors or components. In Unity, one can check if any button is pressed by using Input object (something like Input.GetKeyDown("space")) and if so do the functionality on any script on any gameobject. How can something like that be implemented in UE4? With delegates and events? 

1 hour ago, pele90 said:

Tnx for the answer, nice idea. 

But, is it possible to do it without having a reference of the ball in my player actor.

How is intended in UE4 to receive the input and do some functionality in other actors or components. In Unity, one can check if any button is pressed by using Input object (something like Input.GetKeyDown("space")) and if so do the functionality on any script on any gameobject. How can something like that be implemented in UE4? With delegates and events? 

As I told you before, I am probably not the best to speak about this because of my lack of experience with UE4, so take my replies carefully :D

The basic input flow is the following one:

  1. Create a function to handle the key input, for example "Jump" or "SpaceBarFunc" (check UE4 default InputManager).
  2. Do stuff inside that function
  3. Bind the function with the input event

So having this in mind, you could register your delegates in construction phase and then trigger an event inside #2 step function.

About the Unity comparation, there is no direct matching for that because getting the input in Unity is something global, but in UE4 looks like is not.


InputComponent->BindAction("YouAction", IE_Pressed, this, &YourPawn::YourFunction);

That line of code is step #3, where you tell to the input system that YourPawn::YourFunction has to be fired when YourAction key is pressed. The problem is that your mind is not thinking in the engineer properly way, because Unity allows you to catch input strokes everywhere, but that is terrible. You should build an extra layer based probably in "Command" pattern to manage the input, and that is the UE4 InputManager, key bindings and delegates.

You can get a reference by iterating through the actors in the world, but I would probably pass the cube a reference to the ball in the level blueprint. From there, use delegates or multi-cast delegates to call your functions wherever you need them.

This topic is closed to new replies.

Advertisement