Tramway SDK
script.h
Go to the documentation of this file.
1// Tramway Drifting and Dungeon Exploration Simulator SDK Runtime
2
3#ifndef TRAM_SDK_ENTITIES_SCRIPT_H
4#define TRAM_SDK_ENTITIES_SCRIPT_H
5
6#include <framework/entity.h>
8
9#include <framework/script.h>
10
11#include <iostream>
12
13namespace tram {
14
15// TODO: make constr_func etc. into typedefs??
16namespace ScriptableType {
17 void Register(name_t base_type, Entity* (*constr_func)(name_t new_type, const SharedEntityData&, const ValueArray&), void (*destr_func)(Entity*));
18 Entity* Make(name_t base_type, name_t type, const SharedEntityData&, const ValueArray&);
19 void Yeet(Entity* yeetable);
20}
21
22template <typename T>
23class Scriptable : public T {
24public:
25 Scriptable(const SharedEntityData& shared_data, const ValueArray& field_array, name_t type) : T(shared_data, field_array) {
26 this->type = type;
27 }
28
30
32 if (Script::CallFunction("__impl_entity_update_parameters_callback", {this->id})) {
33 T::UpdateParameters();
34 }
35 }
36
38 if (Script::CallFunction("__impl_entity_set_parameters_callback", {this->id})) {
39 T::SetParameters();
40 }
41 }
42
43 void Load() {
44 if (Script::CallFunction("__impl_entity_load_callback", {this->id})) {
45 T::Load();
46 }
47 }
48
49 void Unload() {
50 if (Script::CallFunction("__impl_entity_unload_callback", {this->id})) {
51 T::Unload();
52 }
53 }
54
55 void Serialize() {
56 /*if (Script::CallFunction("__impl_entity_serialize_callback", {this->id})) {
57 if (entity) entity->Serialize();
58 }*/
59 }
60
61 inline void SanitizeData(Value& data) {
62 // there might be some issues if the message's data pointer is not pointing
63 // to a value, but something else.
64
65 // hopefully we won't get segfaults.
66 bool valid_type = data.IsFloat() || data.IsInt() || data.IsBool() || data.GetType() == TYPE_NAME;
67
68 if (!valid_type) data = Value();
69
70 if (data.GetType() == TYPE_NAME) {
71 if (!name_t::is_valid(data)) data = Value();
72 }
73 }
74
76 Value data = msg.data ? *msg.data_value : Value();
77
78 SanitizeData(data);
79
80 if (Script::CallFunction("__impl_entity_message_handler_callback", {this->id, msg.type, msg.sender, msg.receiver, data})) {
81 T::MessageHandler(msg);
82 }
83 }
84
85 void EventHandler(Event& evt) {
86 Value data = evt.data ? *evt.data_value : Value();
87
88 SanitizeData(data);
89
90 if (Script::CallFunction("__impl_entity_event_handler_callback", {this->id, evt.type, evt.subtype, evt.poster, data})) {
91 T::EventHandler(evt);
92 }
93 }
94
96 return type;
97 }
98protected:
100};
101
102}
103
104#endif // TRAM_SDK_ENTITIES_SCRIPT_H
Provides script language subclassing.
Definition: script.h:23
void UpdateParameters()
Definition: script.h:31
Scriptable(const SharedEntityData &shared_data, const ValueArray &field_array, name_t type)
Definition: script.h:25
name_t GetType()
Definition: script.h:95
void EventHandler(Event &evt)
Definition: script.h:85
void Serialize()
Definition: script.h:55
void Unload()
Definition: script.h:49
void SanitizeData(Value &data)
Definition: script.h:61
void SetParameters()
Definition: script.h:37
void Load()
Definition: script.h:43
void MessageHandler(Message &msg)
Definition: script.h:75
~Scriptable()
Definition: script.h:29
name_t type
Definition: script.h:99
Definition: value.h:314
Generic, type-safe union.
Definition: value.h:18
bool IsBool() const
Definition: value.h:128
bool IsInt() const
Definition: value.h:129
bool IsFloat() const
Definition: value.h:130
Type GetType() const
Definition: value.h:174
void Load(const char *filename)
Loads a language file.
Definition: language.cpp:33
value_t CallFunction(name_t name, std::vector< Value > parameters)
Definition: script.cpp:60
void Register(name_t base_type, Entity *(*constr_func)(name_t new_type, const SharedEntityData &, const ValueArray &), void(*destr_func)(Entity *))
Definition: script.cpp:39
void Yeet(Entity *yeetable)
Definition: script.cpp:51
Entity * Make(name_t base_type, name_t new_type, const SharedEntityData &data, const ValueArray &array)
Definition: script.cpp:43
Serialization, i.e.
@ TYPE_NAME
Definition: type.h:15
UID name_t
Definition: uid.h:43
Event data.
Definition: event.h:24
event_t type
Type of the event.
Definition: event.h:58
void * data
Pointer to arbitrary data.
Definition: event.h:63
id_t poster
Definition: event.h:60
event_t subtype
Arbitrary event subtype.
Definition: event.h:59
value_t * data_value
Pointer to a value_t.
Definition: event.h:65
Message data.
Definition: message.h:16
message_t type
Type of the message.
Definition: message.h:53
id_t receiver
ID of the Entity that will receive the message.
Definition: message.h:55
id_t sender
ID of the Entity that sent the message.
Definition: message.h:54
value_t * data_value
Pointer to a value_t.
Definition: message.h:60
void * data
Pointer to arbitrary data.
Definition: message.h:58
Basic Entity parameters.
Definition: entity.h:132
Interned string type.
Definition: uid.h:10
static bool is_valid(const UID &)
Checks whether the name is valid.
Definition: uid.cpp:121