Page 1 of 1

onLeaveCombat event for Waypoint files

Posted: Tue May 29, 2012 1:34 pm
by Hedon
I was looking at ways to add this event to waypoints without modding the library classes but came up short. Here's a patch to player.lua and waypointlist.lua that allows you to define <onLeaveCombat> in your waypoint file just like <onLoad>. As with the player profile <onLeaveCombat> definition it is called every time you leave combat as well.

Code: Select all

Index: classes/player.lua
===================================================================
--- classes/player.lua	(revision 718)
+++ classes/player.lua	(working copy)
@@ -1613,6 +1613,15 @@
 			error(msg);
 		end
 	end
+	
+	-- check for onLeaveCombat in waypoint file
+	if (type(__WPL.onLeaveCombatEvent) == "function") then
+		local status,err = pcall(__WPL.onLeaveCombatEvent);
+		if( status == false ) then
+			local msg = sprintf(language[85], err);
+			error(msg);
+		end
+	end
 
 
 	-- give client a little time to update battle flag (to come out of combat),
Index: classes/waypointlist.lua
===================================================================
--- classes/waypointlist.lua	(revision 718)
+++ classes/waypointlist.lua	(working copy)
@@ -17,6 +17,8 @@
 
 		self.Type = 0; -- UNSET
 		self.ForcedType = 0; 	-- Wp type to overwrite current type, can be used by users in WP coding
+		
+		self.onLeaveCombatEvent = nil;
 	end
 );
 
@@ -86,6 +88,15 @@
 					self.onLoadEvent = nil;
 				end;
 			end
+		elseif( string.lower(name) == "onleavecombat" ) then
+			if( string.len(action) > 0 and string.find(action, "%w") ) then
+				self.onLeaveCombatEvent = loadstring(action);
+				assert(self.onLoadEvent, sprintf(language[152]));
+
+				if( _G.type(self.onLeaveCombatEvent) ~= "function" ) then
+					self.onLeaveCombatEvent = nil;
+				end;
+			end
 		end
 	end

Re: onLeaveCombat event for Waypoint files

Posted: Tue May 29, 2012 1:52 pm
by rock5
Doesn't really make sense to add an onLeaveCombat to the waypoint file. If all you want to do is edit the profile onLeaveCombat function, you can edit it in the onLoad section. You just need to know where it is. The profile events are stored in settings.profile.events. So you can do this.

Code: Select all

function settings.profile.events.onLeaveCombat()
  ...
  ...
  ...
end

Re: onLeaveCombat event for Waypoint files

Posted: Tue May 29, 2012 4:22 pm
by Hedon
Now that's slick. I wanted to save the profile event too so came up with this in my waypoint onLoad. The original definition is saved and called in the waypoint redefinition.

Code: Select all

<onLoad>
		--save the original profile function pointer if its defined or define a blank function if not
		if (type(settings.profile.events.onLeaveCombat) ~= "function") then
			onLeaveCombatProfile =  function (unused) return; end;
		else
			onLeaveCombatProfile = settings.profile.events.onLeaveCombat;
		end

		function settings.profile.events:onLeaveCombat()
			onLeaveCombatProfile();
			... waypoint event handling definition here
		end
</onLoad>

Re: onLeaveCombat event for Waypoint files

Posted: Tue May 29, 2012 5:45 pm
by lisa
--=== moved ===--

Re: onLeaveCombat event for Waypoint files

Posted: Tue May 29, 2012 11:36 pm
by rock5
That's the way to do it except I usually check if the function exists first or else it will be nesting the onleavecombat function if the script restarts itself such as when it changes character. So,

Code: Select all

   If not onLeaveCombatProfile then
      if (type(settings.profile.events.onLeaveCombat) ~= "function") then
         onLeaveCombatProfile =  function (unused) return; end;
      else
         onLeaveCombatProfile = settings.profile.events.onLeaveCombat;
      end
   end