Hey, I have a crouch system so if you press left ctrl the player crouches, and I’ve set up an image in my game mode widget blueprint (which also includes health bar etc) but how do I connect it so when the player crouches it displays the image? Thanks in advance
Use a bind function. When the UMG is initialized you can get a reference to your pawn and store it as a variable of whatever class your pawn is (i.e. my character class). Then in your new binding function get the pawn variable, and get the crouched toggle bool from it.
You now have the bool that you can do whatever method you prefer, but what I would do is pull off the bool pin and type “select”. This node is a life saver. At first it looks like it doesn’t do anything, but take its output and plug it into the return value of the return node of your function. You should see an option for true and false. Set the true option to your crouched icon and false to your standing one.
Thanks for the reply, okay I’ve started doing this, but I think I’m doing it in the wrong place. I’ve done this within the actual binding event (for the crouched icon behaviour) in the widget blueprint, also what do I connect the select to? Thanks
If you have the select node in the binding function, take the grey return value and put it into the brush (or whatever the output is for the function). If you give a screen shot of your binding I could show you.
Edit: which setting did you use a binding for? I don’t think a bool should be the output for the function. It should be a brush or image.
The output of the binding function will match what it is binding. So if you bind the color, the color will be the output. If you bind the brush, a brush will be the output. And be sure to plug in the output of the select node first so the inputs will be correct.
Also, I had both icons hidden to start and they never reappeared, and when visible they both just stay there permanently. (In the bottom right above the ammo count) The health is currently the player standing icon, and the crouch icon (the icon I’ve binded) is now what appears to be a slice of battenberg for some reason below it. Sorry for this I’m not too good with blueprints haha
The crouched image has the image selected but is also binded, the binding is the blueprint you shown me (with the select) yet it comes out looking as the yellow and pink squared texture. They both are on screen constantly and do not toggle
You still want a single image widget, right? With this you will only need one.
The binding will be checking the bool, and as it picks it up it will set the image according to the select. This will essentially let one image widget use two images. I believe if you set the false option empty, there will be no image displayed (setting the visibility would be better for that though).
How are you assigning the “My Character” here, and what are your default values for the two image variables? The only reason I can see the weird checker texture coming up is that something isn’t assigned right.
The image is set here, I’ve chosen the crouched image and then I’ve binded it (pic 2) As you can see on the screen I’ve moved the health bar currently behind the crouched player symbol, that has no binding and I’ll probably delete it if not needed
The two images it’s selecting are the two image widgets that are on screen (this is why I’ve not deleted the health symbol in pic 1, as the standing icon is that). I’ve tried creating a variable called image and setting its default value as the image but no images show up, so as you can see in the picture below there is no default value even shown.
Oooooh, now I see what you did. This may be a bug in the editor. The StandingIcon and CrouchingIcon variables are image widget classes not an actual image. For some reason it was able to accept that as an input and that caused it too turn wonky and give the yellow and purple squares.
Also by assinging your character, I mean even though you have a variable for a character, it doesn’t mean it is holding anything. When you create the Game Mode Widget add a self node to the “owning player” input. Then in your UMG blueprint (yup, it has its own tick, begin play, and stuff for you to use, look look for the event graph tab) on the Event Begin Play event, get the owner and set that as the “My Character” (Cast to your character if need be). Now you will be referencing your pawn where as the variable was empty before.
Then make two variables (not widgets) like you would in any other blueprint and set its type to texture 2d, image, something along those lines (I’m stuck on a tablet and can’t open UE4 to check). Compile and you should be able to set their default values to your health icon and “Crouched”.
Notice how the options for your CrouchedIcon and StandingIcon have options but no default values. That is because it is referencing the widget in the UMG editor. Hopefully this makes more sense.
Okay that’s good to know, just two things I can’t do that you told me, firstly I can’t get a reference to self in the first person blueprint and insert it into the owning player of the game mode widget. Also I created those two images as texture 2d but they won’t go into the true and false of the select, if I drag of the texture 2d and make a select, it says “Option 0 + 1” instead of true and false, with an integer instead of a boolean index also
You might have to create the Game Mode Widget with a player controller class or HUD class, or check “expose on spawn” on your My Character variable.
If you use the expose on spawn method, refresh the “create Game Mode Widget” node and you should have a new “My Character” input. Self should fit there.
If you decide to use the player controller class to create the widget and plug that self in for owner. In the UMG blueprint you will need to do get owner -> get conrolled pawn -> cast to whatever your character class is.
Also, I can’t remember exactly the type of variable you need for images. Hover over the image input on the “Make Slate Brush” node. The note that pops up should by the type of variable it will be. You might need to make a new select node if it is behaving weird.
When you create your hud, you should set it as a variable in either the character or controller, I typically use the controller but wherever you are calling crouch with the ctrl btn should be fine, Lets call your hud var “MyHud”. Anyway, if you create a function in your Widget, lets say “SetCrouchImage”, and add a bool to the input panel of the function called “isCrouching”.
Then create the slate material similar to what you did in the top image but use the “iscrouching” in the select. Also, instead of setting variables for the images, you can actually just plug the select into the make slate brush image slot and select the images you want from the dropdowns on true and false. If you use vars you have to make sure they are set where the drop down will just pull the selected image.
Finally, drag out a getter to your “CrouchedIcon” image and drag off to set the brush. Plug in the make slate brush you just made. Now lets go to where you call the ctrl button Pressed Release node is.
When you press ctrl call the function “SetCrouchImage” from “MyHud” with “isCrouched” == true, on release call it again with it isCrouched == false. If you create and set “MyHud” in the controller and your “Crouch” btn is in your character, just cast to playerController>YourPlayerController>Get MyHud and call the function.
This way works well and is slightly more efficient since it is not calling the bind function every tick to set the image, just when pressed or released.
This is the illegal object pattern in Slate. You’ve used a bad object as a Slate Brush. From the looks of it, you’re feeding the slate brush an image widget. You need to use Texture2D, or MaterialInterface object.