SUGGESTION (for users): Generating Code in Access, etc.

Started by Charity, Thu 07/01/2010 23:00:33

Previous topic - Next topic

Charity

So, I had an idea.  Don't know if anyone has tried this.  (Apologies if this is the wrong forum, but I couldn't think of one that was more appropriate.)

Say you are making an RPG, or another game that utilizes a large number of arrays and structs.  The numbers of variables that need to be set can increase rapidly, (exponentially in some cases), and while some of these variables can be set automatically through the script, many of them will need to be set manually, at least for their initial value.  This is the unavoidable cost of creating stats for large numbers of monsters, characters, items, or whatever other entities your game just can't get enough of.  What you can avoid, however, is the tedious copy-pasting of
Code: ags

guy[1].Name = 
guy[1].HP =
guy[1].Level =
guy[1].Etc. Etc.

Repeating with guy[2] and guy[3] and guy[256].

If you have Microsoft Access, or a similar program, all you need to do is input the relevant variables into a spreadsheet.  Input your variables (ID#, Name, HP, Level, etc.) in the columns and instances of the struct (be it describing characters, items, etc.) in the rows.  Then you just tell Access to generate a report or label with the data displayed in a format like (Access Psuedocode:)
Code: ags

guy[{ID#}].Name = "{Name}";
guy[{ID#}].HP = {HP};
guy[{ID#}].Level = {Level};

and then paste the resulting text into on_game_start()

When I tried this I wasn't able to copy the label text straight from Access, so I dumped it to a plain text file and copied it from there.

Also note that Access uses certain characters (such as {}, and I think quotes, actually) in its label generator, so you may need to use placeholder characters to signify those characters and then run a Replace All in notepad before copying into AGS.

Obviously, this won't be worth it for structs with only a couple instances, but for large ones, it might save you a bit of work.

I would not be at all surprised if a similar effect could be accomplished using Excel or another simpler spreadsheet program, but I haven't tried.  Another thought I had was programming a simple utility in AGS itself to generate simple code in a String and dump it into a .txt file.

Has anyone else used a similar (or dissimilar) method to generate AGS script and found it useful, or for that matter entirely unuseful (too much work or producing problematic code)?  Feel free to share your ideas/experiences here (or point out problem areas you see in this method or others), as this information could be useful.

Khris

I was planning on using a similar method for my tile map data. I'd write an editor that's able to import, load and save tile maps; the finished map would be exported as a text file that can then be directly pasted into a module's script.

Clarvalon

If it's a requirement to have the initial values hardcoded into the game then this sounds like a reasonable shortcut.

However, it may be more practical to save the spreadsheet as a .csv file and simply read the values for each item in one row at a time at the beginning of the game (not that I know much about AGS's IO functionality).
XAGE - Cross-Platform Adventure Game Engine (alpha)

RickJ

Do what clarvalon says.  The String Extender function, String.StrToken() shown below allows one to easily read CSV values.  Here is an example of how to use it.  You would of course want to put this example in a while loop that reads each line from the csv file and then executes the tokenizer code.

You could also use StrToken() to copy the CSV file to a binary file and then read the binary file on game startup.   You could then distribute only the binary version of the file with your game.

Code: ags

	struct guy {
		String Name;
		int HP; 
		int Level;
		int Etc;
	}
	guy Guy[100];
	int id;
	String line, buf;

	// Id, name, HP, Level, Etc
	line = "1, Bob, 100, 20, 50";
	id = Line.StrToken(',', true);
	Guy[id].Name = Line.StrToken(',');
	buf = Line.StrToken(',');
	Guy[id].HP = buf.AsInt();
	buf = Line.StrToken(',');
	Guy[id].Level = buf.AsInt();
	buf = Line.StrToken(',');
	Guy[id].Etc = buf.AsInt();



String Extender Function - String.StrToken()
Code: ags

*** Script Header ***
import String StrToken(this String*, char c, bool reset=0) 

*** Global Script ***
//================================================================== 
	String 	TokenString;
	int 		TokenIx=0;
//
	String StrToken(this String*, char c, bool reset) {
//
// Emulates operation of standard C language strtok() function. When
// this function is first called, for a given string value, it 
// returns the first occurance of the specified separator character.  
// Subsequent calls return the next occurance of the specified token 
// character.
//
// The parameter C specifies the token seperator character to use 
// for this call.  A different character may be specified eaxch time 
// this function is called.
//
// The optional reset parameter fporces the tokenization process to 
// start over from the begining of the lione.
//
//    Example:
//
//    String Line;
//    Line = "Goodbye, cruel world. Farewell!");
//    Display(Line.StrToken(' ',true);
//    Display(Line.StrToken(',');
//    Display(Line.StrToken('!');
//    Display(Line.StrToken(',',true);
//
//    Displays the following ...
//    Goodbye,
//    cruel world
//    Farewell
//    Goodbye
//-------------------------------------------------------------------
	int 		i, j, t;
	String 	token;

	// Initialize tokenizer 
	if ((TokenString==null)||(TokenString!=this)||(reset)) {
		if (this!=null) TokenString = this;
		else TokenString = "";
		TokenIx = 0;
	}
	// Check for end of string
	if (TokenIx<this.Length) {
		
		// Skip leading whitespace
		token = "";
		i = TokenIx;
		while ((i<this.Length)&&(this.Chars[i]<=' ')) i++;
		j = i;	// Save token beginning
	
		// Get token end
		while ((i<this.Length)&&(this.Chars[i]!=c)) i++;
	
		// Save token index for next time
		TokenIx = i+1;

		// Extract token from string
		token = this.Substring(j, i-j); 
	}
	else {
		// Token index, TokenIx, is greater than the length so ...
		// since there are no more tokens return an empty string
		token = "";
	}
	return token;
}



SMF spam blocked by CleanTalk