[Question] Making own actor like "playerstart". How to add sprite?

I do not understand how to add sprite to blueprint, to see this sprite on map. Bad that some classes like playerstart have only headers, and i can not look at it’s sources to understand right logic.

I have this class:

UCLASS(Blueprintable)
class ASM_EnemyFactory : public AActor
{
	GENERATED_UCLASS_BODY()

public:

protected:
	UPROPERTY(EditDefaultsOnly, Category=Sprite)
	class UTexture2D* SpriteTexture;	

private:
	TSubobjectPtr DrawSphereComponent;
	TSubobjectPtr Sprite;
};

And:

ASM_EnemyFactory::ASM_EnemyFactory(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
{
	DrawSphereComponent = PCIP.CreateDefaultSubobject(this, FName("DrawSphereComponent0"));

	if (DrawSphereComponent)
	{
		DrawSphereComponent->ShapeColor.R = 173;
		DrawSphereComponent->ShapeColor.G = 239;
		DrawSphereComponent->ShapeColor.B = 231;
		DrawSphereComponent->ShapeColor.A = 255;

		DrawSphereComponent->AlwaysLoadOnClient = false;
		DrawSphereComponent->AlwaysLoadOnServer = false;
		DrawSphereComponent->bAbsoluteScale = true;
	}
	Components.Add(DrawSphereComponent);
	RootComponent = DrawSphereComponent;

	Sprite = PCIP.CreateDefaultSubobject(this, FName("Sprite0"));
	Sprite->SetSprite(SpriteTexture); 
	Sprite->AttachParent = DrawSphereComponent;
	Components.Add(Sprite);
}

In editor i see only DrawSphereComponent and do not see Sprite. What should i do to see the sprite too?

Hi Ilya,

Your basic approach looks good there. A few things to try:

  • if you want the sprite only for editor usage, try using PCIP.CreateEditorOnlyDefaultSubobject()
  • make sure Sprite->bVisible is true (it should be by default, so I expect it is)
  • set Sprite->bAbsoluteScale = true
  • ensure your SpriteTexture is set to something valid when SetSprite is called

Hope that helps!

Jeff

Hi, tnx for reply.
I have edited code to:

	Sprite = PCIP.CreateEditorOnlyDefaultSubobject(this, FName("Sprite0"));
	if (Sprite)
	{
		Sprite->SetSprite(SpriteTexture); 
		Sprite->bVisible = true;
		Sprite->bAbsoluteScale = true;
		Sprite->AttachParent = DrawSphereComponent;
		Components.Add(Sprite);
	}

And i still have no sprite texture in this actor.

Also, i have checked you said, and right, SpriteTexture is always 0x0000… even if it set, like i showed it in screenshot.

Maybe SpriteTexture should be different defined for sprite?

If I’m understanding correctly, it looks like you’re assigning the SpriteTexture member of the SpriteComponent to the bullseye-looking texture. But your code is trying to use ASM_EnemyFactory::SpriteTexture in your SetSprite() call.

You might be able to just remove the SetSprite call altogether, since it is probably already set properly by your assignment in the component.

“the bullseye-looking texture” it is just texture i have found in editor, it does not matter which texture to use, with all textures i have same issue.

“the bullseye-looking texture” is set in blueprint defaults as SpriteTexture variable defined in code as:

UPROPERTY(EditDefaultsOnly, Category=Sprite)
class UTexture2D* SpriteTexture;   

And class constructor Sprite->SetSprite(SpriteTexture); is using that texture to set as sprite texture, but in blueprint editor window i have no sprite.

When i tried to remove SetSprite() then in blueprint editor window i can see default sprite texture (raptor head or some like that)

But if i completely remove sprite adding from code, and then i am adding sprite from component list in editor, then it works fine.

Also, in other thread i have posted my other experiments with adding components in c++ constructor , i have tried to add boxcomponent, and it has no collision in game, and tried to add staticmesh component, and it has no texture in editor and no collision in game, but i am sure i was setting all things right like it provided with examples.

So, i am thinking, if i have same issues with all components i tried to add (except movement component and skeletal mesh component), then i do not know what is wrong with me :slight_smile:

You mentioned your SpriteTexture is NULL when you call SetSprite, and it looks like there are 2 variables here called SpriteTexture. It looks like you set one in the editor, but are using the other in the code?

Jeff, yes SpriteTexture is NULL, but it was set in blueprint, in blueprint of this class SpriteTexture is not null, so i do not know why it is null in code in constructor. Also i have only one variable called SpriteTexture defined in code and then used in blueprint.

I have deleted all my code and have added sprite in blueprint from components list and set texture there, and now it works fine.

But bad thing is i did not understand why it was not been working when i created sprite component in code.

Anyway tnx.

Cool, glad you got it working.

Fwiw, the 2nd SpriteTexture variable I was referring to was the one we provide in USpriteComponent, which I believe is the one that was set when you assigned a texture in the Components tab.

Cheers!