Announcement

Collapse
No announcement yet.

Need a few basic coding tips

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Need a few basic coding tips

    So, quick preface: I learned coding in Game Maker, moved to Unity years later, and now I'm gunning for Unreal. C++ is giving me a lot of trouble but I'm starting to catch onto some key concepts which I'm finding simply aren't explained anywhere.

    Like, initially I didn't realize that InputComponent control bindings were basically setting up a function that's called when that input is recognized (that's what it does, right? I'm following a lot of tutorial/example code and it's really hard sometimes just to tell what I'm looking at), and that trying to #include additional things wouldn't enable me to use that if the parent class isn't a Character. I'm still not 100% sure how I'm "supposed to" handle components, code-wise. Movement goes on the Character class, then if I want to add something like a weapon system I'd add an ActorComponent for it? If I choose the wrong kind, do I have to go through the trouble to erase it completely and remake it with a different parent class?

    Right now, I've managed to make a PlayerMovement Character class and add (empty) functions for x/y movement and camera control, and bind them to inputs. And it took me hours to do that without Visual Studio hitting me with errors. Headers seem annoying to deal with, but they're starting to make some sense.

    Now, I want to bind vertical camera movement basically 1:1 to the gamepad's Y axis. That is, I don't want to "add" vertical movement to the camera; I want to set it directly. What would be a good way to do this?

    I figure I want to bypass AddControllerPitchInput(), forget about AddPitchInput(), and directly assign a value to RotationInput.Pitch in PlayerController.cpp. I can't tell if this is possible, though. And if it is, how would I gain access to that variable and change it from my script?

    I still feel like I'm missing some really basic, key understanding here which would go a long way toward helping me feel comfortable working in C++ the way I do in Unity's C#. But I'm determined to see this through, and I really don't want to dig into blueprints. Any pointers?

    (Just want to point out because this feels like a very low level plea for help, I have read through several beginner coding tutorials at this point. A few have helped, but many of them were total dead-ends for me. It seems there are a lot of concepts they just don't explain, which means the tutorials are just rote code copying or trying to teach basic game logic concepts I'm already pretty well aware of given my prior experience. The help I need is unfortunately almost impossible to find through a quick search.)

    #2
    So first thing to consider is the the default Character that comes with UE4 is a very complex beast. It's movement system is very complicated and hard to follow even for people who have been working in the engine a long time. There's a lot of code there to make it do what it does.

    If you just want some simple movement in for now, you probably want to look at creating a default 'Pawn' class (which is an object that can be 'possessed' by a player), and giving that object a 'PawnMovementComponent'. The MovementComponents have a bunch of functions that you can override to get them setup quickly with your Pawn class, like setting the updated component etc.

    In terms of the actual movement logic, I did something fairly similar in my Hovertank game. Strafing, Thrusting and Steering are all constantly applied while the player is inputting movement. Pitch however has limited up / down and I wanted it to be bound to the 'range' of possible inputs.

    What I do is store a cumulative value called 'PitchVal' in the object, then add and subtract the delta from the mouse to that value. My input function looks something like this (I don't have the project on this machine atm):

    Code:
    void UBZGame_HoverMovemementComponent::OnPitchInput(float Val)
    {
    	PitchVal += Val * PitchSensitivity;
    	PitchVal = FMath::Clamp(PitchVal, PitchMin, PitchMax);
    }
    Relatively simple code but works well. It consistently adds 'value' to Pitch until it reaches its maximum, but it's not bound to the maximum up / down range of the mouse (which is infinite, but on a gamepad it's limited).

    Does this help at all?

    In your case, you can get the 'Val' the same way as above, but instead map that value to a range of possible locations for the camera. For example:

    Code:
    void UBZGame_HoverMovemementComponent::OnPitchInput(float Val)
    {
    	const float CameraLocationZ = FMath::GetMappedRangeValue(FVector2D(-1.f, 1.f), FVector2D(CameraMinHeight, CameraMaxHeight), Val);
    
    	const FVector CameraLocationWS = FVector(0.f, 0.f, CameraLocationZ);
    }
    Last edited by TheJamsh; 08-18-2015, 08:36 AM.

    Comment


      #3
      Thanks for the response! I'm not quite sure how to override functions, but maybe this is pretty similar to what I was looking to learn in the original post.

      So, if I make a Pawn class, that has access to all variables and functions in the pawn.cpp file by default? Do I need to go through any extra steps to access them or can I just call them as if they were declared in my custom class? As for overriding functions, I think I've seen those so I can guess as to how they work. If I override a function, that's basically replacing the original (say, in pawn.cpp) with my version? Or does it add that functionality to the original?

      Just for clarity's sake, in my original post, I think the pitch input variable I wanted to access was in the character controller (somewhere around line 4400, ouch), so would I have been able to assign a value to that since I had set it up as a Character class? I'm assuming the GENERATED_BODY() in the header effectively slaps the entire contents of the original class type in there somehow.

      One last little question, I think at some point in my fumbling I managed to cause a bunch of new errors to pop up when I added an extra #include in my class, which I had pulled from a different class type. I think I grabbed #include "GameFramework/Character.h" from a Character class and put it in an ActorComponent. I guess it would be handy to know how #includes work in Unreal/C++.

      Comment

      Working...
      X