// /////// // // Methods // // /////// // // Callback invoked when user changes selection method type in editor private void OnChangeSMType() { if (_selectionParams is null) { // Cannot allow this field to be empty, so set it back to what // is specified in the selection method name Debug.Log( $"The {nameof(_selectionParams)} field cannot be empty"); OnChangeSMName(); } else { // Update selection method name accordingly to what is now set // in the generation configurator fields _selectionMethod = SMManager.Instance.GetNameFromType( _selectionParams.GetType()); } }
private void SetGenParams() { // Get reference to the GenerationManager GenerationManager gmInstance = FindObjectOfType <GenerationManager>(); // Get GenerationManager's class type Type gmType = typeof(GenerationManager); // Class type of the selection method configuration Type smType = null; // Generation parameters specific for the selection method IDictionary <string, object> smConfig = null; // Assume no seed strategy for the generation manager's PRNG _genSeedStrategy = null; // Loop through all generation parameter sets in the current experiment foreach (KeyValuePair <string, object> settings in _experiment.GenParamSet[_genParamSet]) { // Check what's the current parameter if (settings.Key.Equals("seedStrategy")) { // If it's a seed strategy (not exactly a parameter), keep it _genSeedStrategy = settings.Value as Func <int, int>; } else if (settings.Key.Equals("_selectionMethod")) { // If it's the selection method type, keep it, we'll use it later smType = settings.Value as Type; } else if (settings.Key.Equals("_selectionParams")) { // If it's the selection method params, keep them, we'll use them later smConfig = settings.Value as IDictionary <string, object>; } else { // If we get here, it's a regular parameter, set it in the // GenerationManager using reflection FieldInfo gmField = gmType.GetField( settings.Key, BindingFlags.NonPublic | BindingFlags.Instance); if (gmField is null) { Debug.LogWarning( $"Unknown {nameof(GenerationManager)} field: '{settings.Key}'"); } else { gmField.SetValue(gmInstance, settings.Value); } } } // If a selection method was specified, configure it if (smType != null) { // Get the existing instance of the current selection method type AbstractSMConfig smCfgInstance = AbstractSMConfig.GetInstance(smType); // Get reference to the GenerationManager's field specifying the // selection method FieldInfo gmFieldSmName = gmType.GetField( "_selectionMethod", BindingFlags.NonPublic | BindingFlags.Instance); // Get reference to the GenerationManager's field specifying the // selection method parameters FieldInfo gmFieldSmCfg = gmType.GetField( "_selectionParams", BindingFlags.NonPublic | BindingFlags.Instance); // Update the generation manager with the experiment-specified // selection method name gmFieldSmName.SetValue( gmInstance, SMManager.Instance.GetNameFromType(smType)); // Update the generation manager with the experiment-specified // selection method configuration gmFieldSmCfg.SetValue(gmInstance, smCfgInstance); // Are any selection method-specific parameters specified in the // experiment? if (smConfig != null) { // Update the selection method configuration with the // experiment-specified selection method parameters foreach (KeyValuePair <string, object> smSettings in smConfig) { FieldInfo smField = smType.GetField( smSettings.Key, BindingFlags.NonPublic | BindingFlags.Instance); if (smField is null) { Debug.LogWarning( $"Unknown {smCfgInstance.GetType().Name} field: '{smSettings.Key}'"); } else { smField.SetValue(smCfgInstance, smSettings.Value); } } } } }