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…
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:
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:
class PROJECTNAME_API USpriteFunctions : public UBlueprintFunctionLibrary //Your project name in uppercase followed by "_API"
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);
#include "ProjectName.h" //Replace with your project name
FVector2D USpriteFunctions::GetSourceSize(UPaperSprite* sprite)
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.
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.
It’s there! Excellent. That should help a lot with some of the things they chose not to expose to Blueprints…
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 FDateTime | Unreal Engine 5.2 Documentation 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.
I am currently using Unreal 5.2 and noticed this information is outdated.
First of all, the function mentioned above (i.e.)
FVector2D UPaperSprite::GetSourceSize() const;
will not work in a Production build, as the data it references is WITH_EDITORONLY_DATA and the function itself is WITH_EDITOR.
So, how can you find the dimensions of a Sprite? Well, you could use:
// This holds the origin of the bounds (or the pivot point in Unreal units)
// This holds the extents of the bounds (i.e. the half-dimension) in Unreal units
From this you can find the sprite dims in Unreal units. This will also work even if you are running from an atlas. If you need to know the actual texture units for some reason, then you should also use:
UPaperSprite *Sprite = <some sprite>
FBoxSphereBounds Bounds = Sprite->GetRenderBounds();
FVector WorldSpriteDims = ( Bounds.BoxExtent * 2.0 );
// WorldSpriteDims.X is the horizontal size for the sprite
// WorldSpriteDims.Z is the vertical size for the sprite
FVector WorldSpritePivot = Bounds.Origin;
// WorldSpritePivot.X and WorldSpritePivot.Z are the pivot points of the sprite in Unreal units.
// If you need the actual texture dims, do this:
FVector TexDims = ( Bounds.BoxExtent * 2.0 ) * ( double ) Sprite->GetPixelsPerUnrealUnit();
// TexDims.X will hold the texture X size and
// TexDims.Z will hold the texture Y size.