I’ve set it up to expose PaperSprite’s GetSourceSize() to Blueprints, now I’m trying to follow the same method for DateTime’s GetDayOfWeek() but I’ve encountered a problem with returning a value of the type EDayOfWeek.
Visual Studio’s intellisense is giving info when I hover over it, so it’s definitely being recognized but when I try to build it says:
The function looks like:
and
Is there some way enums have to be wrapped/casted in order to be returned from a function? Some way of treating it as a UENUM maybe? Or maybe I’m way off and doing something very wrong…
Hmmm… when I just replace EDayOfWeek with TEnumAsByte<EDayOfWeek>, I get an error with:
Which sounds strange when you consider it has no problem recognizing the EDayOfWeek enum…
Did some Googling but that didn’t really shed much light on the problem, especially given I’m using a built-in enum so I can’t change how the enum is defined if that’s the problem or it ruins the whole point.
I think the enum used with TEnumAsByte is still required to be a UENUM which isn’t the case with EDayOfWeek as pointed out by the error message in your original question. The error message should be an UHT/UBT related error similar to the one you get when attempting to inherit a UCLASS from a non UCLASS.
Your first option that requires the least amount of work would be to use uint8 as the return type since EDayOfWeek represents the days as numbers between and including 0 to 6. The disadvantage is that uint8 isn’t forced to this range and could be set to all other uint8 values.
The second option would be to provide a “wrapper” UENUM with the same values as in EDayOfWeek and a conversion function from EDayOfWeek to the new UENUM type. This enforces the enum to be one of the few values in the UENUM but may have other complications.
The last alternative could be a simple wrapper USTRUCT that has a single regular (not UPROPERTY) EDayOfWeek member variable. Your blueprint functions that operate with EDayOfWeek would then have this new struct as input/output type. Unfortunately this means that you still do not get direct access to the value of EDayOfWeek in blueprint (unless you use this approach in combination with the first or second one above) and everything you want to do with it must be implemented in C++ as blueprint functions. Everything you do only in C++ that doesn’t need to be blueprint related can still work directly with EDayOfWeek.
You can always customize your engine as well if you’re building it from source yourself. In that case you could simply try to add the UENUM macro to EDayOfWeek.
Which possibly leads into your next issue. FDateTime isn’t a UCLASS, USTRUCT or UENUM either.
Interesting… this is turning out to be a lot more complicated than I’d expected for something that seemed so trivial…
I think I’ll just go with this one. Or rather… try this one and if it doesn’t work do 1 of 2 things:
Just use a known date and weekday, then count the days between then and the weekday I want and use modulo to get the weekday. Not hard to do, I just thought using Unreal’s own functions would be more practical
Just not bother with weekdays at all for now. It’s not mega important for the idea I had and probably isn’t worth the hassle.
One thing though: Assuming the GetDayOfWeek() function is implemented correctly… there’s no reason that the unit8 should ever actually wind up being outside of 0-6, is there? Assuming that the date itself is valid, and if the date is invalid then the weekday won’t be needed or valid anyway.
Well I tried the uint8 way and I got it working. Seems to accurately give me the correct number between 0 and 6 for Monday - Sunday. Excellent. Thanks for the help.
That’s correct. What I meant by “The disadvantage is that uint8 isn’t forced to this range and could be set to all other uint8 values.” is that a Blueprint user might use your GetDayOfWeek function node in blueprint and store the return value in a uint8/byte variable. Then at some point later they perform some calculations and use that result as a day of week again. If they don’t pay attention it is possible that the value is now outside the range 0-6 and no longer a valid day.
Can you explain how to do this and get it to work? I get “Expected the name of a previously defined enum” when using your method. And I don’t know how HeWasAgaisntIt managed to get his function to return a uint8 but mine just threw errors.