From 7e13e3b45c08bfb288c57f23a7e9440e0a128003 Mon Sep 17 00:00:00 2001 From: codeimp Date: Wed, 15 Sep 2010 20:29:40 +0000 Subject: [PATCH] @ Added a very useful function in the map element fields. Why didn't I add this long ago?! --- Source/Core/Map/UniFields.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Source/Core/Map/UniFields.cs b/Source/Core/Map/UniFields.cs index df32895c..b7aaab6d 100644 --- a/Source/Core/Map/UniFields.cs +++ b/Source/Core/Map/UniFields.cs @@ -66,5 +66,22 @@ namespace CodeImp.DoomBuilder.Map if(owner != null) owner.BeforeFieldsChange(); } + + /// This returns the value of a field by name, or returns the specified value when no such field exists or the field value fails to convert to the same datatype. + public T GetValue(string fieldname, T defaultvalue) + { + if(!this.ContainsKey(fieldname)) + return defaultvalue; + + try + { + T val = (T)this[fieldname].Value; + return val; + } + catch(InvalidCastException) + { + return defaultvalue; + } + } } }