I was wondering if anyone can take a look at my bluprint scripting and tell where I went wrong.
I have a For Sale widget that pops up with a BeginOverlap when the I approach a building and then closes when I move away from the building with an EndOverlap, which all works fine.
Now, the For Sale widget has a BUY button, which when clicked it closes the For Sale widget and pops up a new Player Farm widget. And it also sets a playerFarmOwnership boolean variable to True, so that when the character approaches the building after he bought the building the PlayerFarm widget would pop up, instead of the For Sale widget. I’ve tested to see if each of the widgets pop up when they are supposed to and they work fine.
But the problem I’m having is that after I click on the BUY button and the Player Farm widget pops up, that Player Farm widget won’t close when I move away from the building and I get an error message, which I can’t understand. The For Sale widget closes fine when I move away. And if I manually set the playerFarmOwnership variable to True as the default, the Player Farm widget closes just fine when I move away from the building. It just doesn’t want to close when the click of the BUY button sets it to True.
Please take a look at the photos and the error message below and let me know where I went wrong.
In the above function, the For Sale widget closes and is replaced with the Player Farm widget and the playerFarmOwnership variable is set to True.
In the below function, it opens either of the two widget depending on whether the playerFarmOwnership variable is set to True or not. It is also aimed at closing whichever of the two widgets is currently displayed, but the Player Farm widget won’t close on EndOverlap, while the For Sale widget does.
And this is the error message I get.
Blueprint Runtime Error: Accessed None trying to read property CallFunc_Create_ReturnValue2 from function: ‘ExecuteUbergraph_bp_smallFarmPlot’ from node: Remove from Parent in graph: EventGraph in object: bp_smallFarmPlot with description: Accessed None trying to read property CallFunc_Create_ReturnValue2
Store dynamically spawned objects in a variable. If you do not, they will get Garbage Collected. To do it easily, disconnect the return value from CreateWidget, right click the pin and PromoteToVariable. Use that variable to manipulate the widget.
I am still getting the same failed result as before. Can you please check below to see if I did correctly what you told me to do?
Could you try doing a separate cast in EndOverlap as well? I think that accessing PlayerFarmOwnership like this will always return False.
Apart from that it all looks fine, if it still does not work, please do not hesitate to let me know.
I’ve tried doing a separate cast in EndOverlap. Please check below to see if I did what you meant.
The Player Farm widget still won’t close when I move away. I still get the same kind of error. The error that I get points to the Remove From Parent function connected to the Player Farm variable.
This is the error I get in the message log:
Blueprint Runtime Error: Accessed None trying to read property w_playerFarmVar from function: ‘ExecuteUbergraph_bp_smallFarmPlot’ from node: Remove from Parent in graph: EventGraph in object: bp_smallFarmPlot with description: Accessed None trying to read property w_playerFarmVar
I think it has something to do when with the character still being within the trigger box when I click on the BUY button. Player Farm widget doesn’t seem to want to close after I click BUY and then leave the trigger box. As I mentioned before, when I manually set the playerFarmOwnership variable to True, Player Farm widget opens and closes when the character enters and leaves the trigger box. Just not when the BUY button sets the variable to True.
Accessed None errors mean that, at some point, the variable you stored the widget in became invalid.
I think you might be accidentally creating multiple widgets when you enter the trigger, and try to remove them more than once when leaving the trigger. Could you please put a PrintText node after OnComponentBeginOverlap and OnComponentEndOverlap to see how many times it fires?
In addition, before creating a widget, you could check whether it already exists and create it only if it does not:
Essentially, what happens is that you enter the trigger, create a new widget and store it in the variable. You then leave the trigger and remove the widget from the viewport but this does not destroy the widget. It is still valid in the memory as it is being referenced by the variable. Next time the overlap triggers, you create a brand new widget and store it in the variable. The old widget that had not been destroyed is still not in the viewport and is now no longer referenced, meaning the it is now flagged for garbage collection and trying to access it is a bad idea.
First of all, make sure that the triggers only fire once when you enter/leave. If they only fire once, then ensure you check if the widget you’re about to create does not already exist. If it does, do not create it again, but simply add it again to the viewport instead - like in the screenshot above.
Let me know if it helps.
I didn’t know about the ?IsValid tool. Thank you.
The triggers are firing once when I enter and once when I leave.
I hooked up ?IsValid to the create widget in the OnComponentBeginOverlap trigger function and the same create widget in the OnClicked BUY button function.
When I enter the trigger box, OnComponentBeginOverlap fires once.
When I click the BUY button whilst within the trigger box, IsNotValid executes which pops up the Player Farm widget.
When I leave the trigger box, OnComponentEndOverlap fires once, but the Player Farm widget isn’t removed.
Then I try re-entering the trigger box with the widget still on my screen, OnComponentBeginOverlap fires once and IsNotValid executes. This is where it’s meant to open the Player Farm widget, but the widget was still open as it failed to close before.
When I leave and try re-enterering again IsValid executes. Each time after that when I re-enter, IsValid always executes. But always with the Player Farm widget on my screen. And OnComponentBeginOverlap and OnComponentEndOverlap each only fire once.
When I leave the trigger box,
OnComponentEndOverlap fires once, but
the Player Farm widget isn’t removed.
I am assuming that at some point you do click the Buy button; doesn’t it create a second, identical widget? It is referenced by the NewVar0 in W-forSale? and part of the viewport. You never remove this one.
Also, the RemoveFromParent in W-forSale should be behind everything else, otherwise, you’re risking the rest of the code not being executed.
It doesn’t create a second widget on the screen when I click the Buy button. But it does create a second widget when I re-enter the trigger box with the widget already open. I’ve a added an exit button on the widget, which I have to click twice to close it after re-entering the trigger box. But I only need to click exit once to close the widget that was opened by the Buy button function.
The W_playerFarm widget is referenced by the NewVar0 in the W-forSale Bluprint (it’s the variable promoted from Create W_playerFarm Widget from within the W-forSale Bluprint, so I believe I referenced correctly, unless I’m mistaken).
But, as you said, I never removed this one. Or, at least I haven’t removed it from within the W_forSale Bluprint. I assumed that the OnComponentEndOverlap function in bp_smallFarmPlot, would close the same W_playerFarm widget that I opened in the W_forSale Bluprint graph. Should I do a separate Remove from Parent for the W_playerFarm widget in the W_forSale Bluprint? If so, how do I close that widget that I opened by the Buy button function in the W_forSale Bluprint by using the OnComponentEndOverlap in bp_smallFarmPlot ?