Просмотр исходного кода

world: add script_world::broadcast()

Daniele Bartolini 4 месяцев назад
Родитель
Сommit
317b4e05e5
2 измененных файлов с 34 добавлено и 0 удалено
  1. 27 0
      src/world/script_world.cpp
  2. 7 0
      src/world/script_world.h

+ 27 - 0
src/world/script_world.cpp

@@ -200,6 +200,33 @@ namespace script_world
 		// Unit not found
 	}
 
+	void broadcast(ScriptWorld &sw
+		, const char *function_name
+		, const ArgType::Enum *arg_types
+		, const Arg *args
+		, u32 num_args
+		)
+	{
+		if (sw._disable_callbacks)
+			return;
+
+		LuaStack stack(sw._lua_environment->L);
+
+		for (u32 i = 0; i < array::size(sw._script); ++i) {
+			lua_rawgeti(stack.L, LUA_REGISTRYINDEX, sw._script[i].module_ref);
+			lua_getfield(stack.L, -1, function_name);
+
+			stack.push_args(arg_types, args, num_args);
+
+			int status = sw._lua_environment->call(num_args, 0);
+			if (status != LUA_OK) {
+				report(stack.L, status);
+				device()->pause();
+			}
+			stack.pop(1);
+		}
+	}
+
 	void multicast(ScriptWorld &sw
 		, const char *function_name
 		, const UnitId *units

+ 7 - 0
src/world/script_world.h

@@ -77,6 +77,13 @@ namespace script_world
 		u32 unit_index;
 	};
 
+	/// Calls @a function_name in all scripts if defined.
+	void broadcast(ScriptWorld &sw
+		, const char *function_name
+		, const ArgType::Enum *arg_types = NULL
+		, const Arg *args = NULL
+		, u32 num_args = 0
+		);
 
 	///
 	void multicast(ScriptWorld &sw