How would you go about this ...?

Monkey Forums/Monkey Programming/How would you go about this ...?

Lindsay(Posted 2014) [#1]
Hi everyone! I'm new to Monkey but not to programming - I started in 1980, and have used Basic, C/C++, Pascal, dBase, and about 6 other languages whose names I've long since forgotten :)

For the game I'm currently designing, I need to select a single, random string from a list of strings. I don't need that list of strings in memory - I'm only interested in one at a time.

My ideal approach would be, store them in a text file, one item per line, and just read and discard X lines until I reach the one I need.

Is there this sort of file I/O in Monkey? Can I have text files as part of the project?

Failing that -- how else could I approach it?

BTW, I'm talking about a significant list - definitely hundreds, perhaps 1,000 or more strings, which is why I don't want them all in memory all the time!

Oh, one other thing - cross platform compatibility is essential!

Thanks,
Lindsay


darky000(Posted 2014) [#2]
Yes you can do that. You can replace your variable string until it matches to the item you are searching for.

Here is a snippet of a code I quickly did. You can also change all items to ToUpper() to check the item regardless if the item searched is capitalized or not. You can do your own foolproof but this should work.

Field itemTxt
Method LoadItemList:Int()
	itemTxt = LoadString("item.txt") 'This loads the .txt
		
	If itemTxt.IsEmpty() Then
		Error("No File list is found")
	Endif

	Return 0
End Method

Method GetItem:Int(searchItem:String)
		LoadItemList()
		
		Local lines:String
                Local ly:Int
				
		lines = itemtxt.Split(String.FromChar(10)) 'Will split when it finds an enter command
		ly = lines.Length()
		
		
		Local num:Int = 0
		While num < ly 
			If lines[num].Compare(searchItem) = 0 Then
				Print("Do what you want with this item")
                                Exit
			Endif 
			
			num += 1
		Wend
		
		If num = ly Then
			Print("Item not found")
		Endif
			
		Return 0
End Method






You can also use the ReadInt() if you want to read the txt file by counts and work it out from there. Check the Class Stream on the document for more information. I can create a smaple code for you for this if you want but I'm still at work so I can do it later on if you like.


Lindsay(Posted 2014) [#3]
Thanks darky000 - that's plenty for me to go on with. Do you happen to know what the maximum length of a string is?

Regards,
Lindsay


Trez(Posted 2014) [#4]
No idea what the max length is for s String type but it may be platform dependent, anyway a lot more than you probably need to worry about.

If you don't want to load all the words in one batch simply create x text files that are numbered , e.g. wordlist1.txt, wordlist2.txt etc store say 100 words in each list.

Then when you load the list just generate a random number between 1 and the maximum number of lists you have and append this number to the filename. "wordlist" + x + ".txt"

The rest you can do using the method described by darky000. As you want a random word just generate another random number using Rnd() and select a word from the array generated when using Split.

This way you can just keep adding new word lists to your game to extend it.

Hope that helps a little.


Gerry Quinn(Posted 2014) [#5]
Pretty sure you can rely on 30k chars anyway. in practice I'd expect you can have strings of several Mb on most platforms.


TeaBoy(Posted 2014) [#6]
Wow, excellent art BTW, especially the top picture on your site ;)


Lindsay(Posted 2014) [#7]
Thanks everyone! I've been looking at the available classes and think a databuffer and datastream might be a good way to go as I can pack the data in quite tightly but also seek directly to the beginning of the string I need, which is more efficient than reading through potentially the whole file to find it.

Thanks Teaboy -- the great irony of my life is when I was writing games in the 80's, my one sticking point was being hopeless at creating game art. I still am, but I can paint pretty well with traditional materials, which is something I didn't even start doing until 2001. Down the track I'd love to write a game like Machinarium, the (hand drawn) art in that is gorgeous :)