How do I use GetActorsInSelectionRectangle when the user releases the mouse button?
GetActorsInSelectionRectangle is only allowed to be called inside the DrawHUD event of the HUD, so that prevents me from calling it from within my LeftMouseButtonRelease event inside the PlayerController. So how should I go on to get the selected units the moment the player releases the mouse button (finishing his selection)?
If I try to call GetActorsInSelectionRectangle from within my LeftMouseButtonRelease event I get the following message in VS:
Warning GameHUD_0 Canvas Draw functions may only be called during the handling of the DrawHUD event
What I ended up doing is to create a few methods in the HUD to enable an asynchronous way of handling the selection rectangle. The following is not the exact way it was implemented, but the general concept of it.
HUD->StartSelectionRectangle(mouseX, mouseY)
HUD->UpdateSelectionRectangle(mouseX, mouseY)
HUD->FinishSelectionRectangle(mouseX, mouseY)
HUD->GetSelectionResult()
When the player presses the mouse button, I would call HUD->StartSelection(mouseX, mouseY). This would set a flag inside the HUD telling it to start drawing a rectangle at the given mouse coordinates. The rectangle would be drawn once DrawHUD is triggered.
While the player is still holding the mouse button down (dragging it), every time
Tick() is triggered at the player controller, I would update the mouse position with a call to HUD->UpdateSelectionRectangle(mouseX, mouseY). This way, every time DrawHUD is triggered it would always redraw the rectangle considering the latest mouse position as it moves.
When the player finally releases the mouse button, I would call HUD->FinishSelectionRectangle(mouseX, mouseY). This would set a flag inside the HUD telling it to finish the selection rectangle. The next time DrawHUD is triggered, GetActorsInSelectionRectangle() would be called and the selection result would be stored inside the HUD itself.
The player controller would then be able to access that result by calling HUD->GetSelectionResult().