Jump to content

EventQueue Manager


Einlander
 Share

Recommended Posts

[I had text here but in the edit it dissapeared :(]

I found that there can only be one instance of the EventQueue. This meant that all the widget events need to be handled in a single location leading to possible complications in code and lowering the possibility to dynamic things. This script allows the developer to subscribe to an eventqueue manager and get their event safely, without usurping all gui controls.

The script will automatically create an EventQueueManager variable that all other scripts can expect.

To use it from a script not attached to an entity use:

EventQueueManager:AddListener(Update)  -- a simple function

 

On a script attached to an entity or a lua based class use:

EventQueueManager:AddListener(serverbrowser.Update)  --  convert : into .

 

Place the script into a file at: Scripts/Functions/eventqueuemanager.lua

--[[
		  Name:	EventQueueManager
		Author:	Einlander
Date Completed: 2017-8-20
   Description:	An EventQueue manager
	     Notes:	This script puts all the eventqueue proccessing in one location, allowing other scripts to manage themselves
				and not take control of the eventqueue.
				THIS SCRIPT WILL AUTO LOAD AS A GLOBAL
				THIS SCRIPT WILL AUTO LOAD AS A GLOBAL
				THIS SCRIPT WILL AUTO LOAD AS A GLOBAL
				THIS SCRIPT WILL AUTO LOAD AS A GLOBAL
	   Updates: 2017-8-22
					-Will no longer overwrite EventQueueManager variable. This will lead to unintended behavior
					-No longer loops through the evenqueue. It is possible to have infinite Loops With previous behavior
--]]

EventQueueManager = {}
function EventQueueManager:Create()
	local self = {}
	local callbackList = {}
	
	--self:AddListener is an overloaded function
	--self:AddListener(callback) simple callback, used on functions not attached to an entity
	--self:AddListener(self, callback) Used when adding from a script attached to an entity (calling from and to any function that starts with Script: )
	function self:AddListener(...)
		local args = {...}
		local _callbackself 
		local _callback 
		if (#args==1) then		
			_callback = args[1]		
		elseif (#args==2) then				
			_callbackself = args[1]
			_callback = args[2]
		else
			return false
		end		
		for index,callback in ipairs(callbackList) do
			if callback == _callback then 
			--System:Print("Match Found")
			return false 
			end
		end
		table.insert(callbackList,{callback = _callback,self = _callbackself})
		return true
	end
	function self:RemoveListener(_callback) -- removes the callback
		for index,callbackitem in ipairs(callbackList) do
			if callbackitem.callback == _callback then				
				table.remove(callbackList,index)				
				return true
			end
		end
		return false
	end
	function self:Update() -- processes eventqueue and sends the event info to all callbacks
		if EventQueue:Peek() then -- will no longer loop through the entire event queue. It can lead to infinite loops.
			local event = EventQueue:Wait()	
			for index,callbackitem in pairs(callbackList) do
				if callbackitem.self == nil then 					
					callbackitem:callback(event)
				else
					callbackitem.callback(callbackitem.self,event)
				end
			end
		end
	end
	return self
end


if EventQueueManager ~= nil then -- Will no longer Overwrite variable but will allow for unexpected behavior
	EventQueueManager = EventQueueManager:Create()
end

8-22-2017 -- updated script

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...