File access in Lua Player

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

Moderators: Shine, Insert_witty_name

Post Reply
essoft
Posts: 2
Joined: Tue Aug 16, 2005 8:13 am

File access in Lua Player

Post by essoft »

I'm trying to port a simplified version of the tile engine in the PC role playing game I'm working on to the PSP. At first I tried to use pspsdk but I couldn't find an easy way to draw 2D graphics and images to the screen (only 3D graphics which I'm not very good with). So instead I decided to use Lua Player which turned out to be really good.

However I'm having some trouble reading from a file. My code successfully reads the first variable from the file but doesn't seem to get any more after that.

Here's an extract from my code from where I access the file:

Code: Select all

...
	success = io.input(file)
	if success == nil then
		screen:print(10,10,"Error - Failed to open level file",white)
	else
		pic = 0
		px = 0
		py = 0
		shift = 0
		
		-- Get the number of tiles in the level
		BLU_num_of_tiles = io.read("*number")
		screen:print(10,10,BLU_num_of_tiles,white)

		-- Scan through the tiles
		q = 0
		while q < BLU_num_of_tiles+1 do
	      	q = q + 1
			
			-- Get tile information from file
			BLU_tile_names&#91;q&#93; = io.read&#40;"*line"&#41;
			screen&#58;print&#40;10,10,BLU_tile_names&#91;q&#93;,white&#41;

			trans = io.read&#40;"*number"&#41;
			tick_rate = io.read&#40;"*number"&#41;

			-- Load the tiles
			BLU_load_tile&#40;BLU_tile_names&#91;q&#93;, q, trans&#41;
		end
...
And here's an extract from the start of the file I'm trying to access:

Code: Select all

162
tiles/grass3.dat
0
48
tiles/blbigcliff.dat
0
1
tiles/brbigcliff.dat
0
0
(first line is the number of tiles to be loaded, then after that there are 162 groups of three variables)

Obtaining the first number from the file (after the "-- Get the number of tiles in the level" comment) works perfectly. But attempts to access the next variables within the while loop fail returning nil.

I put in the screen:print's to make absolutely sure the BLU_tile_names[q] was empty. It seems to be, I get the following error:

Code: Select all

error&#58; index.lua&#58;58&#58; bad argument #2 to `print' &#40;string expected, got nil&#41;
Any idea what I may be doing wrong?
essoft
Posts: 2
Joined: Tue Aug 16, 2005 8:13 am

Post by essoft »

I've solved the problem. It was actually due to the BLU_load_tile(BLU_tile_names[q], q, trans) function I was calling in the loop resetting q to 0 (I didn't know variables of the same name can't be used in different functions in lua). The file access was working all along.
StouffR
Posts: 11
Joined: Thu Sep 15, 2005 11:43 pm

Load a picture

Post by StouffR »

Hi !

I try like you to dev a 2D Game.
I saw, you have find a solution to load it ?

How are you to do ?

Do you know SDL ?
Gary13579
Posts: 93
Joined: Mon Aug 15, 2005 7:43 am

Post by Gary13579 »

What I am doing in my game is just using calling dofile..

Code: Select all

function loadMap&#40;mapID&#41;
    dofile&#40;mapID..".map"&#41;
    --Map&#91;1&#93;&#91;1&#93; contains a 1, &#91;1&#93;&#91;2&#93; contains a 3...


end

Code: Select all

.map file

Map = &#123;
&#123; 1, 3&#125;
&#125;
It works extremly well for me, and I don't have to mess around with any file functions :)
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

@gary
but if someone edits your file and does (not) know what he does, you are bullshitted...
(well thats always possible...)
but if you create your own level definition (which may be easy as hell)
you can check if its correct, otherwise put out an error
if you execute lua code... everything may happen... you do not know before ;)
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

essoft wrote:I didn't know variables of the same name can't be used in different functions in lua
If you use "local" (see Lua manual), it should work.
Gary13579
Posts: 93
Joined: Mon Aug 15, 2005 7:43 am

Post by Gary13579 »

LuMo wrote:@gary
but if someone edits your file and does (not) know what he does, you are bullshitted...
(well thats always possible...)
but if you create your own level definition (which may be easy as hell)
you can check if its correct, otherwise put out an error
if you execute lua code... everything may happen... you do not know before ;)
All they have to do is look at your programs source and figure out how levels are loaded :P
Post Reply