Skip to content

Commit

Permalink
Make the message sending functions return a bool, closes Unvanquished#3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kangz authored and slipher committed Oct 16, 2023
1 parent 65d66da commit 0ed6fa6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions tools/cbse/templates/implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ Entity::Entity(const MessageHandler *messageHandlers, const int* componentOffset
Entity::~Entity() {
}

void Entity::SendMessage(int msg, const void* data) {
bool Entity::SendMessage(int msg, const void* data) {
MessageHandler handler = messageHandlers[msg];
if (handler) {
handler(this, data);
return true;
}
return false;
}

// Entity helper functions to send message e.g.
// void Entity::Damage(int value);
{% for message in messages %}
void Entity::{{message.name}}({{message.get_function_args()}}) {
bool Entity::{{message.name}}({{message.get_function_args()}}) {
{% if message.get_num_args() == 0 %}
SendMessage({{message.get_enum_name()}}, nullptr);
return SendMessage({{message.get_enum_name()}}, nullptr);
{% else %}
{{message.get_tuple_type()}} data({{message.get_args_names()}});
SendMessage({{message.get_enum_name()}}, &data);
return SendMessage({{message.get_enum_name()}}, &data);
{% endif %}
}
{% endfor %}
Expand Down
8 changes: 4 additions & 4 deletions tools/cbse/templates/implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class Entity {

// This function should never actually be used directly, it dispatches
// the messages to the right components
void SendMessage(int msg, const void* data);
bool SendMessage(int msg, const void* data);

// Handy functions to send a specific message to the components of Entity e.g.
// void Damage(int amount);
// Handy functions to send a specific message to the components of Entity e.g. (returns true if the message was handled)
// bool Damage(int amount);
{% for message in messages %}
void {{message.name}}({{message.get_function_args()}});
bool {{message.name}}({{message.get_function_args()}});
{% endfor %}

// Getter for the different components, returns nullptr if it doesn't have the component e.g.
Expand Down

0 comments on commit 0ed6fa6

Please sign in to comment.