mirror of
https://github.com/Relintai/entity_spell_system.git
synced 2025-04-19 21:33:15 +02:00
implemented serialization for EntityDataContainer.
This commit is contained in:
parent
7d4b718acc
commit
5c1c47f5ce
@ -1,5 +1,23 @@
|
||||
#include "entity_data_container.h"
|
||||
|
||||
Dictionary EntityDataContainer::to_dict() {
|
||||
return call("_to_dict");
|
||||
}
|
||||
void EntityDataContainer::from_dict(const Dictionary &dict) {
|
||||
call("_from_dict", dict);
|
||||
}
|
||||
|
||||
Dictionary EntityDataContainer::_to_dict() {
|
||||
Dictionary dict;
|
||||
|
||||
dict["class_name"] = get_class_static();
|
||||
|
||||
return dict;
|
||||
}
|
||||
void EntityDataContainer::_from_dict(const Dictionary &dict) {
|
||||
ERR_FAIL_COND(dict.empty());
|
||||
|
||||
}
|
||||
|
||||
EntityDataContainer::EntityDataContainer() {
|
||||
}
|
||||
@ -7,4 +25,13 @@ EntityDataContainer::~EntityDataContainer() {
|
||||
}
|
||||
|
||||
void EntityDataContainer::_bind_methods() {
|
||||
//Serialization
|
||||
BIND_VMETHOD(MethodInfo("_from_dict", PropertyInfo(Variant::DICTIONARY, "dict")));
|
||||
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::DICTIONARY, "dict"), "_to_dict"));
|
||||
|
||||
ClassDB::bind_method(D_METHOD("from_dict", "dict"), &EntityDataContainer::from_dict);
|
||||
ClassDB::bind_method(D_METHOD("to_dict"), &EntityDataContainer::to_dict);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("_from_dict", "dict"), &EntityDataContainer::_from_dict);
|
||||
ClassDB::bind_method(D_METHOD("_to_dict"), &EntityDataContainer::_to_dict);
|
||||
}
|
||||
|
@ -2,11 +2,18 @@
|
||||
#define ENTITY_DATA_CONTAINER_H
|
||||
|
||||
#include "core/reference.h"
|
||||
#include "core/dictionary.h"
|
||||
|
||||
class EntityDataContainer : public Reference {
|
||||
GDCLASS(EntityDataContainer, Reference);
|
||||
|
||||
public:
|
||||
Dictionary to_dict();
|
||||
void from_dict(const Dictionary &dict);
|
||||
|
||||
Dictionary _to_dict();
|
||||
void _from_dict(const Dictionary &dict);
|
||||
|
||||
EntityDataContainer();
|
||||
~EntityDataContainer();
|
||||
|
||||
|
@ -390,8 +390,14 @@ Dictionary Entity::_to_dict() {
|
||||
dict["talents"] = _s_talents;
|
||||
|
||||
//// Data ////
|
||||
//Vector<Ref<EntityDataContainer> > _s_data;
|
||||
//Vector<Ref<EntityDataContainer> > _c_data;
|
||||
|
||||
Array entity_datas;
|
||||
|
||||
for (int i = 0; i < _s_data.size(); ++i) {
|
||||
entity_datas.append(_s_data.get(i)->to_dict());
|
||||
}
|
||||
|
||||
dict["entity_datas"] = entity_datas;
|
||||
|
||||
//// Crafting ////
|
||||
|
||||
@ -556,8 +562,24 @@ void Entity::_from_dict(const Dictionary &dict) {
|
||||
|
||||
//// Data ////
|
||||
|
||||
//Vector<Ref<EntityDataContainer> > _s_data;
|
||||
//Vector<Ref<EntityDataContainer> > _c_data;
|
||||
Array entity_datas = dict.get("entity_datas", Array());
|
||||
|
||||
for (int i = 0; i < entity_datas.size(); ++i) {
|
||||
Dictionary entry = entity_datas.get(i);
|
||||
|
||||
String class_name = dict.get("class_name", EntityDataContainer::get_class_static());
|
||||
|
||||
if (ClassDB::can_instance(class_name) && ClassDB::is_parent_class(class_name, EntityDataContainer::get_class_static())) {
|
||||
Ref<EntityDataContainer> data = Ref<EntityDataContainer>(ClassDB::instance(class_name));
|
||||
|
||||
if (data.is_valid()) {
|
||||
data->from_dict(entry);
|
||||
|
||||
_s_data.push_back(data);
|
||||
_c_data.push_back(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//// Crafting ////
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user