| 40 | namespace script_world |
| 41 | { |
| 42 | void create_instances(ScriptWorld &sw |
| 43 | , const void *components_data |
| 44 | , u32 num |
| 45 | , const UnitId *unit_lookup |
| 46 | , const u32 *unit_index |
| 47 | ) |
| 48 | { |
| 49 | const ScriptDesc *scripts = (ScriptDesc *)components_data; |
| 50 | |
| 51 | for (u32 i = 0; i < num; ++i) { |
| 52 | UnitId unit = unit_lookup[unit_index[i]]; |
| 53 | CE_ASSERT(!hash_map::has(sw._map, unit), "Unit already has a script component"); |
| 54 | |
| 55 | u32 script_i = hash_map::get(sw._cache |
| 56 | , scripts[i].script_resource |
| 57 | , UINT32_MAX |
| 58 | ); |
| 59 | |
| 60 | ScriptWorld::ScriptData sd; |
| 61 | sd.module_ref = LUA_REFNIL; |
| 62 | |
| 63 | if (script_i != UINT32_MAX) { |
| 64 | sd = sw._script[script_i]; |
| 65 | } else { |
| 66 | script_i = array::size(sw._script); |
| 67 | |
| 68 | LuaStack stack = sw._lua_environment->require(scripts[i].script_resource_name, 1); |
| 69 | sd.module_ref = luaL_ref(stack.L, LUA_REGISTRYINDEX); |
| 70 | |
| 71 | array::push_back(sw._script, sd); |
| 72 | hash_map::set(sw._cache, scripts[i].script_resource, script_i); |
| 73 | } |
| 74 | |
| 75 | ScriptWorld::InstanceData data; |
| 76 | data.unit = unit; |
| 77 | data.script_i = script_i; |
| 78 | |
| 79 | u32 instance_i = array::size(sw._data); |
| 80 | array::push_back(sw._data, data); |
| 81 | hash_map::set(sw._map, unit, instance_i); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | ScriptId create(ScriptWorld &sw, UnitId unit, const ScriptDesc &desc) |
| 86 | { |