Fledgling dev flailing their way through a magical girl apocalypse

Well, 5 days later and I’m still at it!

I spent half the time experimenting with accessing a log file and discovering that doing so 60x a second was Too Much ™. Pesky OS. There was a close statement too, but windows Was Not Happy.

The issue was further complicated by the need to start and stop file reading—the program making the logs rotate them so the filename changes (plan is to have it open the most recent).

The code examples from @Alex_8BitSkull and stack exchange were invaluable. Though I was distracted from the actual reading of the file by an epic struggle with timer.cancel. Creating timers is easy—stopping them is another monster altogether.

Pkeod’s comment in DyleniumFalcon’s thread ( Timer.cancel (solved) ) gave my exhausted brain a much-needed nudge. With that inspiration, time.cancel was finally slain, and laid to rest in an on_message.

Most of which is debugging prints to myself.


local function boom(self, handle, time_elapsed)						-- for testing 
	local functn = "boom "											-- debug 
	print(scriptn .. functn .. "boom handle " .. handle)			-- debug 
end


function on_message(self, message_id, message, sender)
	-- use on_message to start / stop the log reading
	-- Learn more: https://defold.com/manuals/message-passing/
	local functn = "on_message "									-- for debugging
	if message_id == hash("start_reading") then						-- check if the messages is "start_reading"
		print(scriptn .. functn .. "message_id " .. message_id)		-- debug seemed like useful thing to know.
		read_log = "yes"											-- set read_log to yes. for controlling things.
		print(scriptn .. functn .. "read_log " .. read_log) 		-- debug 
		if self.start_presses <= 0 then 							-- help prevent more timers running.
			self.tail_timer = timer.delay(0.5, true, boom) 			-- wait .5 seconds and call function to read the log.
																	-- repeat until canceled. Took me an hour to figure out timers.
			self.start_presses = self.start_presses + 1				-- help prevent more timers from running. needs to be better 
		end
	end
	if message_id == hash("stop_reading") then						-- check for "stop_reading"
		print(scriptn .. functn .. "message_id " .. message_id)		-- debug seemed like useful thing to know.
		read_log = "no"												-- set read_log to no, for control later 
		print(scriptn .. functn .. "read_log " .. read_log) 		-- debug 
		timer.cancel(self.tail_timer)								-- DIE DIE DIE DIE DIE
																	-- took me 2 days to figure out how to cancel.
		self.start_presses = self.start_presses - 1					-- decriment the timer count. 
	end
end

Now that the log isn’t clobbered by 1000s of accesses, I can return to reading and doing something with the data.

The obligatory screenshot. I love the background, I’m so glad the artist licensed it. This will eventually turn into a settings and configuration screen, but for now I just make buttons and hope things happen.

On the left is are a couple buttons that use http.request(). One returns the defold home page’s status. The second is more complicated request to twitch’s API that returns data for a user.

On the right is where most of my efforts currently lay, with nothing much happening (at least not in the game UI, there’s loads in the console).

Behold, the glory of all my debug messages!

DEBUG:SCRIPT: main on_input Touch!
DEBUG:SCRIPT: test_http.gui_script stop reading log
DEBUG:SCRIPT: readchatlog on_message message_id [stop_reading]
DEBUG:SCRIPT: readchatlog on_message read_log no
DEBUG:SCRIPT: readchatlog log_timer usetimer no
DEBUG:SCRIPT: main on_input Touch!
DEBUG:SCRIPT: test_http.gui_script start reading
DEBUG:SCRIPT: readchatlog on_message message_id [start_reading]
DEBUG:SCRIPT: readchatlog on_message read_log yes
DEBUG:SCRIPT: readchatlog boom boom handle 65536
DEBUG:SCRIPT: main on_input Touch!
DEBUG:SCRIPT: test_http.gui_script stop reading log
DEBUG:SCRIPT: readchatlog on_message message_id [stop_reading]
DEBUG:SCRIPT: readchatlog on_message read_log no
DEBUG:SCRIPT: readchatlog log_timer usetimer no

Next up:

  • Reading the file
  • separating out the user names from the message
  • throwing it into a db
  • should probably make a db too
  • figuring out how to display it. There’s a particular UI I want to build.

The goal is to have each log line have a rectangle. The next line will bump the previous rectangles up/down until the oldest messages leave the viewing area.

5 Likes