IO Reading Question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

IO Reading Question

Post by Beelz »

I'm writing a block breakout game and I'm having trouble loading the templates I've made for it... I'm really just wondering if anyone can tell me what I'm doing wrong here:

Code: Select all

function LoadMap(map)
	local row_num = 0
	local column_num = 0

	for line in io.lines(map) do 
    	row_num = row_num + 1
		for j = 1, line:len() do
			column_num = column_num + 1
			NewBlock(row_num,column_num,string.sub(line, j))
		end
		column_num = 0
	end
end
The variables for the function 'NewBlock' are Row, Column, and Health. Each line of the template file is a row, each character of the lines are the columns, and the value of each character is the health of the block... This is what the templates look like:

Code: Select all

0000000000000000
0000000000000000
0000000000000000
5555555555555555
0112344554432110
0112233333322110
0012222222222100
0001111111111000
If anyone could lend me a hand I would be very grateful. The rest of the game is completely done... I just need to read from the templates and debug the blocks' collisions to finish it.

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: IO Reading Question

Post by slime »

The io library doesn't know about the game's source directory path (or save directory path). You can use [wiki]love.filesystem.lines[/wiki] instead.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: IO Reading Question

Post by Jasoco »

First it's not recommended to use any io functions. Instead use the Löve Filesystem commands. There's one for lines(). (Slime beat me to this one)

Second, len is depreciated. Just use #line.


A tip, I usually use tables for my map templates. Though they use more text. They're more future proof. But you can use what you need. Your method will still work fine. Though a table would use a different method.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: IO Reading Question

Post by Beelz »

Ah, thank you guys! That did the trick... Just one more question for now.

How can I get each character for the strings? I keep getting the character count as opposed to the character's value.



EDIT

Never mind I figured it out finally...

Code: Select all

string.sub(line,j,j)

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 137 guests