[SMAPI] Detect whether player is outdoors

Brimbane

Farmhand
I would need SMAPI code to check whether the player is outdoors or not.
To be more precise: I need code to abort/leave a function if it is detected that the player is indoors in general (houses, dungeons etc, anywhere but outside in the open).

Concrete example (from the "Wind Effects" mod):
Code:
        public void Shake(IReflectionHelper helper, Vector2 tile)
        {
            // I guess here is where the outdoors check should be
            (NEEDED CODE)

            // already shaking
            if (helper.GetField<float>(this.bush, "maxShake").GetValue() != 0)
                return;

            helper.GetField<bool>(this.bush, "shakeLeft").SetValue(this.left);
            helper.GetField<float>(this.bush, "maxShake").SetValue((float)Math.PI / 128f);
        }
 

Mizzion

Tiller
Something like this will work

C#:
if(Game1.player.currentLocation.isOutdoors.Value)
{
    //Do something here
}
 

Brimbane

Farmhand
When trying this code, the compiler tells me two things:
- "Game1" does not exist in the current context
- "Game1.player.currentLocation.isOutdoors" is a NetBool field and IsOutdoors property should be used instead
 
Last edited:

Mizzion

Tiller
Where are you placing the shake method? And yeah, use the IsOutdoors.

If you're in the SDV discord, you can come to the making-mods channel and get faster help, us modders are usually hanging out there.
 

Brimbane

Farmhand
I am not using Discord for anything, sorry. Just check the Wind Effects source here.

In Frameworks/Shakers, I am editing the following JSONs to prevent the shaking from happening:
BushShaker, FruitTreeShaker, GrassShaker, HoeDirtShaker, TreeShaker

Insertion I intend is like this (example BushShaker.json):
Code:
[...]
namespace WindEffects.Framework.Shakers
{
    internal class BushShaker : IShaker
    {
        private readonly Bush bush;
        private readonly bool left;
      
        public BushShaker(Bush bush, bool left)
        {
            this.bush = bush;
            this.left = left;
        }

        public void Shake(IReflectionHelper helper, Vector2 tile)
        {
            // Outdoor check inserted here
            If (!Game1.player.currentLocation.IsOutdoors.Value)
                return;

            // already shaking
            if (helper.GetField<float>(this.bush, "maxShake").GetValue() != 0)
                return;
          
            helper.GetField<bool>(this.bush, "shakeLeft").SetValue(this.left);
            helper.GetField<float>(this.bush, "maxShake").SetValue((float)Math.PI / 128f);
        }
    }
}
 
Last edited:

Mizzion

Tiller
Your edit should do what you want. You don't need the .Value part though, that was only for the netbool version.
 

Mizzion

Tiller
You shouldn't have that error. I downloaded the source to that mod, and tested it. I got no error at all. If you have the github source forked, can you link your fork for me. I will take a look and see what's happening.
 

Brimbane

Farmhand
I may have made a mistake since I didn't actually excute the global project file StardewMods.sln from the master folder, but the local one (WindEffects.csproj). Dunno if that really made a difference in the end, though. Anyway, I am still getting errors which don't allow me to get this compiled.

My Github fork

One warning is about "IModHelper" being deprecated in SpriteBatchExtensions.cs ("use IGameContentHelper or IModContentHelper instead"), the other errors are about "Game1" not being available in the current context.

Thanks a lot for taking the time to look into this!
 
Last edited:

Mizzion

Tiller
Sorry for the late reply (My wife had a list of chores to do lol.) . At the top of your BushShaker.cs file you will see

C#:
using Microsoft.Xna.Framework;
using StardewModdingAPI;
using StardewValley.TerrainFeatures;
using System;
All you have to do is add to the bottom of the using,

C#:
using StardewValley;
That should fix that error.
 

Brimbane

Farmhand
Got it working now by adding the line you mentioned to all affected files that did not have it. Also managed to fix that "IModHelper" warning in the meantime.

*UPDATE*
Tested ingame and seems to work as intended. Finally no wind in the greenhouse or dungeons! Thanks a lot for your help!
 
Last edited:
Top