aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-31 03:17:16 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-31 03:17:16 -0400
commit7b63e52b38d131ee44eb341f22d3d39f37979507 (patch)
treef25a6ebd4aac58ed2a2c30d7ca192f276a5513dc /src
parent871a882833364cf3a2b1de13fc25f610ef5d7da5 (diff)
Upon spawning, automatically call Entity Init function if it exists
Diffstat (limited to 'src')
-rw-r--r--src/script.cpp44
-rw-r--r--src/script.hpp3
2 files changed, 31 insertions, 16 deletions
diff --git a/src/script.cpp b/src/script.cpp
index 1a6bda8..d471d65 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -107,19 +107,26 @@ void ScriptSystem::scriptExport()
sol::table ScriptSystem::spawn(sol::object param)
{
- sol::table* toRet;
+ sol::table* toRet; // "Entitiy" table to be returned
if (param.get_type() == sol::type::table) {
- sol::table tab = param;
-
- entityx::Entity e = manager->create();
- auto d = e.assign<Scripted>().get();
- d->caller = lua.create_table();
- toRet = &(d->caller);
-
- *toRet = tab; // Copy table to our new entity to preserve functions
- // this reduces the amount of work done in this function
- // as well
-
+ sol::table tab = param; // Cast the generic parameter to a table
+
+ entityx::Entity e = manager->create(); // Create a new entity
+ auto d = e.assign<Scripted>().get(); // Since this entity was created
+ // via Lua, assign the Scripted
+ // component.
+ d->caller = lua.create_table(); // Create our Entity "table"
+ toRet = &(d->caller); // set our return table to the Entity "table"
+
+ *toRet = tab; /* Copy table to our new entity to preserve functions
+ * this reduces the amount of work done in this function
+ * as well. We are allowed to do this because lua allows
+ * for the reallocation of types.
+ */
+
+ /*
+ * Create the component dependancies (i.e.: position) first
+ */
if (tab["Position"] != nullptr) {
(*toRet)["Position"] =
e.assign<Position>(Position().FromLua(tab["Position"])).get();
@@ -131,22 +138,27 @@ sol::table ScriptSystem::spawn(sol::object param)
}
if (tab["Render"] != nullptr) {
- if (!e.has_component<Position>())
- e.assign<Position>();
+ if (!e.has_component<Position>()) // Position must exist for render
+ (*toRet)["Position"] = e.assign<Position>().get();
(*toRet)["Render"] =
e.assign<Render>(Render().FromLua(tab["Render"])).get();
}
if (tab["Velocity"] != nullptr) {
- if (!e.has_component<Position>())
- e.assign<Position>();
+ if (!e.has_component<Position>()) // Position must exist for vel.
+ (*toRet)["Position"] = e.assign<Position>().get();
(*toRet)["Velocity"] =
e.assign<Velocity>(Velocity().FromLua(tab["Velocity"])).get();
}
} else {
+ // TODO better logging
std::cerr << "Parameter to spawn() must be a table!" << std::endl;
}
+ // If the Entity has an Init function defined, call it
+ if ((*toRet)["Init"].get_type() == sol::type::function)
+ (*toRet)["Init"](*toRet);
+
return *toRet;
}
diff --git a/src/script.hpp b/src/script.hpp
index bd8c620..be02542 100644
--- a/src/script.hpp
+++ b/src/script.hpp
@@ -87,6 +87,9 @@ class ScriptSystem : public entityx::System<ScriptSystem>
/**
* The function called by lua scripts in order to spawn an entity.
+ * @param param The table that must be passed in by Lua. This is a
+ * sol2 object instead of a sol2 table because this allows us to handle
+ * errors easier instead of letting sol2 do the error handling.
*/
sol::table spawn(sol::object param);