[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Structuring code with lots of pauses and states?

Structuring code with lots of pauses and states?

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Victorious   (89 posts)  [Biography] bio
Date Wed 22 Jun 2016 10:44 AM (UTC)

Amended on Thu 23 Jun 2016 02:39 PM (UTC) by Victorious

Message
Hi,

I'm trying to achieve something like the following.

-- a queue of items that i need to process in-game by using skills/spells 
while (queue is not empty) do
	local head = get first item
	getItemFromContainer() -- may fail
	processItem(head) -- this takes time, can fail, and uses mana. I also want to pause execution when I run out of mana to regenerate
	putItemInContainer() -- this step may fail as well
	removeCompletedItemFromQueue()
end


I've tried implementing it using wait.regexp to pause execution, but I'm finding that this is really messy. Is there a better way? Here's a simplified example. Aside from messiness, it is also difficult to shut it down because it relies on having a stack of functions (i'm not sure if that would even work) which are all paused at the appropriate times.


function mainLoop()
	wait.make(function()
		while true do
			local isQueueEmpty = #queue == 0
			
			if isQueueEmpty then
				print("Queue processing complete.")
				return
			end
			
			local head = queue[1]
			local getItemSuccess, getItemError = getItem(head.item, head.sourceContainer)
			
			if getItemSuccess, == false then
				print(getItemError)
				return
			end
			
			local processSuccess processError = processingLoop(head.item, head.sourceContainer, head.destContainer)
			if processSuccess, == false then
				print(processError)
				return
			end
			
		end -- while
	end) --wait
end --func

-- gets an item from the specified container
-- returns: a boolean indicating success, and an error message on failure
function getItem(item, container)
	-- send get command to mud
	-- use wait.regexp with 5 second timeout to determine success or error
end

-- processes an item
function processingLoop(item, sourceContainer, destContainer)
	while true do
		if regen required then 
			local regenSuccess, errorMsg = regenLoop(item, sourceContainer)
			-- return if regen not successful
		end
		
		-- send commands to mud, use wait.regexp again to determine whether processing is complete or not. Return true once item done
	end	
end

function regenLoop(item, sourceContainer)
	-- send command to put item back in container
	while regenRequired do
		wait.time(2)
	end
	
	-- send command to get item from container
end
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Wed 22 Jun 2016 03:39 PM (UTC)

Amended on Wed 22 Jun 2016 03:40 PM (UTC) by Fiendish

Message
Can you edit that post to use code tags and indenting?

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Wed 22 Jun 2016 07:46 PM (UTC)
Message
Template:codetag To make your code more readable please use [code] tags as described here.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Victorious   (89 posts)  [Biography] bio
Date Reply #3 on Thu 23 Jun 2016 02:43 PM (UTC)
Message
Post edited - my apologies.

I did manual indentation this time; does putting it between the code tags do it automatically?
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Thu 23 Jun 2016 08:00 PM (UTC)
Message
No it doesn't (unlike some more modern sites).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,848 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]