Forum

> > CS2D > Scripts > Input More Than First Line
Forums overviewCS2D overview Scripts overviewLog in to reply

English Input More Than First Line

9 replies
To the start Previous 1 Next To the start

old Input More Than First Line

KaBooMa
User Off Offline

Quote
Hey, So i asked something a little similar a bit back. Can someone tell me how to input from line 2 and line 3 from a txt file? I got it saving 15 things into a file. I need to load each one and load them in the right spot. When i load 1 i get the saved x. It then saves X[id] to that. at end of all this it sets player pos to X[id] and Y[id]. But thats beside the point. If someone could show me a little example on how to load line 1 then line 2 then line 3. or something along the lines. Id be happy

old Re: Input More Than First Line

Yasday
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
local a = 0;
local LINE_VALUE = "";
local LINE_TO_READ = 5;
for lines in io.lines(FILE_PATH) do
	a = a + 1;
	if a == LINE_TO_READ then
		LNE_VALUE = lines
	end
end
A little example.

old Re: Input More Than First Line

Banaan
User Off Offline

Quote
You could use io.lines in a for loop:
1
for line in io.lines("save.txt") do

or read the entire content of the file and split it at each newline ("\n"). But that would require writing a string:split function first since it isn't included in the Lua standard library...

old Re: Input More Than First Line

palomino
User Off Offline

Quote
Banaan has written
You could use io.lines in a for loop:
1
for line in io.lines("save.txt") do

or read the entire content of the file and split it at each newline ("\n"). But that would require writing a string:split function first since it isn't included in the Lua standard library...

Would be great if somebody made a non-standard library...

old Re: Input More Than First Line

KaBooMa
User Off Offline

Quote
Thanks Everyone for the help! I was making like 16 different text files to load from I couldn't find it out. Now i have 1 COOKIES FOR EVERYONE!!
cookie

old Re: Input More Than First Line

Banaan
User Off Offline

Quote
Sunny Autumn has written
Banaan has written
You could use io.lines in a for loop:
1
for line in io.lines("save.txt") do

or read the entire content of the file and split it at each newline ("\n"). But that would require writing a string:split function first since it isn't included in the Lua standard library...

Would be great if somebody made a non-standard library...


I think Lee's AMX2D includes quite a lot of useful functions for string/table manipulation (core/string.lua and core/table.lua). Though one can find (or write themselves) better alternatives (there are many ways to reach the same result, e.g. about 15 different trim functions) for most of his functions, I think it's a really useful addition.

It's a shame he used string.trim rather than string:trim though...
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview