I have this object reference for a carrot that I want to use so I can use the variable has carrot, but I got the error that it accessed None, so I set it up to only work when valid but now it does nothing because it is still none.
I know I have to set the reference here, but the carrot object is spawned in the game when the player presses a button, so I can’t select it because it doesn’t show up in my map until later, is there a way that I can go about either selecting it, or changing the code so I can avoid this issue altogether?
Somewhere in your BP you spawn carrot. Return value from that spawn node is the actual reference to that single Carrot Actor. So you can set it there, it would look more or less like this:
Assuming the script you’ve shown is inside the player character: Get Player Character → Cast To your character, set Carrot Reference.
Thank you! How would I then be able to use this variable in the other blueprint?
Avoid pulling data out of widgets, it leads to some bad design choices in the long run.
A neat way to do it all is via a dispatcher that you register once when the game starts. And every time a button is pressed, a reference is set automatically. For this, however, we’d need to know, where, when and how the widget gets created. Tbh, there is generally no reason to even spawn the carrot in the widget… I’d dispatch a click and spawn it where it needs to be referenced.
Ah well that sounds a lot more complicated than I anticipated, and the script I showed is actually inside another actor in the scene that the player can interact with. The widget gets created when the player interacts with a fridge (this script is inside the fridge actor)
Admittedly, this will be a short lived solution once things get more complicated and a major source of banging one’s head against the wall. On the other hand, it you keep things really simple, it might be fine if a wee bit tedious to maintain.
Thank you! I’m trying this at the moment, sorry for the following, stupid questions, for the SpawnItem custom event, would this be a custom event or when the player interacts? And in the fridge set up, is the variable from promoting Return Value to a variable?
Just a regular custom event that something else will call. This event will listen to the broadcasts of the widget buttons.
And in the fridge set up, is the variable from promoting Return Value to a variable?
Right clicking data pins gives you an option to automatically create a variable of the correct type. Rather than clicking 6 buttons and finding stuff on a long lists, you can promote it:
But you can, ofc, create the variable manually. It’s just a lot of clicking.
Thank you! I’ve got the new script set up now, but I’m running into an issue as when I cast to the player it says it does not inherit from player controller and the items are no longer spawning in