|
| 1 | +# Managing Memory |
| 2 | + |
| 3 | +## UObject |
| 4 | + |
| 5 | +A UObject is one of the fundamental parts of Unreal Engine. It represents an engine/editor object tracked by Unreal Engine Garbage Collector. |
| 6 | + |
| 7 | +Each UObject is constantly tracked, and whenever the Garbage Collector runs (generally every 60 seconds or in very specific parts of the engine/editor loop, like when entering PIE mode) |
| 8 | +it checks if the UObject has still references to other UObjects, and in negative case (no references) it will be destroyed. |
| 9 | + |
| 10 | +You can trigger a Garbage Collector run from python with: |
| 11 | + |
| 12 | +```python |
| 13 | +import unreal_engine as ue |
| 14 | +ue.console_exec('obj gc') |
| 15 | +``` |
| 16 | + |
| 17 | +## py_UEObject |
| 18 | + |
| 19 | +this is the low-level C struct representing the python mapping between a PyObject (c struct representing a python object) and a UObject. |
| 20 | +Whenever you create a py_UEObject (from the UE python api) another GC (related to the python plugin) will start tracking it. |
| 21 | + |
| 22 | +Whenever the UE GC runs, the UNrealEnginePython GC will run too, checking if a UObject mapped to a py_UEObject is still alive. |
| 23 | + |
| 24 | +If the UObject mapped to a python object is dead, an exception will be triggered. |
| 25 | + |
| 26 | +This is an example: |
| 27 | + |
| 28 | +```python |
| 29 | +import unreal_engine as ue |
| 30 | + |
| 31 | +from unreal_engine.classes import BlueprintFactory |
| 32 | + |
| 33 | +factory = BlueprintFactory() |
| 34 | +# run GC |
| 35 | +ue.console_exec('obj gc') |
| 36 | +# this will raise an exception as the UObject mapped to factory has been destroyed by the GC run |
| 37 | +print(factory) |
| 38 | +``` |
| 39 | + |
| 40 | +By running this script you will end with something like this: |
| 41 | + |
| 42 | +``` |
| 43 | +PyUObject is in invalid state |
| 44 | +Traceback (most recent call last): |
| 45 | + File "<string>", line XX, in <module> |
| 46 | +Exception: PyUObject is in invalid state |
| 47 | +``` |
0 commit comments