1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Hydrogen Tanks vs. Oxygen Tanks

Discussion in 'Programming Questions and Suggestions' started by Seamus Donohue, Aug 13, 2019.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Seamus Donohue

    Seamus Donohue Trainee Engineer

    Messages:
    90
    When using IMyGasTank, is there a way (OTHER THAN parsing .CustomName) to determine if a particular tank holds Hydrogen or Oxygen?
     
  2. urthsong

    urthsong Trainee Engineer

    Messages:
    18
    The only other way I know of is to get it from DetailedInfo

    i.e. if tank is a block of type IMyGasTank,

    var gasType = tank.DetailedInfo.ToString().Split('\n')[0].Split(':')[1].Trim();

    For oxygen, I think the type will be "Oxygen Tank"
     
  3. Seamus Donohue

    Seamus Donohue Trainee Engineer

    Messages:
    90
    ...which also requires parsing. Okay, thanks.

    I know Malware has mentioned, before, that parsing is slow and should be avoided whenever possible.
     
  4. domingo

    domingo Trainee Engineer

    Messages:
    46
    Thus do it only on initialization and keep the result cached.
     
  5. Malware

    Malware Master Engineer

    Messages:
    9,867
    Code:
    
    static readonly MyDefinitionId Hydrogen = MyDefinitionId.Parse("MyObjectBuilder_GasProperties/Hydrogen");
    static readonly MyDefinitionId Oxygen = MyDefinitionId.Parse("MyObjectBuilder_GasProperties/Oxygen");
    	
    enum GasType
    {
    	None,
    	Hydrogen,
    	Oxygen
    }
    
    GasType GetContentType(IMyGasTank tank)
    {
    	MyResourceSinkComponent sinkComponent;
    	if (tank.Components.TryGet(out sinkComponent))
    	{
    		var resources = sinkComponent.AcceptedResources;
    		if (resources.Contains(Hydrogen)) 
    			return GasType.Hydrogen;
    
    		if (resources.Contains(Oxygen))
    			return GasType.Oxygen;
    	}
    	return GasType.None;
    }
    
    warning untested code, may have typos and brainfarts
     
  6. Seamus Donohue

    Seamus Donohue Trainee Engineer

    Messages:
    90
    I appreciate this, thank you! I'll test this out later! :D
     
Thread Status:
This last post in this thread was made more than 31 days old.