# \[Organizing My Code\] Where should Spawn functions be written?

**URL:** <https://forums.unrealengine.com/t/organizing-my-code-where-should-spawn-functions-be-written/32939>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [June 19, 2015, 2:34am UTC](https://forums.unrealengine.com/t/organizing-my-code-where-should-spawn-functions-be-written/32939 "2015-06-19T02:34:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitcode](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bitcode](https://forums.unrealengine.com/u/bitcode)\
**Post date:** [June 19, 2015, 2:34am UTC](https://forums.unrealengine.com/t/organizing-my-code-where-should-spawn-functions-be-written/32939/1 "2015-06-19T02:34:49Z")

</div>

I have a class AMainCharacter inherited from ACharacter. This is part of the constructor:

```auto

AMainCharacter::AMainCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	static ConstructorHelpers::FObjectFinder<UBlueprint> WeaponBlueprint(TEXT("Blueprint'/Game/FPS/Blueprints/Weapons/BP_Weapon_M4A1.BP_Weapon_M4A1'"));

	if (WeaponBlueprint.Succeeded())
	{
		WeaponSpawn = (UClass*)WeaponBlueprint.Object->GeneratedClass;
	}
}

```

And this is the BeginPlay() function:

```auto

void AMainCharacter::BeginPlay()
{
	Super::BeginPlay();
	
	FActorSpawnParameters SpawnParameters;
	SpawnParameters.Owner = this;

	SpawnParameters.Instigator = Instigator;

	AWeapon* Spawner = GetWorld()->SpawnActor<AWeapon>(WeaponSpawn, SpawnParameters);

	if (Spawner)
	{
		Spawner->AttachRootComponentTo(FirstPersonMesh, "Equip", EAttachLocation::SnapToTarget);
		CurrentWeapon = Spawner;
	}
	
}

```

This code works fine and as soon as I start the game I get a weapon in my character just like intended. However, since my little game is getting bigger, I want to organize the code. I don’t want to use my character class to spawn weapons around the map. Where should I put my spawn calls?

My first idea was to put them all in GameMode.cpp, but by doing so, how could I access my character Object (not class) from inside GameMode and spawn a weapon in his hands/inventory?

I just can’t find a way to make a pointer to my character object from inside GameMode.

Also, if GameMode.cpp is the wrong way of organizing Spawn functions, could you guys give me some ideias of how to do that?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f6530a3e1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f6530a3e1](https://forums.unrealengine.com/u/anonymous_user_f6530a3e1)\
**Post date:** [June 19, 2015, 2:54am UTC](https://forums.unrealengine.com/t/organizing-my-code-where-should-spawn-functions-be-written/32939/2 "2015-06-19T02:54:44Z")

</div>

What I do is put the code in the Pawn/ Character code in a function like SpawnWeapon(WeaponToSpawn, AttachmentSocket). If you are using multiplayer and servers then you’ll need access to remote procedure calls (RPCs) which I’ve found easiest to use the PlayerController for, so it makes sense to keep them all together.

If you want to make them spawn from a pickup or something, then I usually cast the intersecting actor to my pawn class and then call the spawn like:

ACustomPlayerPawn\* Player = Cast\<ACustomPlayerPawn\>( OtherActor) ;  
if (Player) { Player-\>SpawnWeapon(); }

---

<div class="post-metadata">

**Author:** ![bitcode](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bitcode](https://forums.unrealengine.com/u/bitcode)\
**Post date:** [June 19, 2015, 3:36am UTC](https://forums.unrealengine.com/t/organizing-my-code-where-should-spawn-functions-be-written/32939/3 "2015-06-19T03:36:45Z")

</div>

AusPaco thanks for the reply.

But I have another question: is there a way to get the AMainCharacter instance from inside MyGameMode? For example:

```auto

AMyGameMode::AMyGameMode(const FObjectInitializer& ObjectInitializer) : Super (ObjectInitializer) 
{
	DefaultPawnClass = AMainCharacter::StaticClass();

        AMainCharacter* characterPtr = GetMainCharacterInstanceAddress(); // I made up this function to give an example of what I want

}

AMyGameMode::BeginPlay()
{
      characterPtr->Destroy();
}

```

If I could do that, then I could interact with my AMainCharacter instance from inside MyGameMode, which would help in spawning items directly in the character’s inventory.

I’m just not sure I should make this interaction in MyGameMode or in another class.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f6530a3e1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f6530a3e1](https://forums.unrealengine.com/u/anonymous_user_f6530a3e1)\
**Post date:** [June 19, 2015, 5:19am UTC](https://forums.unrealengine.com/t/organizing-my-code-where-should-spawn-functions-be-written/32939/4 "2015-06-19T05:19:26Z")

</div>

The standard way to find an actor is to basically go through all actors of a certain class using the actor iterator until you find the one you want:

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

From the wiki:

```auto

void AYourControllerClass::PrintAllStaticMeshActorsLocations()
{
	//EngineUtils.h
	for (TActorIterator<AStaticMeshActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
	{
		ClientMessage(ActorItr->GetName());
		ClientMessage(ActorItr->GetActorLocation().ToString());
	}
}

```

Just replace AStaticMeshActor with your custom class.
