| View previous topic :: View next topic |
| Author |
Message |
essoft
Joined: 16 Aug 2005 Posts: 2
|
Posted: Tue Aug 16, 2005 8:53 am Post subject: File access in Lua Player |
|
|
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: |
...
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[q] = io.read("*line")
screen:print(10,10,BLU_tile_names[q],white)
trans = io.read("*number")
tick_rate = io.read("*number")
-- Load the tiles
BLU_load_tile(BLU_tile_names[q], q, trans)
end
...
|
And here's an extract from the start of the file I'm trying to access:
| Code: |
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: | | error: index.lua:58: bad argument #2 to `print' (string expected, got nil) |
Any idea what I may be doing wrong? |
|
| Back to top |
|
 |
essoft
Joined: 16 Aug 2005 Posts: 2
|
Posted: Tue Aug 16, 2005 6:57 pm Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
StouffR
Joined: 15 Sep 2005 Posts: 11
|
Posted: Tue Sep 20, 2005 10:49 pm Post subject: Load a picture |
|
|
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 ? |
|
| Back to top |
|
 |
Gary13579
Joined: 15 Aug 2005 Posts: 93
|
Posted: Sat Sep 24, 2005 12:16 am Post subject: |
|
|
What I am doing in my game is just using calling dofile..
| Code: |
function loadMap(mapID)
dofile(mapID..".map")
--Map[1][1] contains a 1, [1][2] contains a 3...
end
|
| Code: |
.map file
Map = {
{ 1, 3}
}
|
It works extremly well for me, and I don't have to mess around with any file functions :) |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Sat Sep 24, 2005 1:31 am Post subject: |
|
|
@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 |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sat Sep 24, 2005 1:52 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
Gary13579
Joined: 15 Aug 2005 Posts: 93
|
Posted: Sat Sep 24, 2005 8:21 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
|