Box selection of actors

I’m planing to create a small game where the player can select created units (actors) on the map through the cursor/finger. Actually exactly like in games like Age of Empire etc.

What I currently have is only the visual “box” in the user interface. I have no idea at the moment how I can now select all actors that are within this box. Any ideas?

A function like 'Vector Between (actorVector, vector2, vector2` on a 2D map would already help a lot.

Ok, I got it.

What I did:

  1. Wrote a new c++ function at my GameMode class that tells me if a actor location is inside my selection box.

MyGameMode.h

#pragma once

#include "GameFramework/GameMode.h"
#include "Core.h"
#include "math.h"

#include "MainGameMode.generated.h"

/**
 * 
 */
UCLASS()
class AMainGameMode : public AGameMode
{
	GENERATED_UCLASS_BODY()

public:

	UFUNCTION(BlueprintCallable, Category = "Utilities")
	bool is2DVectorInRectangle(FVector2D Point, FVector2D RectangleStart, FVector2D RectangleEnd);

};

MyGameMode.cpp

#include "projectName.h"
#include "MainGameMode.h"

AMainGameMode::AMainGameMode(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
{
}


bool AMainGameMode::is2DVectorInRectangle(FVector2D Point, FVector2D RectangleStart, FVector2D RectangleEnd) {
	FVector2D min;
	FVector2D max;

	min.X = fminf(RectangleStart.X, RectangleEnd.X);
	min.Y = fminf(RectangleStart.Y, RectangleEnd.Y);

	max.X = fmaxf(RectangleStart.X, RectangleEnd.X);
	max.Y = fmaxf(RectangleStart.Y, RectangleEnd.Y);

	if (Point.X > min.X && Point.X < max.X) {
		if (Point.Y > min.Y && Point.Y < max.Y) {
			return true;
		}
	}

	return false;
}
  1. Create this blueprint:

In your HUD blueprint graph:

3 additional methods to your HUD blueprint:

In your controller blueprint graph:

Input settings:

And that’s how it looks like then:

PIcture are outdated