Open eugeneloza opened 6 years ago
Doesn't look that simple. Only by virtual function to get a value. Thou raising exceptions is harder, maybe return some default 'error' value.
Looks clean and nice
type DVariant = object
Value: pointer;
ValueType: TValueType;
case ValueType of //(that part won't seem to work, need a better idea here)
vtInteger: function GetValue: integer; // if Self = nil then log error
vtString: function GetValue: string;
...
end;
end;
function GetValueByName(aName: string): DVariant;
begin
Result := nil;
Result := inherited GetValueByName(aName);
case aName of
'hp': Result := ConstructVariant(@Self.hp.Current, vtFloat);
'direction': Result := ConstructVariant(@Self.Direction, vtVector3);
end;
end;
DVariant.ToString, DVariant.ToInteger...
etc.
And yes, check how Variant
is implemented in FPC, stop reinventing the wheel.
entities should invoke not only raw fields, but methods too! Maybe use only getters and setters - that'd be a clean approach, but would require nearly duplicating the code, because these getters and setters would operate on different kind of data (raw pointers).
As this is a feature required for scripting engine I'll move it for "later".
Such approach is needed only for a very few entities (accessible from the scripts), e.g. Actors and active World objects. So, no need to make it into anything "global". Just a interface
for such objects with GetValueByName
, SetValueByName
and CallMethodByName
will do it perfectly.
This might come in handy: http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.TObject.MethodAddress http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.TObject.FieldAddress
Study Published
property closer. It seems that TObject
already has such functionality. This way making a script engine would be much simpler.
We may try to build absolutely any entity dictionary of TObject to be addressed directly, e.g. from a console script. Also every entity may have an (autogenerated or manual)
Alias
or several of those. Take advantage of$IF DECLARED(ClassName)}ClassName+'.'+{$ENDIF}
stuff to check if the entity has a proper value and raise a warning otherwise. Maybe, should then doDEntity = class(DObject) GetValueByName(ValueName: string);
something like that? Hmm...Declared
would hardly operate on non-static stuff, however, we can just use a "Static set of those" inside acase input of
statement.