Good evening;
I would like to turn on and off a lamp from a button in an inventory.
I placed a reference of the lamp in the Widget but there is a mistake, I do not understand there what. Could you tell me what is wrong?
I first added a Bleuprint to the “light point”, then in the Widget I created a variable “type object” in which I referenced the “Lihgt point” that I named “Lumière Rouge”.
Is there a better way?
You only created a variable that will hold the reference. You haven’t set the reference yet.
For example, here I’m assigning the variable Lamp (in your case would be Lumière_Rouge) to a BP_Lamp object (in your case it would be PointLightRouge), right after creating my widget in the gamemode:
You could always leverage a blueprint interface, and just connect the OnClick to the “lights on” interface call…
Further, you could modify the interface call to check that Self is the the same as the requested reference, so that you could potentially select different lights to toggle with different buttons.
Thank you Mosthost LA for your intervention,
But… in my state of “newbie”, without a concrete example for me all this remains absolutely abstract.
I’m buying an online teache
Perhaps you could share more context here? Choosing a communication method will depend on what you’re trying to achieve.
does every lamp have its own widget button?
how does the user choose which lamp to switch with the button?
or do you literally have 1 lamp and 1 button and that’s it?
It would also help if you told us where you create the widget and how the lamp ended up in the scene - did you drag an actor with a point light into the scene from the Content Browser?
I am aware that I am leaving you in the interrogation, so I will try to enlighten you as best I can.
I engaged in a small learning exercise, to understand simple principles, the goal being to master the inputs and outputs of variables in the Bleuprint.
For the moment I have placed three lamps and three buttons that are used to turn these lamps on and off, plus a fourth button that displays a number. All this in an inventory Widget.
First of all. If you ever feel that you need to use Get All of Something nodes, you’re making a mistake in 99.99% cases. It has its uses but avoid using it unless you understand what it does and what it’s used for best. It leads to bad practices if used for getting references.
[HR][/HR]
So you call Create Widget in the Level Bleurpint and the lamps are in the level as well. In this case it’s all fairly straightforward:
You do not need to do anything in the widget. Remove whatever your buttons do. And instead implement the above.
You can get a reference to your Lamp by selecting it in the scene and drag-dropping it into the graph; or select in the scene and right click in the graph -> Create Reference to Lamp. The above will bind the button’s onClick event and let it toggle the light.
When you do not care about the order of elements. Maybe you want to check how many enemies remain in the level. Or you want to destroy all traps. Or your backpack falls into lava and you lose all items. Still, there’s usually a more efficient way to keep track of things but GetAll can be a (clunky) shortcut.
Folks sometimes use GetAll when they are 100% sure there is only 1 thing they need to get, it’s lazy but hey, it does work, indeed.
Also, read the tooltip of that node, it comes with a warning.