My Mega Challenge

Here is my mega challenge.
3 weapons available : rifle, launcher, flamethrower with limited amount of ammos each.
Possibility to reload ammos.
Pause/quit menu.

Suggestions are more than welcome.

7 Likes

Great job, could you give me a hint/code example on how to use the ActiveIndex for weapon switching.

Hello,
I decided to define and use 3 different arms: rifle, launcher and flame thrower.
In ShooterCharacter.h I defined GunClass and Gun as array, in addition I defined a new array called Ammos, this array contains the quantity of ammos for each arm.

	UPROPERTY(EditDefaultsOnly) 
		TSubclassOf<AGun> GunClass[3];
	UPROPERTY()
		AGun* Gun[3];
	UPROPERTY()
		TArray<int32> Ammos = {100,3,3};

Then in BP I associated the mesh of each arm.
In ShooterCharacter.cpp I spawned the 3 arms:

void AShooterCharacter::BeginPlay()
{
	Super::BeginPlay();
	Health = MaxHealth;
	int32 i = 0;
	GetMesh()->HideBoneByName(TEXT("weapon_r"), EPhysBodyOp::PBO_None);  
	for (i = 0; i <= 2; i++)
	{
		Gun[i] = GetWorld()->SpawnActor<AGun>(GunClass[i]);
		if (Gun[i])
		{
			Gun[i]->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
			Gun[i]->SetOwner(this);
			UE_LOG(LogTemp, Warning, TEXT("Attached mesh number %d"), i);
			if (i != ActiveIndex)
				Gun[i]->SetActorHiddenInGame(true);
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("No Gun Attached on %d"),i);
		}
	}
}

Finally I defined the function ChangeWeapon that you can associate to any key you want :

void AShooterCharacter::ChangeWeapon()
{
	ASimpleShootrePlayerController* OwnerController = Cast< ASimpleShootrePlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
	Gun[ActiveIndex]->SetActorHiddenInGame(true);
	ActiveIndex++;
	ActiveIndex = ActiveIndex % 3;
	Gun[ActiveIndex]->SetActorHiddenInGame(false);
	if (ActiveIndex == 0)
	{
		DamageValue = 10.0;
	}
	else if (ActiveIndex == 1)
	{
		DamageValue = 50.0;
	}
	else
	{
		DamageValue = 50.0;
	}
	OwnerController->ShowWeapon(ActiveIndex);
}
1 Like

Grazie mille.

1 Like

Nice work, my suggestion align aim crosshair!

I finish today course and I go make the mega challenge! =)

1 Like

I see you hid the actor in game and at the end you use ShowWeapon(ActiveIndex). What is ShowWeapon()'s code? I can’t manage to implement it.

1 Like

Here it is :

void ASimpleShootrePlayerController::ShowWeapon(int32 WeaponNum)
{ 
   if (WeaponNum == 0)
   {
   	if (ActiveWeapon == 1) LauncherIcon->SetVisibility(ESlateVisibility::Collapsed);
   	if (ActiveWeapon == 2) FlamethrowerIcon->SetVisibility(ESlateVisibility::Collapsed);
   	RifleIcon->SetVisibility(ESlateVisibility::Visible);
   	if (RifleSound != nullptr)
   		UGameplayStatics::SpawnSound2D(GetWorld(), RifleSound);
   }
   else if (WeaponNum == 1)
   {
   	if (ActiveWeapon == 2) FlamethrowerIcon->SetVisibility(ESlateVisibility::Collapsed);
   	if (ActiveWeapon == 0) RifleIcon->SetVisibility(ESlateVisibility::Collapsed);
   	LauncherIcon->SetVisibility(ESlateVisibility::Visible);
   	if (LauncherSound != nullptr)
   		UGameplayStatics::SpawnSound2D(GetWorld(), LauncherSound);
   }
   else if(WeaponNum == 2)
   {
   	if (ActiveWeapon == 0) RifleIcon->SetVisibility(ESlateVisibility::Collapsed);
   	if (ActiveWeapon == 1) LauncherIcon->SetVisibility(ESlateVisibility::Collapsed);
   	FlamethrowerIcon->SetVisibility(ESlateVisibility::Visible);
   	if (FlamethrowerSound != nullptr)
   		UGameplayStatics::SpawnSound2D(GetWorld(), FlamethrowerSound);
   }
   ActiveWeapon = WeaponNum;
}
1 Like

Thank you Luise. Did you have any trouble with crashes? I made a gun array like you did here

UPROPERTY(EditDefaultsOnly)
TSubclassOf GunClass[3];
UPROPERTY()
AGun* Gun[3];

but unreal crashes every time. It works for a few seconds than crashes. I can spawn both weapons without making the array, but when I try to assign both of them it sais in the log unable to spawn actor 0, no actor class assigned. Any tips?

Hello Alex,
check your code, you missed AGun>in the definition of the vector GunClass[3].
It should be:

UPROPERTY(EditDefaultsOnly) 
		TSubclassOf<AGun> GunClass[3];
	UPROPERTY()
		AGun* Gun[3]

Then do not forget to assign the mesh of each arm in BP

I have managed to progress a little. I can spawn and swap between 2 weapons at the moment, but when I change to the second weapon (I’ve used the Launcher mesh from the folders), since it’s bigger than the rifle it spawns and overlays it. I need a comand to delete the previous weapon actor. I’ve tried Destroy() but it deletes the entire array. Is there any command that deletes or hides between them. This is the code I’ve got:

Gun[i] = GetWorld()->SpawnActor<AGun>(GunClass[i]);
	GetMesh()->HideBoneByName(TEXT("weapon_r"), EPhysBodyOp::PBO_None);
	if (i == 0)
	{
		Gun[0]->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
		Gun[0]->SetOwner(this);
		Gun[1]->Destroy(GunClass[1]);

Is this that I need to change

Gun[1]->Destroy(GunClass[1]);

Does anyone have any tips?
I’ve tried with blueprints, but some of the commands are different and I’m really stubborn to do it in code.
I could also work by hidding the socket, but I can’t find any commands for that either.

Try to hide the actor instead of destroying:

Gun[1]->SetActorHiddenInGame(true); 
1 Like

I’ve finally did it! Thank you, Luise! It’s different than your code, but it works! :grin:

1 Like

This is looking really good! :smiley:

1 Like

What do you do to make the Shooter Character Spawn muiltiple unique weapons instead of multiple identical weapons?

What do you mean by this quote?

Do you have more than one Blueprint for weapons?

Edit:
Found out on my own that making muiltiple Blueprint Classes derived from the Gun c++ class was the way to make new weapons - questions answered.

1 Like

This looks good! Nice job with the flamethrower! :slight_smile:

Privacy & Terms