About Monkey 2 › Forums › Monkey 2 Development › Example of config file using Reflection
Tagged: reflection example
This topic contains 1 reply, has 2 voices, and was last updated by 
 nerobot 2 years, 4 months ago.
- 
		AuthorPosts
 - 
		
			
				
December 12, 2016 at 8:41 pm #5759
I just converted some code from Blitzmax that uses Reflection. This is a convenient way of saving config files for things like player controls, screen resolutions and other things you want to keep persistent. Just extend config, add your config fields and use save and load. It’s pretty basic with not much error checking (should players try hacking your config) and only accepts Int, String and Floats but it’s a little starting point and real world usage example if you haven’t tried reflection yet.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126Namespace rigzconfig#Import "<std>"#Import "<reflection>"Using std..#remmonkeydoc Config abstract class for saving and loading simple config filesUses reflection to iterate through every field in the class and save them to a file. You can then load the fileat a later date and repopulate the fields in the file. To use simply extend the Config class with your own Classand create whatever fields you need (Int, String or Float), and use _Save_ and _Load_ to save and load the values.#endClass Config Abstract#remmonkeydoc Output the class fields to a "=" delimeted file@return String#endMethod ToString:String()'Get the TypeInfo of this instanceLocal type:=Self.InstanceTypeLocal output:String'Iterate through each field in the class (note there are no fields in the abstract class, so this will'be looping through the extended class you create yourself for your config classFor Local setting:=Eachin type.GetDecls()If setting.Kind = "Field"'output the name and value of the field. Note that we have to Cast the specific type of each field.output += setting.Name + "="Select String(setting.Type)Case "Int"output += Int(setting.Get(Self))Case "String"output += String(setting.Get(Self))Case "Float"output += Float(setting.Get(Self))DefaultRuntimeError("You can only convert Int, Float or Strings with your config")Endoutput += "~n"EndifNextReturn outputEnd#remmonkeydoc Save this type to a config fileThis will take all of the fields in this type and save them to a config file. You can load them in again at anytime using #LoadConfig@param url String url of the config to save#endMethod Save:Void(url:String)Local output:String = ToString()SaveString(output, url)End Method#remmonkeydoc Load a config into the typeThis will take a configuration file and populate all the fields of the type with the settings found in the config file. Use _Save_ to save a configurationfile@param url String url of the config to load#endMethod Load:Int(url:String)Local configline:StringLocal linearray:String[]Local input:=LoadString(url)Local type:=Self.InstanceTypeLocal lines:=input.Split("~n")For Local line:=Eachin linesLocal pair:=line.Split("=")If(pair.Length=2)'Use GetDecl to to get the DeclInfo of the field in this class based on the name in the config fileLocal decl:=type.GetDecl( pair[0] )If decl And decl.Kind = "Field"'Set the value of each field in the class to the value found in the config fileSelect String(decl.Type)Case "Int"decl.Set( Self, Int(pair[1]) )Case "String"decl.Set( Self, pair[1] )Case "Float"decl.Set( Self, Float(pair[1]) )DefaultRuntimeError("You can only load Int, Float or Strings with your config")EndEndifEndifNextReturn TrueEnd MethodEnd'An example config classClass PlayerProfile Extends ConfigField MoveLeft:Int = 97Field MoveRight:Int = 100Field MoveUp:Int = 119Field MoveDown:Int = 115Field ScreenWidth:Int = 1024Field ScreenHeight:Int = 768Field TestString:String = "Testing a String"Field TestFloat:Float = 3.141EndFunction Main()Local test:PlayerProfile = New PlayerProfiletest.Save("test.cfg")test.TestString = ""test.Load("test.cfg")Print test.TestStringEndDecember 13, 2016 at 8:28 am #5767Good. But only single line strings worked with that plain-text.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.