playMaker

Author Topic: Date actions: day of the week, month length...  (Read 8347 times)

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Date actions: day of the week, month length...
« on: February 09, 2016, 03:43:12 AM »
Hi,

Some more DATE actions would be very handy for us non programmer types.

With Get System Date Time: https://hutonggames.fogbugz.com/default.asp?W823
we can already get the year, month and day. That is great!

An action to calculate the length of any particular month would be nice to have. (give year and month, get number of days in month)
Also an action to tell which weekday any particular year/month/day is.

Thanks in advance,
szomaza

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Date actions: day of the week, month length...
« Reply #1 on: February 16, 2016, 01:43:35 PM »
Look what I did!  :-)
I reverse engineered the Int Add action to get familiar with stuff, then looked up a date function (DateTime.DaysInMonth Method (Int32, Int32)) and could solve my first problem with my very first PlayMaker action: YAY!

Code: [Select]
// https://msdn.microsoft.com/en-us/library/system.datetime.daysinmonth(v=vs.110).aspx
// ArgumentOutOfRangeException
// month is less than 1 or greater than 12.
// -or-
// year is less than 1 or greater than 9999.

using UnityEngine;
using System;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Time)]
    [Tooltip("Returns the number of days in the specified year and month.")]
    public class GetNumberOfDaysInMonth : FsmStateAction
    {
        [RequiredField]
        public FsmInt year;
        [RequiredField]
        public FsmInt month;
        public FsmInt numberOfDays;

        public override void Reset()
        {
            year = null;
            month = null;
            numberOfDays = null;
        }

        public override void OnEnter()
        {
            numberOfDays.Value = DateTime.DaysInMonth(year.Value, month.Value);
        }
    }
}

Now I'm off to do the research for the 2nd one: "which weekday any particular year/month/day is" .

Any improvements are appreciated. Like handling that exception when a higher number then 12 is entered for the month, etc to make it fool proof.
Also feel free to add "Copyright HutongGames" if you think this is useful, and please add it to the Ecosystem so I can easily find it at later times.

Thanks in advance:
szomaza

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Date actions: day of the week, month length...
« Reply #2 on: February 17, 2016, 09:38:35 AM »
The 2nd one is done too.
Get day of week for specific year, month, day:

Code: [Select]
// https://msdn.microsoft.com/en-us/library/bb762911%28v=vs.110%29.aspx

using UnityEngine;
using System;
using System.Globalization;
using System.Threading;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Time)]
    [Tooltip("Returns day of the week (Monday, Tuesday, ...) for the specified year, month, day.")]
    public class GetDayOfWeek : FsmStateAction
    {
        [RequiredField]
        public FsmInt year;
        [RequiredField]
        public FsmInt month;
        [RequiredField]
        public FsmInt day;

        public FsmString dayOfWeek;

        public override void Reset()
        {
            year = null;
            month = null;
            day = null;
            dayOfWeek = null;
        }

        public override void OnEnter()
        {
            CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

            //DateTime dateValue = new DateTime(year.Value, month.Value, day.Value);
            //dayOfWeek.Value = dateValue.ToString("dddd", new CultureInfo("en-US"));

            DateTime dateValue = new DateTime(year.Value, month.Value, day.Value);
            dayOfWeek.Value = dateValue.ToString("dddd");
           
            Thread.CurrentThread.CurrentCulture = originalCulture;
        }
    }
}

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Date actions: day of the week, month length...
« Reply #3 on: February 17, 2016, 09:39:25 AM »
Looks like this:

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Date actions: day of the week, month length...
« Reply #4 on: February 17, 2016, 09:42:13 AM »
These will work for me now, but I would really appreciate if somebody who knows what he/she is doing would "patch these up", handling exceptions, whatever... to make them "fool proof" and add them to the Ecosystem, so later it is easier to find.

Thanks in advance,
szomaza

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Date actions: day of the week, month length...
« Reply #5 on: February 18, 2016, 03:04:07 AM »
The Day Of The Week I originally wanted to get as a number but could not figure out how to do that.
I read it is possible though: Sunday is 0, Monday 1, etc.

Improvements to the actions could be adding this as a possible result and also the possibility to select locale from a drop down list, when getting the name of the day.

Br,
szomaza

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Date actions: day of the week, month length...
« Reply #6 on: February 22, 2016, 01:59:09 AM »
Started using them and noticed that both are missing
Finish();
from the end of the OnEnter() part.

Without this they work but then in the FSM they don't continue on to the next state.
So add that if you use these or you can always add a next frame event or a wait 0 to achieve the same.

Br,
szomaza

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15537
  • Official Playmaker Support
Re: Date actions: day of the week, month length...
« Reply #7 on: March 02, 2016, 02:40:35 AM »
Hi,

 you can actually add them to the Ecosystem yourself very easily:


Let me know how it goes or there is a step in the process you don't understand.

 Bye,

 Jean

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Date actions: day of the week, month length...
« Reply #8 on: March 11, 2016, 02:28:45 AM »
Thanks Jean for replying.

Yes I could add them but as I wrote, unfortunately these just barely work, there are no checks, they can easily crash if given wrong values, etc... so I would not want them in the Ecosystem in this state.

Unfortunately I can't, so I just hope somebody is interested and will bring them up to the quality we are used to from you guys.

Br,
szomaza

szomaza

  • Sr. Member
  • ****
  • Posts: 253
Re: Date actions: day of the week, month length...
« Reply #9 on: November 17, 2017, 01:48:08 AM »
Warning!
Anybody using my date actions, be careful as they seem to be causing some serious problems now with other playmaker actions when on Unity 2017.1.1f1 and Playmaker 1.8.5.f8.

Some more details about the problem here:
http://hutonggames.com/playmakerforum/index.php?topic=16041.0
and I'll add the solution here as soon as there will be one.

Sorry for this, seems like my coding proficiency does not stand the test of time.

Hoping there will be a foolproof solution to these "simple" date actions soon,
szomaza

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15537
  • Official Playmaker Support
Re: Date actions: day of the week, month length...
« Reply #10 on: November 22, 2017, 01:35:47 AM »
Hi,

the fix I did works isn't it?

 Bye,

 Jean