Dan

Moderator
Code:
public static void OnLogin(LoginEventArgs e)
        {
            if ( e.Mobile != null && e.Mobile is PlayerMobile && e.Mobile.AccessLevel == AccessLevel.Player && e.Mobile is Young)
                Timer.DelayCall(TimeSpan.FromSeconds(1), () =>
                    {
                        if (!e.Mobile.HasGump(typeof(YoungSystemGump)))
                            e.Mobile.SendGump(new YoungSystemGump(e.Mobile));
                    });
        }

I am trying to make this gump only show to young players. I have tried every variant of

e.Mobile.Young == true;
e.Mobile.Young == Young.True.

but always get the same few errors. I know I am referencing it wrong but can not for the life of me figure this one out. Any help would be greatly appreciated.
 
Try this:

Code:
if ( e.Mobile != null && e.Mobile is PlayerMobile && e.Mobile.AccessLevel == AccessLevel.Player && ((PlayerMobile)e.Mobile).Young)
 
Works great thanks! If you were to translate the ((PlayerMobile)e.Mobile).Young) into...."English" for teaching purposes how does it read? Can I add anything in place of PlayerMobile? like (Dog)e.Mobile , just trying to learn too.
 
If the e.Mobile is a PlayerMobile type, then we can "cast" the PlayerMobile type to e.Mobile. What this is saying is basically:

Give me the Young property of e.Mobile, assuming e.Mobile is a PlayerMobile and therefore has a Young property.

Yes, if you say this:

Code:
if (e.Mobile is Dog)

then you can say this:

Code:
if (e.Mobile is Dog && ((Dog)e.Mobile).HouseTrained) // then
    //Let the dog stay indoors all day unsupervised
 
it's a short hand way of doing this:

PlayerMobile pm = (PlayerMobile)e.Mobile;

and doing this: pm.Young

instead of (PlayerMobile)e.Mobile).Young

However, as @Ravenwolfe previously advised that if you do the shorthand casting you want to make sure to check for nulls.
@Lokai s codes does: e.Mobile != null
 
Another thing about casting, which I learned the hard way.

Code:
PlayerMobile pm = e.Mobile as PlayerMobile

pm will be null if e.Mobile is not a playermobile, so you will need a null check.

Code:
PlayerMobile pm = (PlayerMobile)e.Mobile

if e.Mobile is not a PlayerMobile, you will throw an invalid cast exception, so a check is always needed either way you do it.
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back