Engine crashes when using NewObject<>()

Hello guys.

I am going to spawn a default weapon into Weapon Inventory when the player starts the game.

This function (called at BeginPlay) makes my engine crash
![alt text][1]

NewObject is for UObject derived classes. It looks like uoure trying to spawn an actor so you would use…

GetWorld()->SpawnActor<AWeapon>(AWeapon::StaticClass());

Edit: An option then might be to make a UWeapon class that has a SpawnWeapon function you can call. That way you make a UWeapon, add it to your inventory,and when the player switches to it you can call

Inventory[index]->Spawn(parameters);

Actually I don’t want to spawn the actor in the game. Just want to create a new instance of AWeapon (like if it was a NEW operator, but in UE it’s forbidden)

I don’t believe that’s possible with actors or uobjects. You probably want to spawn it and turn off all static meshes and visibility, then store it in your inventory.

I do this
![alt text][1]

But when I print the inventory
![alt text][2]

The DebugMessage doesn’t appear. The if(Inventory[i]) returns false

I edited my original answer which might be more of the direction you want to go, but could you show me where you declare Inventory in your .h?

72548-1.png

Could you check to make sure that the weapon is actually getting spawned, and check to size after you call add through breakpoints?

The weapon hasn’t a mesh yet, it’s a C++ class.
Also, the Inventory.Num() returns 1

Second question is, is this multiplayer? If it is, if the server isn’t adding the weapon to the inventory,then it is likely being overwritten back to nothing

Yes, it’s gonna be a Multiplayer game.

Okay, then since we know it’s being added to the inventory is double check that the server is the one that’s adding the weapon and not the client, otherwise if inventory is replicated you’ll lose what you spawned,which will cause that if check to fail since it got replaced with an empty array from the server

I dont see a problem with doing it that way. In AMyProjectCPPCharacter::BeginPlay(), just do:

// In BeginPlay()
if(Role == Role_Authority)
{
    GiveWeapons();
}

AMyProjectCPPCharacter::GiveWeapons()
{
    if(Role == Role_Authority)
    {
        // Spawn Weapon
        // Add to Inventory
    }
}

This way, only the server is in charge of spawning the weapon and adding it to the inventory, which gets replicated to the player, so on the clients side they’ll have an inventory with that same weapon.

My goal is to add to the Inventory all the Weapons the player should have when the game starts.
Is that way good (spawn actor)?

The problem is that the SpawnActor seems to work badly.

Here, when I print the number of items in the inventory and their names, I only get as number of items 1, but the name doesn’t get displayed…

![alt text][1]

![alt text][2]

72599-1.png

WeaponName is a FString

“Is the break point getting into that if statement now?”

As you can see from the screen, the DebugMessage with < NAME > doesn’t get called. So… no.

Is the break point getting into that if statement now? And can you show me the get weapon name function?

Would you try changing that for loop to something like…
for(AWeapon* weapon : Inventory)
{
//gengine call here
}

![alt text][2]
![alt text][3]

I’m just trying to give the player some weapons at begin play…

If there’s another way or maybe weapons shouldn’t be actor… i don’t know.

I don’t know what’s the trick with this engine, I simply would have done a NEW weapon inside the Inventory… but I can’t use NEW