Nymph Game Engine
Chaiscript based Game Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
script_object.h
Go to the documentation of this file.
1 #ifndef SCRIPT_OBJECT_H
2 #define SCRIPT_OBJECT_H
3 #include <memory>
4 #include <type_traits>
5 #include <json/json.h>
6 #include "events/subject.h"
7 #include "events/observer.h"
8 #include "events/event.h"
9 #include "chaiscript_wrapper.h"
10 
11 //= SCRIPTABLE
12 //= SCRIPTABLE BASES Subject Observer
13 
14 namespace Script {
15  class ScriptingSystem;
19  class ScriptObject : public Events::Subject, public Events::Observer, public std::enable_shared_from_this<ScriptObject> {
20  private:
21  friend class ScriptingSystem;
22 
23  std::string class_name;
24  bool saved;
25  bool sent_this;
26  unsigned int num_saved_fields;
27  std::vector<std::string> serialized_var_names;
28 
29  std::shared_ptr<chaiscript::dispatch::Dynamic_Object> script_object;
30 
31  std::function<void (std::shared_ptr<chaiscript::dispatch::Dynamic_Object>)> script_on_start;
32  std::function<void (std::shared_ptr<chaiscript::dispatch::Dynamic_Object>, const float)> script_on_update;
33  std::function<void (std::shared_ptr<chaiscript::dispatch::Dynamic_Object>)> script_on_destroy;
34  std::function<void (std::shared_ptr<chaiscript::dispatch::Dynamic_Object>, std::shared_ptr<ScriptObject>)> script_set_obj_handle;
35 
36  template<class T>
37  typename std::enable_if<std::is_base_of<Events::Event, T>::value, std::function<void(std::shared_ptr<chaiscript::dispatch::Dynamic_Object>, std::shared_ptr<T>)>>::type selectEventHandler();
38 
39  void invokeScriptFunction(const std::function<void ()>& func);
40 
41  void setSerializable(const std::string& name);
42 
43  void create();
44  void sendThisToScript();
45 
46  public:
52  ScriptObject(const std::string& name);
58  ScriptObject(const ScriptObject& other);
62  ~ScriptObject() = default;
63 
64  //= BEGIN SCRIPTABLE
65 
71  void setClassName(const std::string& name);
72 
73  std::string getClassName() const noexcept;
74  //= END SCRIPTABLE
75 
79  void onStart();
85  void onUpdate(const float delta);
89  void onDestroy();
90 
91  bool shouldBeSerialized() const noexcept;
92 
93  Json::Value getSaveData() const noexcept;
94  void loadSavedData(Json::Value& save_data);
95 
96  bool representsDynamicObject(const chaiscript::dispatch::Dynamic_Object& obj) const noexcept;
97 
98  virtual void onNotifyNow(std::shared_ptr<Events::Event> event) override;
99  virtual void handleQueuedEvent(std::shared_ptr<Events::Event> event) override;
100  };
101 }
102 #endif
virtual void handleQueuedEvent(std::shared_ptr< Events::Event > event) override
HandleQueuedEvent allows derived classes to define behaviour for when queuedEvents are received...
Definition: script_object.cpp:191
bool shouldBeSerialized() const noexcept
Definition: script_object.cpp:183
bool representsDynamicObject(const chaiscript::dispatch::Dynamic_Object &obj) const noexcept
Definition: script_object.cpp:165
virtual void onNotifyNow(std::shared_ptr< Events::Event > event) override
When receiving an event that is immediate, onNotifyNow is used. It acts as an interrupt to make sure ...
Definition: script_object.cpp:187
Class for script object.
Definition: script_object.h:19
Class for a subject that an observer would observe for changes.
Definition: subject.h:13
Class for scripting system.
Definition: scripting_system.h:10
Interface to be notified of an item's changes.
Definition: observer.h:13
~ScriptObject()=default
Destroys the object.
void onStart()
Calls onStart in the attached script object.
Definition: script_object.cpp:169
std::string getClassName() const noexcept
Definition: script_object.cpp:20
void onUpdate(const float delta)
Calls onUpdate in the attached script object.
Definition: script_object.cpp:173
Json::Value getSaveData() const noexcept
Definition: script_object.cpp:57
ScriptObject(const std::string &name)
ScriptObject constructor.
Definition: script_object.cpp:7
void onDestroy()
Calls onDestroy in the attached script object.
Definition: script_object.cpp:178
void loadSavedData(Json::Value &save_data)
Definition: script_object.cpp:88
void setClassName(const std::string &name)
Sets the class name on the scripting object.
Definition: script_object.cpp:16