MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / AccessVariant

Function AccessVariant

Source/Engine/AI/BehaviorKnowledge.cpp:18–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16#endif
17
18bool AccessVariant(Variant& instance, const StringAnsiView& member, Variant& value, bool set)
19{
20 if (member.IsEmpty())
21 {
22 // Whole blackboard value
23 if (set)
24 {
25 CHECK_RETURN(instance.Type == value.Type, false);
26 instance = value;
27 }
28 else
29 value = instance;
30 return true;
31 }
32 // TODO: support further path for nested value types (eg. structure field access)
33
34 const StringAnsiView typeName(instance.Type.TypeName);
35 const ScriptingTypeHandle typeHandle = Scripting::FindScriptingType(typeName);
36 if (typeHandle)
37 {
38 const ScriptingType& type = typeHandle.GetType();
39 switch (type.Type)
40 {
41 case ScriptingTypes::Structure:
42 {
43 const String memberStr(member);
44 // TODO: let SetField/GetField return boolean status of operation maybe?
45 if (set)
46 type.Struct.SetField(instance.AsBlob.Data, memberStr, value);
47 else
48 type.Struct.GetField(instance.AsBlob.Data, memberStr, value);
49 return true;
50 }
51 default:
52 {
53 if (void* field = typeHandle.Module->FindField(typeHandle, member))
54 {
55 if (set)
56 return !typeHandle.Module->SetFieldValue(field, instance, value);
57 else
58 return !typeHandle.Module->GetFieldValue(field, instance, value);
59 }
60 break;
61 }
62 }
63 }
64#if USE_CSHARP
65 if (const auto mClass = Scripting::FindClass(typeName))
66 {
67 MObject* instanceObject = MUtils::BoxVariant(instance);
68 bool failed = false;
69 if (const auto mField = mClass->GetField(member.Get()))
70 {
71 if (set)
72 mField->SetValue(instanceObject, MUtils::VariantToManagedArgPtr(value, mField->GetType(), failed));
73 else
74 value = MUtils::UnboxVariant(mField->GetValueBoxed(instanceObject));
75 return !failed;

Callers 1

AccessBehaviorKnowledgeFunction · 0.85

Calls 15

FindScriptingTypeFunction · 0.85
FindClassFunction · 0.85
StringClass · 0.50
IsEmptyMethod · 0.45
GetTypeMethod · 0.45
GetFieldMethod · 0.45
FindFieldMethod · 0.45
SetFieldValueMethod · 0.45
GetFieldValueMethod · 0.45
GetMethod · 0.45
SetValueMethod · 0.45
GetValueBoxedMethod · 0.45

Tested by

no test coverage detected