Skip to content

Instantly share code, notes, and snippets.

@EricWF
Created March 17, 2014 22:41
Show Gist options
  • Save EricWF/9609876 to your computer and use it in GitHub Desktop.
Save EricWF/9609876 to your computer and use it in GitHub Desktop.
/// this is the method "declaration". Just like you declare methods in a header file for a class, this is how you declare
/// methods for an entity. It is important to remember that these are "methods". That means that while the user may see
/// the function signature "void(level &)" the actual signature is "void(entity &, level &)". It has to take a "this" reference.
/// When writing methods you must write the method using the internal interface with the "this" reference.
/// This is the declaration.
/// It should be considered similar to declaring a function as so:
/// class entity {
/// void update_(level &);
/// };
/// YOU DON'T NEED TO DO THIS PART
struct update_m : method_base<update_m, void(level &)> {};
/// Alright, this is the weird part that doesn't make much sense. The way entity is designed
/// you need to pass an actual object of update_m to entity to call the method. This means that the actual
/// name of the method is not update_m, but instead whatever the name of the update_m object is.
/// in this case update_. we use update on an entity as so. entity.call(update_, level &) (or entity(update_, level &))
/// YOU DON'T NEED TO DO THIS PART TO OVERLOAD AN EXISTING METHOD DEFINITION
constexpr update_m update_ {};
/// To provide a definition for a method, simply write a lambda that has the write interface.
/// i.e. one that return void and takes entity &, level & as it's parameters.
/// this is like defining the method in a cpp file as so.
/// void entity::update_m(level & l) {
/// do stuff...
/// }
auto update_def_1 =
[](entity & e, level & l)
{
// do stuff...
}
/// ANY definition with the same signature as update_ can be used as an update function in an entity.
auto update_def_2 =
[](entity & e, level & l)
{
}
/// This is one way to set the update method on an entity
/// It sets the update_ method to use update_def_1
entity e1;
e1.set(update_, update_def_1);
/// This is another way to set the update method on an entity.
/// This sets the update_ method to use update_def_2
entity e2;
e2 << method(update_, update_def_2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment