Skip to content

Developer Reference Sample Config

Kevin Hoddinott edited this page Oct 5, 2020 · 1 revision

This is a sample Config class

This isn't the only way to do it but felt this might be the best way to help get modders started.

/*
Sample Config Class
   This is just a template on how you could build your config classes you can inherit this 
but at the end of the day this is just a template to help modders newer to API something 
to start from
*/
class SampleUApiConfig : UApiConfigBase {
        string ModVersion = "2";
	string GUID = "";
         //Sample loader for global configs
        override void Load(){
                super.Load(); //this marks the data as not received and will call set defaults
                UApii().Rest().GlobalsLoad("SampleMod", this, this.ToJson());
	}
	
        //Sample loader for player data
	override void Load( string ID){
		super.Load(ID); //this marks the data as not received and will call set defaults
		GUID = ID;
		//Load mod data from the player object in the database
                 UApi().Rest().PlayerLoad("SampleMod", GUID, this, this.ToJson());
	}
	

	//Sample save
	override void Save(){
		
		if (GetGame().IsServer()){	//By Default the API is configure to only allow save operations from the server AUTH
		
			//Global Configs
			UApi().Rest().GlobalsSave("SampleMod", this.ToJson());
		
			//Player Configs
			UApi().Rest().PlayerSave("SampleMod", GUID, this.ToJson());
		}
	}
	
	override void SetDefaults(){
		/*
	
		  This is to set the defaults for the mod before requesting a load so that way 
		if it doesn't exsit the API will create the file
	
		*/
	}
	
	override string ToJson(){
		// Override and Replace with your class Name this converts this class to JSON
		string jsonString = JsonFileLoader<SampleUApiConfig>.JsonMakeData(this); 
		return jsonString;
	}
	
	override void OnDataReceive(){
		SetDataReceived();
		if(ModVersion != "2"){
                        ModVersion = "2";
			//DoSome Code Upgrade
		
			Save(); //Resave the upgrade Version Back to the server
		}
	}
	
	
	// This is called by the API System on the successfull response from the API
	override void OnSuccess(string data, int dataSize) {
		JsonFileLoader<SampleUApiConfig>.JsonLoadData(data, this);
		if (this){
			OnDataReceive();
		}
	};
	
	// This Are Called by DayZ the API System on errors from the API System you can use them to run check or to load try again later
	override void OnError(int errorCode) {};
	override void OnTimeout() {};
}