x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Change the crosshair dynamically in the first person template

I'm trying to change the crosshair dynamically in the first person template. The idea is to change the crosshair when targeting an item or hide the crosshair when going into iron sights. I'm using the following as a test:


ABasicFPSHUD::ABasicFPSHUD()
{
    ABasicFPSCharacter* Character = Cast
  
   (UGameplayStatics::GetPlayerCharacter(this, 0));
    FString crosshairPath;
    TCHAR* something;
    if (Character != nullptr) {
        UE_LOG(LogTemp, Warning, TEXT("Charcter not null"))
        something = TEXT("/Game/FirstPerson/Textures/hand");
        UE_LOG(LogTemp, Warning, TEXT("the text is %s"), something)
    }
    else {
        something = TEXT("/Game/FirstPerson/Textures/FirstPersonCrosshair");
    }
    // Set the crosshair texture
    static ConstructorHelpers::FObjectFinder
   
     CrosshairTexObj(something);
    CrosshairTex = CrosshairTexObj.Object;
}

   
  

The problem is that the if statement is working and I would think that it would set the CrosshairTextObj to "/game/firstperson/textures/hand" but it falls back on the else statement for some reason and I have no idea why.

Is my approach to dynamically changing the crosshair wrong? What's the best way to do this in C++?

Product Version: UE 4.16
Tags:
more ▼

asked Dec 27 '17 at 08:49 PM in C++ Programming

avatar image

ThePerro
3 2 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Hey there, you are doing that initialization on the constructor and probably when he's executing that, the player hasn't been assigned with a character yet. You need to have 2 different variables for the crosshair and for the hand and on the DrawHUD function you do the cast and decide which texture to use.

more ▼

answered Dec 28 '17 at 10:47 AM

avatar image

xlar8or
6k 163 130 305

avatar image ThePerro Dec 28 '17 at 02:25 PM

Hi, do I guess I should be doing the check in DrawHUD function? Also, what I intend to do is have a boolean in my BasicFPSCharacter class that changes depending if the character's line trace is hitting an object to pick up (which is the hand texture instead of a cross hair). Do I need to have a tick function in the HUD class to always check the value of the boolean? I'm just wondering since I'm not sure if it would just automatically update the cross hair, as I want it to change back into a cross hair if the line trace is no longer hitting an pickup object

avatar image xlar8or Dec 28 '17 at 03:45 PM

DrawHUD is your tick function. So do the cast there and check the boolean value and draw the quad with the appropriate texture.

avatar image ThePerro Dec 29 '17 at 12:24 AM

Thanks for the tip, it worked!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question