Best way to implement Mapping properties?

I wanna make a GenericMap that will be using from both codes and the properties panel.
But anytime I make a change inside the project, the properties panel refreshed and all of my settings lost.
image

How can I implement a GenericMap for Mapping properties?

My implementation:

GenericMap

const GenericMap = cc.Class({
	InitMap(){
		this.map = {};
		/** WARNING!!! this.data MUST BE A [Pair<key, value>]
		 */
		for(let i = 0; i < this.data.length; i++)
		{
			this.map[this.data[i].key] = this.data[i].value;
		}
	},
	Get(key){
		return this.map[key];
	}
});
module.exports = GenericMap;

SoundHolder

const SoundPair = cc.Class({
	properties: {
		key : "",
		value : cc.AudioClip
	}
});

const SoundMap = cc.Class({
	extends : GenericMap,
	properties : {
		data : {
			default: [],
			type: [SoundPair]
		}
	}
});

var SoundHolder = cc.Class({
	name: "SoundHolder",
	extends : cc.Component,
	properties: {
		music : cc.AudioSource,
		fx : cc.AudioSource,
		sounds : SoundMap,
	},
	start() {
		sounds.InitMap();
	}
});
module.exports = SoundHolder;

I got it! That is the right way to implement Mapping properties. All of Mapping data are lost because serialization error. I just need to add name into the classes.

const SoundPair = cc.Class({
    **name: "SoundPair",**
	properties: {
		key : "",
		value : cc.AudioClip
	}
});

const SoundMap = cc.Class({
    **name: "SoundMap ",**
	extends : GenericMap,
	properties : {
		data : {
			default: [],
			type: [SoundPair]
		}
	}
});
1 Like

Thanks for sharing the information. It is very useful! :slight_smile: