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"

Get array index of a clicked object

Hi everybody, I'm making a simple project where you click on the cube, get the widget with three buttons with which you can change the color of the cube. I've set up everything and it's working fine with one cube, but once I add more cubes it doesn't matter which cube I've clicked on, the color will always be changing on the first one(or if I change the index value manually in the blueprint).

alt text

I figured out that the problem is in the Widget Blueprint where I reference the Cube

alt text

The circled value is the index of the Cube that will get it's color changed. My question is how can I get the index of the clicked cube so that I can put it into variable that will automatically assign that index to the Cube reference in the Widget Blueprint where I'm changing the color of the selected Cube?

Product Version: UE 4.21
Tags:
blueprint.png (56.0 kB)
blueprint2.png (389.2 kB)
more ▼

asked May 08 '19 at 12:16 PM in Blueprint Scripting

avatar image

geginator
27 1 1 4

avatar image Lardo Deepdelver May 08 '19 at 01:17 PM

Why would you want the index of the clicked cube, and not just get the cube you are clicking directly?

avatar image geginator May 08 '19 at 01:25 PM

I thought the only way for this was with Get All Actors node and then indexing them.

How do I get it directly?

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

1 answer: sort voted first

Just use the node "get hit result under cursor by channel" (needs a controller reference), break the hit result, cast it to BP_Cube, save the output as a variable. Pass the variable to your widget and you're done. If you need help with understanding casting, references or communicating with widgets you can check out these videos I created that will help you get a foundation for the things I mentioned. Specifically video #1 and #25 will be particularly helpful here.

https://www.youtube.com/playlist?list=PLyu-W38DvZhqByAoeva1jewB5s78zQHIy

more ▼

answered May 08 '19 at 02:30 PM

avatar image

Nebula Games Inc
5.4k 28 9 15

avatar image geginator May 08 '19 at 04:15 PM

I tried using this node and I don't really know how to work with it. I tried all options from Break hit and I got the names of the Cubes I'm clicking on but I can't use them as Index for GET since I need Integer. I also tried promoting them as Variable and calling them from the Cube Reference in the Widget BP and setting them as target but that didn't work either.

So I don't know how to make this work with my current set up. Here's what I have.

Widget BP:

alt text

Cube BP:

alt text

Here's my try with the nodes you suggested:

alt text

Thanks for the tutorial recommendation, I've already watched them and they really helped.

avatar image Nebula Games Inc May 08 '19 at 04:57 PM

Something like this

alt text

You don't script this in the cube BP, notice I am using the controller and an input event "left mouse click" to find a "hit" under my cursor, it returns an actor reference if you clicked on one (in your case a cube), if the actor is a cube not the floor or sky or something else, then you save the reference as a variable. Now depending on your setup and how the widget is created will determine how you feed that reference to the widget.

avatar image geginator May 08 '19 at 09:10 PM

I got it now. I started everything from scratch and created everything in Player BP. I referenced buttons in Player BP as well and then gave them OnClick there. It's messy, but tomorrow I'll figure out how to reference this in Widget BP.

Right now if I try to reference this Cube variable in the Widget BP I get nothing. But at least it's working in Player BP.

Thank you for your help.

(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