[UE4]UObject Notes
Keywords: UE4, UObject, Construct, Construction, Instantiating, UObject-derived, Config, DefaultGame.ini
NewObject using a Blueprint class
1st way:
header:
UPROPERTY(EditAnywhere, Category = "Player")
TSubclassOf<AMyActor> AMyActorClass;
cpp:
MyActor = NewObject<AMyActor>(this, TEXT("MyActor"), RF_NoFlags, AMyActorClass->GetDefaultObject(), true);
2nd way:
header:
UPROPERTY(EditAnywhere, Category = "Player")
TSubclassOf<AMyActor> AMyActorClass;
cpp:
MyActor = NewObject<AMyActor>(this, AMyActorClass);
Different ways to instantiate the object
-
UObject::CreateDefaultSubobject
is only callable in a class constructor, and takes care of creating an instance of the CDO of the subobject’s class, setting its outer class as the caller object, among other things. The created object then becomes the default object for the property when its object class is instantiated. -
NewObject<T>
is the function normally used to instantiate objects after engine initialisation, during normal gameplay. It provides several convenience overloads to handle most scenarios. -
UWorld::SpawnActor<T>
is a convenience method to spawn actors in a level with the specified location and rotation, spawn collision settings, and checks to ensure it’s a spawnable actor class, and is nothing more than a wrapper of NewObject. -
ConstructObject
has been removed in favour of NewObject.
Orign:
Unreal Engine 4. Different ways to instantiate the object
https://stackoverflow.com/a/60030248/1645289
Check Types
Class:
UClass::IsChildOf();
Actor:
AActor::IsA();
Initialize members of UObject from ini configuration file
Engine\Source\Runtime\CoreUObject\Public\UObject\Object.h
/**
* Imports property values from an .ini file.
*
* @param Class the class to use for determining which section of the ini to retrieve text values from
* @param Filename indicates the filename to load values from; if not specified, uses ConfigClass's ClassConfigName
* @param PropagationFlags indicates how this call to LoadConfig should be propagated; expects a bitmask of UE4::ELoadConfigPropagationFlags values.
* @param PropertyToLoad if specified, only the ini value for the specified property will be imported.
*/
void LoadConfig( UClass* ConfigClass=NULL, const TCHAR* Filename=NULL, uint32 PropagationFlags=UE4::LCPF_None, class UProperty* PropertyToLoad=NULL );
/**
* Wrapper method for LoadConfig that is used when reloading the config data for objects at runtime which have already loaded their config data at least once.
* Allows the objects the receive a callback that it's configuration data has been reloaded.
*
* @param Class the class to use for determining which section of the ini to retrieve text values from
* @param Filename indicates the filename to load values from; if not specified, uses ConfigClass's ClassConfigName
* @param PropagationFlags indicates how this call to LoadConfig should be propagated; expects a bitmask of UE4::ELoadConfigPropagationFlags values.
* @param PropertyToLoad if specified, only the ini value for the specified property will be imported
*/
void ReloadConfig( UClass* ConfigClass=NULL, const TCHAR* Filename=NULL, uint32 PropagationFlags=UE4::LCPF_None, class UProperty* PropertyToLoad=NULL );
/** Import an object from a file. */
void ParseParms( const TCHAR* Parms );
How do I create a blueprint that can inherit from a UObject
Examples:
UCLASS(Blueprintable, BlueprintType)
class ENGINE_API UMyObject : public UObject
{
Reference:
https://answers.unrealengine.com/questions/35953/view.html
Config (ini file)
Config Types
perObjectConfig
: 使得配置变为针对每个Object的实例,而不是每个Class存一个配置块。configdonotcheckdefaults
: 配置了这个的话,config在读取的时候就不会去读取default配置。defaultconfig
: 配置只保存在default中
UE4中Config的使用
https://blog.ch-wind.com/ue4-config-usage/
How to add GUI for custom ini file in Project Settings
Credits to: UnLuaModule.cpp
1, Add C++ class for ini config:
UCLASS(Config = UnLua, DefaultConfig, Meta = (DisplayName = "UnLua"))
class UNLUA_API UUnLuaSettings : public UObject
{
GENERATED_BODY()
public:
UUnLuaSettings(const FObjectInitializer& ObjectInitializer);
/** Entry module name of lua env. Leave it empty to skip execution on startup. */
UPROPERTY(Config, EditAnywhere, Category="Runtime")
FString StartupModuleName = TEXT("");
}
2, Use FModuleManager::RegisterSettings()
to register custom ini settings in Project Settings on startup module:
void FMyPluginModule::StartupModule()
{
RegisterSettings();
}
void FMyPluginModule::ShutdownModule()
{
UnregisterSettings();
}
void FMyPluginModule::RegisterSettings()
{
#if WITH_EDITOR
ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings");
if (!SettingsModule)
return;
const auto Section = SettingsModule->RegisterSettings("Project", "Plugins", "UnLua",
LOCTEXT("UnLuaEditorSettingsName", "UnLua"), LOCTEXT("UnLuaEditorSettingsDescription", "UnLua Runtime Settings"),
GetMutableDefault<UUnLuaSettings>());
Section->OnModified().BindRaw(this, &FMyPluginModule::OnSettingsModified);
#endif
void FMyPluginModule::UnregisterSettings()
{
#if WITH_EDITOR
ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings");
if (SettingsModule)
SettingsModule->UnregisterSettings("Project", "Plugins", "UnLua");
#endif
}
}
Plugins Config file(ini)
You can put config files into plugin’s Config directory. e.g.:
[Porject]\Plugins\[Plugin]\Config\CustomSettings.ini
If there’s a config with same file name in the directory [Porject]\Config\
, then the content of [Porject]\Plugins\[Plugin]\Config\
will be merged into [Porject]\Config\
automatically when packaging.
生如夏花之绚烂,死如秋叶之静美。----泰戈尔《生如夏花》