Download

How do I access the size of a sprite?

Right now I just want to find the size of a sprite component in a Blueprint class because I want to use it to restrict the panning of the camera, but after searching the menu I can’t get any further than Get Sprite, which I would assume to be a step in the right direction but doesn’t seem to lead to anything obviously size-related.

I’ve tried looking for size… dimensions… width… length… but I can’t find anything that looks remotely related. You’d think something like this would be quite apparent, but maybe I’m approaching it the wrong way…

Where can you access a sprite’s dimensions? I can even see it in the tooltip for the sprite in the content browser under Source Dimension…

I’ve found out how to get the X and Y dimensions of a texture, but I still can’t figure out if it’s possible to access a sprite’s texture through Blueprints. Presumably it is, and the documentation even seems to imply it:
https://docs.unrealengine.com/latest/INT/API/Plugins/Paper2D/UPaperSprite/index.html

But I can’t use GetSourceTexture() for instance. It doesn’t seem to exist or can’t be used for some reason.

Anyone? Is there a different way of accessing it perhaps? I know I could hardcode the dimensions of course… but I’d rather not leave 2 locations with the same sprite dimensions completely unconnected.

Do you mean you want the actual resolution of the source texture?

Yeah, that or the size of the sprite object it’s applied to. Either would work in my case I think.

The situation being that I have a sprite that’s bigger than the screen that I want the camera to scroll around without going past the edges. Another solution might involve declaring some variables somewhere and using those as the bounds of the camera but then I’d probably want to use those variables to scale the sprite in some way anyway, speaking of which: can you use variables to set values in the Details pane? e.g. For the position of a camera. Or does that have be set through an Event Graph?
I remember seeing something in the tutorials but it might have just been for UMG…

I’m not sure if it is possible to do that as I don’t use paper2d, but if it isn’t, it’s still possible to expose these functions to blueprints yourself, but it requires using C++ to make the custom node.
For example, here I’ve exposed UPaperSprite::GetSourceSize to blueprints, it’s pretty easy to add this to your project:

In ProjectName.Build.cs you will find the line


PublicDependencyModuleNames.AddRange(new string] { "Core", "CoreUObject", "Engine", "InputCore" });

Add Paper2D to this so it looks as below:


PublicDependencyModuleNames.AddRange(new string] { "Core", "CoreUObject", "Engine", "InputCore", "Paper2D" });

This is so you can include Paper2D header files like PaperSprite.h, so you can make a blueprint node that takes one as an argument.

The header file for the node:

SpriteFunctions.h



#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "PaperSprite.h"
#include "SpriteFunctions.generated.h"


/**
 * 
 */
UCLASS()
class PROJECTNAME_API USpriteFunctions : public UBlueprintFunctionLibrary //Your project name in uppercase followed by "_API"
{
	GENERATED_BODY()

public:

	UFUNCTION(BlueprintPure, meta = (DisplayName = "Get Source Size", Keywords = "Source Texture Sprite"), Category = Custom) //Here you can change the keywords, name and category
		static FVector2D GetSourceSize(UPaperSprite* sprite);
	
};

SpriteFunctions.cpp


#include "ProjectName.h" //Replace with your project name
#include "PaperSprite.h"
#include "SpriteFunctions.h"

FVector2D USpriteFunctions::GetSourceSize(UPaperSprite* sprite)
{
	return sprite->GetSourceSize();
	
}

As you can see it’s pretty simple to set up, and you can easily add more functions to this function library so you can access them in blueprints.

The node in use:
c39a96ef55b333355916bb3d5bfad5a51fe95a51.png

For your second question, you could set them through the construction script with variables

Thanks for that, should be helpful. Annoying that they seem to have left such a basic function out or in some unintuitive place at least; maybe they had some other type of solution in mind… I wonder if messing around with Paper 2D Flipbooks might help since you’d expect sprite size to be important in the case of fully fledged animations but who knows.

If nothing else, this’ll give me a reason to start mixing C++ and Blueprints, like I’d originally intended before Blueprints looked so inviting…

I’m trying to implement this now but it’s still not showing up in the editor for some reason…

I changed the line in Build.cs, then I selected New C++ Class… created a class called SpriteFunctions, pasted the code in, changed the include and _API to the name of my project, saved…

Then I went into the editor and selected Refresh Visual Studio Project, which I assume is what I’m meant to do after every time I change any C++? Seems to make sense.

After that, Visual Studio gives a message saying that the solution has been modified outside the environment, though it seems to have given me at least 2 different messages in this situation… one with the option to “Reload” the updated solution from disk (which sounds like what I want) and another with a “Save As…” option that sounds similar. I assume I’m not supposed to build in Visual Studio, and when I try it it predictably doesn’t compile right.

So I’m not sure what’s wrong at this point… I can’t find the function by right-clicking, turning Context Sensitive off and typing the name. I’ve checked multiple times and everything seems fine. I did miss the _API bit before but changing that didn’t fix it.

You should rebuild the solution in Visual Studio, in the solution explorer under Games, right click your project and click Rebuild. Try closing the editor first and then building it

Ok, will try that…

Cool, it succeeded.

Opening Unreal now…

It’s there! Excellent. That should help a lot with some of the things they chose not to expose to Blueprints…

Thanks.

Edit: Actually, one more thing… how do you find what you need to add to the PublicDependencyModuleNames.AddRange list?

For instance, I want to expose a C++ function of the DateTime class; should adding “DateTime” work or might it be a part of a larger module…?

Edit2: I see according to https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Misc/FDateTime/index.html the module is just Core. Only problem is that it says GetDayOfWeek() returns CORE_APIEDayOfWeek, but if I just put that into the C++ it doesn’t recognize the type when I build:

I also tried EDayOfWeek (I assume E refers to it being an enum) but that didn’t work either.

I’ve included DateTime.h and it appears to recognize FDateTime because it doesn’t give an error if I declare a variable of that type before the day of week function, so I assume that’s working ok.

Doesn’t seem that CORE_API is meant to be used like a namespace either, so I can’t figure it out…

Edit3: Visual Studio definitely recognizes EDayOfWeek and that’s definitely what GetDayOfWeek returns because the intellisense says as much. Just won’t let me return that type from a function? Must be meant to be wrapped in a UENUM or something…?

Anyone have any ideas as to why there might be a problem returning an EDayOfWeek? You’d think being an enum it would derive from UENUM in some way and be fine, but apparently it doesn’t work that way. The C++ forum might be better for this, but it’s kind of relevant to both so I dunno.

Not that in UE4.17 the includes for PaperSprite.h and Kismet/BlueprintFunctionLibrary.h must be removed from the .cpp file