I want to call SpawnActor for Character from UObject

When I call use game crashes

void UGunItem::Use(class AShooterCharacter* Character)
{
	
	if (GunClass)
	{	
		Gun = ()->SpawnActor<ABaseGun>(GunClass);
		if (Gun) 
		{
			UE_LOG(LogTemp, Error, TEXT("Kurba"));
		
			Character->Gun->AttachToComponent(Character->GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
			Character->Gun->SetOwner(Character);
		}
	}
}

I want to spawn weapon in hands of character.

Hello! Can you check this lines of code

     Character->Gun->AttachToComponent(Character->GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
     Character->Gun->SetOwner(Character);

Can it be, that it should be like that

     Gun->AttachToComponent(Character->GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
     Gun->SetOwner(Character);

Thank You!

I have another problem which is not related to this one, but I dont want to create another question. Please help me.

void UMagItem::Use(class AShooterCharacter* Character)
{
	UE_LOG(LogTemp, Warning, TEXT("%i"), BulletsInMagazine);
	
	if (Gun == nullptr)
	{
		UE_LOG(LogTemp, Warning, TEXT("Kurde bele"));
		return;
	}
	Gun->BulletsInGun = 0;
	switch (IsMagIn)
	{
	case true:


		break;
	case false:

		 int GunBullets = Gun->BulletsInGun;

		break;
	}
}

I declare gun in header just like that:

ABasePawn* Gun;

But it is nullptr.
How can i initalize this?

If you only declare pointer, then always set it to nullptr in header or Constructor for initialization purpose. As for setting value to it - it depends on what logic you are using, there can be many different approaches. For example some do the stuff like picking up mags = adding their params to Character data and destroying mag actor itself, so all info goes to Character.

Ok, so I should set ABasePawn* Gun to nullptr. But what should i pass into it in .cpp? Sorry Im really weak in terms of C++.

Its not like I don’t have my own ideas. I just want to see other people solutions to see how to make it better.
Pls help.

few methods:

  1. Get world from outer, when you create object you define it alter usually you put this it defines, if it’s actor you can get world instance
  2. Pass related UWorld* during creation of object and keep it in objects variable and use that pointer insted of (), that what components do in UE4 for example
  3. Per function you can also pass UWorld* as argument of function.

I recommend method 2.

Make sure that outer is actor in world or UWorld* so object get destroyed with the world

As I mentioned above - you should decide when to set it. There are lots of details that are connected with this decision and so there are many different possible solutions…

void AShooterCharacter::InitalizePointer(ABasePawn* Gun1)
{
Gun = Gun1;
}

Is this method of getting pointer is bad?

Im calling InitalizePointer from another class.

You reminded me about Paul Halmos phrase “The beginner should not be discouraged if he finds he does not have the prerequisites for reading the prerequisites.” Code isnt born in ideal and perfect shape, perfection is only the result of many steps. So, just be more practical on this - do the job as you can and at some point you will find better solution ))

Thank you.

Thank you.