Giving players 100% comfort level when near heat?
Wxll
The plugin im trying to get to work gives players 100% comfort when they're at a heat source. The code that I'm using (posted in an old oxide thread) sets the players comfort to 100% but then slowly decreases to 50% and im not sure why. Any help would be appreciated :)

void OnEntityEnter(TriggerBase trigger, BaseEntity entity)
{
var player = entity.ToPlayer();
if(player != null && trigger.name.Equals("heatSource")) player.metabolism.comfort.value= 1;
}​
Gachl
Plugin Developer
I suppose the comfort has a natural tendency to increase or decrease towards the correct level, since you're only setting comfort to 100% when someone enters the heatSource it will slowly drift. You may have to keep setting the value while the player is within the heatSource.
Wxll
Original Poster
In response to Gachl (View post):
I suppose the comfort has a natural tendency to increase or decrease towards the correct level, sinc...
I'm struggling to get this to work, ive made a timer but now it stays at 100% even when the players not at a heat source.
		void OnEntityEnter(TriggerBase trigger, BaseEntity entity)
		{
		var player = entity.ToPlayer();
		if(player != null && trigger.name.Equals("heatSource")){
			player.metabolism.comfort.value= 1;
			timer.Repeat(0.1f, 0, () =>
			{
				player.metabolism.comfort.value= 1;
			});
		}
		}​
misticos
Plugin Developer
Sure, because you don't stop the timer
Wxll
Original Poster
In response to misticos (View post):
Sure, because you don't stop the timer
Yeah.... I'm a complete noob with this stuff :/ could you help me out with the code? I'd really appreciate it :)
2CHEVSKII
Plugin Developer
        Timer myTimer;
        
        void StartTimer()
        {
            myTimer = timer.Every(0.1f, () =>
            {
                //code here
            });
        }

        void StopTimer()
        {
            myTimer.Destroy();
            //or
            timer.Destroy(ref myTimer);
        }


Merged post

Actually, I think it's better to use custom component instead of timer or any other method. This is kinda rough, but will work. 
        void Loaded() { foreach(var player in BasePlayer.activePlayerList) AttachComponent(player); }

        void OnPlayerInit(BasePlayer player) => AttachComponent(player);
        
        void AttachComponent(BasePlayer player) => player.gameObject.AddComponent<MetabolismChanger>();

        void Unload() { foreach(var player in BasePlayer.activePlayerList) player.GetComponent<MetabolismChanger>().DetachComponent(); }

        class MetabolismChanger : MonoBehaviour
        {

            public BasePlayer player;

            public bool isNearFireplace;

            void Awake()
            {
                player = GetComponent<BasePlayer>();
                isNearFireplace = false;
            }

            void Update() { if(isNearFireplace) player.metabolism.comfort.value = player.metabolism.comfort.max; }

            public void DetachComponent() => Destroy(this);
                
        }
        
        void OnEntityEnter(TriggerBase trigger, BaseEntity entity)
        {
            if(trigger.name.Contains("heatSource"))
            {
                BasePlayer player = entity.ToPlayer();
                if(player != null)
                    player.GetComponent<MetabolismChanger>().isNearFireplace = true;
            }
        }

        void OnEntityLeave(TriggerBase trigger, BaseEntity entity)
        {
            if(trigger.name.Contains("heatSource"))
            {
                BasePlayer player = entity.ToPlayer();
                if(player != null)
                    player.GetComponent<MetabolismChanger>().isNearFireplace = false;
            }
        }​
Wxll
Original Poster
In response to 2CHEVSKII (View post):
Timer myTimer; void StartTimer() { myTimer = timer.Ever...
Works first time! Huge thanks for the help man I really appreciate it :)
2CHEVSKII
Plugin Developer
In response to Wxll (View post):
Works first time! Huge thanks for the help man I really appreciate it :)
I hope You used method with component which I provided instead of timer.
Wxll
Original Poster
In response to 2CHEVSKII (View post):
I hope You used method with component which I provided instead of timer.
I did :) thanks again