← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/bug-1772168-unused-key-in-luatable into lp:widelands

 

GunChleoc has proposed merging lp:~widelands-dev/widelands/bug-1772168-unused-key-in-luatable into lp:widelands.

Commit message:
New function get_vector() in LuaTable for reading hotspots. This fixes 'Error: Unused key "1"/"2" in Lua table.' for loading ware hotspots.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1772168 in widelands: "Some 'Error: Unused key "1"/"2" in Lua table."
  https://bugs.launchpad.net/widelands/+bug/1772168

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/bug-1772168-unused-key-in-luatable/+merge/354202
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/bug-1772168-unused-key-in-luatable into lp:widelands.
=== modified file 'src/graphic/animation.cc'
--- src/graphic/animation.cc	2018-07-08 16:10:50 +0000
+++ src/graphic/animation.cc	2018-09-03 17:58:19 +0000
@@ -42,17 +42,6 @@
 #include "sound/sound_handler.h"
 
 namespace {
-
-// Parses an array { 12, 23 } into a point.
-void get_point(const LuaTable& table, Vector2i* p) {
-	std::vector<int> pts = table.array_entries<int>();
-	if (pts.size() != 2) {
-		throw wexception("Expected 2 entries, but got %" PRIuS ".", pts.size());
-	}
-	p->x = pts[0];
-	p->y = pts[1];
-}
-
 /**
  * Implements the Animation interface for an animation that is unpacked on disk, that
  * is every frame and every pc color frame is an singular file on disk.
@@ -107,10 +96,8 @@
 };
 
 NonPackedAnimation::NonPackedAnimation(const LuaTable& table)
-   : frametime_(FRAME_LENGTH), hasplrclrs_(false), scale_(1), play_once_(false) {
+   : frametime_(FRAME_LENGTH), hotspot_(table.get_vector<std::string, int>("hotspot")), hasplrclrs_(false), scale_(1), play_once_(false) {
 	try {
-		get_point(*table.get_table("hotspot"), &hotspot_);
-
 		if (table.has_key("sound_effect")) {
 			std::unique_ptr<LuaTable> sound_effects = table.get_table("sound_effect");
 

=== modified file 'src/logic/map_objects/tribes/worker_descr.cc'
--- src/logic/map_objects/tribes/worker_descr.cc	2018-05-07 05:35:32 +0000
+++ src/logic/map_objects/tribes/worker_descr.cc	2018-09-03 17:58:19 +0000
@@ -40,8 +40,7 @@
                          const EditorGameBase& egbase)
    : BobDescr(init_descname, init_type, MapObjectDescr::OwnerType::kTribe, table),
      ware_hotspot_(table.has_key("ware_hotspot") ?
-                      Vector2i(table.get_table("ware_hotspot")->get_int(1),
-                               table.get_table("ware_hotspot")->get_int(2)) :
+                      table.get_vector<std::string, int>("ware_hotspot") :
                       Vector2i(0, 15)),
      default_target_quantity_(table.has_key("default_target_quantity") ?
                                  table.get_int("default_target_quantity") :

=== modified file 'src/scripting/lua_table.h'
--- src/scripting/lua_table.h	2018-04-07 16:59:00 +0000
+++ src/scripting/lua_table.h	2018-09-03 17:58:19 +0000
@@ -27,6 +27,7 @@
 
 #include <boost/lexical_cast.hpp>
 
+#include "base/vector.h"
 #include "scripting/lua.h"
 #include "scripting/lua_coroutine.h"
 #include "scripting/lua_errors.h"
@@ -112,6 +113,19 @@
 		return rv;
 	}
 
+	// Parses a Lua subtable into a Vector2i or Vector2f
+	template <typename KeyType, typename ValueType> Vector2<ValueType> get_vector(const KeyType& key) const {
+		Vector2<ValueType> result = Vector2<ValueType>::zero();
+		std::unique_ptr<LuaTable> table(get_table(key));
+		std::vector<ValueType> pts = table->array_entries<ValueType>();
+		if (pts.size() != 2) {
+			throw wexception("Expected 2 entries, but got %" PRIuS ".", pts.size());
+		}
+		result.x = pts[0];
+		result.y = pts[1];
+		return result;
+	}
+
 	template <typename KeyType> std::unique_ptr<LuaTable> get_table(const KeyType& key) const {
 		get_existing_table_value(key);
 		if (!lua_istable(L_, -1)) {


Follow ups