StaticMeshComponenet Transforms nullptr, Access violation?

AItemGroup* ItemGroupRef;

Yea that is just the component or is it the object class. ?

The component is not the whole class. I think this is the component.
And you are using the component inside the GetWrold() line to use as object key class to access the < A Aclass > and that is wrong, it will return null.

The component object is not the class object.

ItemGroupRef = GetWorld()->SpawnActor< AItemGroup >(ItemGroup, SpawnTransform, SpawnInfo);

Here is the ItemGroup and there you have itemgroupref

ItemGroup is the object class, and itemgroupref is the componet object.

So in your header I see no
ItemGroup; only AItemGroup* ItemGroupRef;
in your getworld you got the ItemGroup that is the key class object.

So where is it in the header declared ?

You mean I should do something like
.h

UPROPERTY(EditAnywhere)
TSubclassOf<AItemGroup> ItemGroup = AActor::StaticClass();

UPROPERTY(EditAnywhere, BlueprintReadWrite)
AItemGroup* ItemGroupRef;

.cpp

ItemGroupRef = GetWorld()->SpawnActor< AItemGroup >(ItemGroup, SpawnTransform, SpawnInfo);

You see what I told you, in your code you have.

AItemGroup::StaticClass()

Meaning you use some function to get the class object but for some reason you fail to properly bring in

AItemGroup object key

yea, something like that

and use

ItemGroupRef

as component

laterā€¦something like
ItemGroupRef->AttachToComponent Get some other function you need to grab whatever(), FAttachmentTransformRules:: blah blah blah and so on.

And there for you attached the itemgroup object component to your local component on the relative scale you set in the other page and now you transform to world since the relative values come from the other cpp page ?

I hope you are not making one of those violent 3d Shooter game where you just stand with your gun and shoot people :slight_smile: people are stuck and synapsing on that, on 3d shooter.

Good luck ditching BP and going C++. I donā€™t like BP I call them cosmetics and only use them only when I have to like to decorate things

now the logic looks like this but still null ptr.
I am doing something wrong?

.h

UPROPERTY(EditAnywhere)
TSubclassOf<AItemGroup> ItemGroup = AActor::StaticClass();

UPROPERTY(EditAnywhere, BlueprintReadWrite)
AItemGroup* ItemGroupRef;

.cpp

ItemGroupRef = GetWorld()->SpawnActor< AItemGroup >(ItemGroup, SpawnTransform, SpawnInfo);

and calling this function still getting the nullptr

ItemGroupRef->GetAllTransforms(ArrayOfLocations);

The wrong part is ditch BP completley, start with a static mesh component that has a scene component that has a mesh attached to the static mesh.

Start from the very start.
This time actually add the root component.
Root = CreateDefaultSubobject< USceneComponent >(ā€œUSceneComponentā€);
Then you can attach the static component to the root uscene component.

Step 2 get world to import with Tsubclassof from the other cpp the object you wish to spawn and create a component for it attach component from the other cpp to your component.

Step 3 Use get possition to get the relative possition of the imported object

Step 4 Transform it to world.

1 Like

Sir I will try this after 15 minutes, and will surely update here about the results

Start with this

I understand there is no need for the mesh because it comes from the other CPP imported class so you may want to skip this and only implement it in the other cpp page, because you need it there loaded on the component, the static mesh that is because you will import the component with it.

1 Like

just comes back and tried Your solution without changing the SceneComponent and now the static mesh is not returning nullprt, the groupitem actor is spawned in the level but the static meshes it hold are empty and no item is attached to them.

@GigiK-K-K @KaidoomDev
Thank You very very much for helping me to solve the first issue, really appreciated

now when I run this function in beginplay to spawn items it crash the engine.
but if i run this function in the onconstructor everythng works fine and the meshes are not empty.
is it okay to run the function in onconstructor?

No, itā€™s not a good practice to run it on the constructor. But it should have no problem if you donā€™t overload the constructor so donā€™t add other functions there because itā€™s not suppose to take many things, maybe you can post the beginplay crash error.

1 Like

when i run this function under OnConstruction(const FTransform& Transform) everything just works fine, i donā€™t know why its not working on beginplay function

the C++ beginplay is not working like you expect to be as blueprint beginplay, the C++ onconstruction is the blueprint constructor.
learn more about their differences ā€¦ and remember one thing if you are spawning actors at runtime you should spawn them on constructor and on beginplay set their meshes. if you are doing these all stuff in one function then your function will not work on beginplay from bp side.

hope it helps
cheers!

1 Like

`OnConstruction(const FTransform& Transform) is not the main constructor function, so you can run things there without problems.

the constructor is simply denoted by the name of the class.
AMyClass::AMyClass()
{ constructor content}

The other function uses the construct and transform, it uses the constructor to automate your construct you can put everything inside of it, itā€™s not a problem.

You need to set up a blueprint after your class to make the getworld() work.
The variables are extended in the blueprint, and on the menu you got the spawn options from your parent class. Itā€™s just an empty blueprint made after the class to spawn the objects you want and it will represent your class, rest nodes and so on you donā€™t need them. You do need a representative for your class to spawn things around and replicate spawns.

1 Like

@GigiK-K-K @c0r37py Thank You friends for assisting me, and I a already exposing those variables to get them as a class parameters

meta = (DisplayName = "ID", Category = "Items Related", MultiLine = "true", ExposeOnSpawn = "true"))

Sure anytime, for a beginer, life is hard as a beginer in C++.
Takes 15 to 20 years to become expert god like in C++
Takes about serveral years just to become an advenced c++ user.

So it works on const and trans function, because itā€™s suppose to work there.
Maybe if you tweek some things on begin play it will work there as well.

I have mine on begin play and it works, but I cannot post the whole code here, itā€™s just unfair.

1 Like

So what I get from you is that you have not created a blueprint based on your class, that might be your problem. So you just used the meta tag instead to expose access to blueprints.

1 Like

Thanks for sharing.