| 55 | }; |
| 56 | |
| 57 | int main() |
| 58 | { |
| 59 | chaiscript::ChaiScript chai; |
| 60 | |
| 61 | chai.add(chaiscript::fun(&Entity::width), "width"); |
| 62 | chai.add(chaiscript::fun(&Entity::height), "height"); |
| 63 | chai.add(chaiscript::fun(&Entity::x), "x"); |
| 64 | chai.add(chaiscript::fun(&Entity::y), "y"); |
| 65 | chai.add(chaiscript::fun(&Entity::name), "name"); |
| 66 | chai.add(chaiscript::fun(&Entity::updater), "updater"); |
| 67 | chai.add(chaiscript::user_type<Entity>(), "Entity"); // this isn't strictly necessary but makes error messages nicer |
| 68 | |
| 69 | chai.add(chaiscript::fun(&Factory::make_entity), "make_entity"); |
| 70 | chai.add(chaiscript::fun(&Factory::get_entity), "get_entity"); |
| 71 | chai.add(chaiscript::fun(&Factory::update_entities), "update_entities"); |
| 72 | chai.add(chaiscript::user_type<Factory>(), "Factory"); // this isn't strictly necessary but makes error messages nicer |
| 73 | |
| 74 | |
| 75 | Factory f; |
| 76 | chai.add(chaiscript::var(&f), "f"); |
| 77 | |
| 78 | std::string script = R""( |
| 79 | f.make_entity(10,10,1,1,"entity1").updater = fun(e){ e.x += 1; e.y += 1 }; |
| 80 | f.make_entity(10,10,10,10,"entity2").updater = fun(e){ e.x += 2; e.y += 2 }; |
| 81 | f.make_entity(10,10,20,20,"entity3"); |
| 82 | |
| 83 | print(f.get_entity("entity1").x == 1) |
| 84 | print(f.get_entity("entity2").x == 10) |
| 85 | print(f.get_entity("entity3").x == 20) |
| 86 | |
| 87 | f.update_entities(); // this runs the function objects we set in the previous lines |
| 88 | // we should now see the updated values |
| 89 | |
| 90 | print(f.get_entity("entity1").x == 2) |
| 91 | print(f.get_entity("entity2").x == 12) |
| 92 | print(f.get_entity("entity3").x == 20) // this one has no updater, so it stays the same |
| 93 | )""; |
| 94 | |
| 95 | |
| 96 | chai.eval(script); |
| 97 | |
| 98 | |
| 99 | |
| 100 | } |
| 101 | |
| 102 | |