Jump to content
Einlander

Lua based EventQueue script

Recommended Posts

I discovered that there can only be one eventqueue in leadwerks. I wanted local eventqueue instances, so I created a lua based version of EventQueue called EventQueueEx

 

It is initialized as such:

MyQueue = EventQueueEx:Create()

It is used EXACTLY the same as the Leadwerks version.

 

--[[
		  Name:	EventQueEX
		Author:	Einlander
Date Completed: 
   Description:	A lua Implementation of the EventQueue class
	     Notes:	This script was mainly created a developer can have self contained EventQueue
				and not have to restructure your program to have exactly 1 EventQueue processing routine.
				To be used exactly as the built-in EventQueue.
--]]
EventQueueEx = {}

function EventQueueEx:Create() -- Create an instance of EventQueueEx
	local self = {}
	local queue = {}
	
	function self:Peek() -- Return true if table is not empty
		if #queue > 0 then return true end
		return false
	end
	function self:Wait() -- get top entry and remove from table		
		if #queue < 1 then return nil end		
		local eventItem = Event(queue[1].id, queue[1].source, queue[1].data, iVec2(queue[1].x, queue[1].y) , iVec2(queue[1].width, queue[1].height), queue[1].extra)
		-- we remove and not nil it to keep a contigous array with no holes in it.
		-- This allows for the #table shorcut to work without manually looping through the whole table
		table.remove(queue,1) 		
		return eventItem
	end
	function self:Flush() -- Clear Queue
		queue = {}
	end
	function self:Emit(id, source, data, x,y,width,height,extra) -- Add Event to Queue
		-- simple input checks and sanitation				
		Debug:Assert(id ~= nil, "id is ["..type(id).."], Number or Event expected." )	
		if id:GetClassName() == "Event"	then 
			-- passed a event object
			table.insert(queue,{id = id.id, source = id.source, data = id.data, x = id.position.x, y = id.position.y, width = id.size.x, height = id.size.y, extra = extra})			
		else
			-- simple input checks and sanitation
			-- will set defaults if value is missing
			Debug:Assert(type(id) == "number", "id is ["..type(id).."], Number Expected." )				
			local source = source
			local data = (type(data) == "number") and data or 0		
			local x = (type(x) == "number") and x or 0
			local y = (type(y) == "number") and y or 0
			local width = (type(width) == "number") and width or 0
			local height = (type(height) == "number") and height or 0
			local extra = extra		
			table.insert(queue,{id = id, source = source, data = data, x = x, y = y, width = width, height = height, extra = extra})
		end
		
	end
	function self:Release() -- allow class to go out of scope
		queue = nil
		self = nil
	end
	return self
end

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...