Browsing in a text file and variables

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
imhotep
Posts: 41
Joined: Tue Dec 13, 2005 9:15 pm

Browsing in a text file and variables

Post by imhotep »

hi, i've been somwhat struggling to create a lua script that is flexible
and smart to search through a text file to assign some values to variables.

here is an example text:

#charpic=patrick.png
#health=100
#type=enemy
#tileset=tiles/patrick_tile.png

what i would need is a script that searches one line, i thought i'd try this

Code: Select all

filename="patrick.txt"

        file = io.input(filename, "r")
 
if file then
	for line in file:lines() do
			middle = string.sub(line, "=")
		if middle then
			key = string.sub(line, 2, middle-1)
			result = string.sub(line, middle + 1)
			character[key] = result
		end
	end
end

-- output etc

my problem is, if if add the picture wit the following code,

Code: Select all

pix=Image.load(character.charpic)
screen:blit(100,100,pix)

a.s.o.

or if i try to assign a color with a variable, i always get errors, like image load error, or that i didn't do the color rgb syntax correctly.

also, my text is displayed with ascii characters that stand for tabs and breaks. is there a way to erase these?
because i think they are added to the variable .... perhaps ....

another question: is there a way to get rid of the "="s and to make it more flexible in case of a mistyping?thanks

--imhotep--
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: Browsing in a text file and variables

Post by Shine »

imhotep wrote:or if i try to assign a color with a variable, i always get errors, like image load error, or that i didn't do the color rgb syntax correctly.
The reason may be that it is a string and the color constructor expects a number. See the Lua manual how to convert it.

In general maybe it is better to use Lua itself to help you:

level.lua:

Code: Select all

charpic="patrick.png"
health=100 
type="enemy"
"tileset="tiles/patrick_tile.png"
and then simply dofile("level.lua")
imhotep
Posts: 41
Joined: Tue Dec 13, 2005 9:15 pm

Post by imhotep »

yup, thanks.
i'll try to convert it.
but i'd like to work with text files as i don't have to include specific files

i'd like this to be like a database for all objects/obstacles/enemies in the level and their stats etc.

but i guess i could go with your suggestion and just include the lua file with a variable for the file name.

thx
Post Reply