MCPcopy Create free account
hub / github.com/ChaiScript/ChaiScript / main

Function main

samples/factory.cpp:57–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55};
56
57int 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

Callers

nothing calls this directly

Calls 4

funFunction · 0.85
varFunction · 0.85
addMethod · 0.45
evalMethod · 0.45

Tested by

no test coverage detected