Skip to content

Instantly share code, notes, and snippets.

@FilipSivak
Created January 19, 2022 22:24
Show Gist options
  • Save FilipSivak/fbbc3c449e59d1bae1f3e5f361204cff to your computer and use it in GitHub Desktop.
Save FilipSivak/fbbc3c449e59d1bae1f3e5f361204cff to your computer and use it in GitHub Desktop.
Struct and Enum in Unreal Engine python
# Example by Filip Sivak, written for https://filipsivak.medium.com/
import unreal
@unreal.uenum()
class MyPythonEnum(unreal.EnumBase):
FIRST = unreal.uvalue(0)
SECOND = unreal.uvalue(1)
FOURTH = unreal.uvalue(3)
@unreal.ustruct()
class PythonUnrealStruct(unreal.StructBase):
some_string = unreal.uproperty(str)
some_number = unreal.uproperty(float)
array_of_string = unreal.uproperty(unreal.Array(str))
@unreal.uclass()
class PythonTestClass(unreal.BlueprintFunctionLibrary):
@unreal.ufunction(static = True, params = [int], ret = PythonUnrealStruct)
def MyPythonFunction(integer_argument1):
struct = PythonUnrealStruct()
struct.some_string = "5"
struct.some_number = integer_argument1 + 1
struct.array_of_string = ["a", "b", "c"]
return struct
@SZNero
Copy link

SZNero commented Oct 21, 2022

i made the PythonUnrealStruct as a variable in the blueprint ,but when i restarted the project .the variable would be removed or invalid,and then i need to recreate the variable. do you kown the reason and how can i make it useful all the time.thank you.

@FilipSivak
Copy link
Author

Yes, this is a typical error. Unfortunately, you need to run the python script creating the variable before use of the variable or on engine startup (in file init_unreal.py) and then recompile the blueprint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment