Issue With Array : Setting variables

I have an array of structs, which holds 2 int32 and a FString. Those are “Day”, “Month” and “Name”. Users can add a new item to the array in the blueprint graph- these represent “holidays”. I have another c++ function / BP node for getting the holiday name (HolidayName).

Now, I have another function that checks if it’s a holiday (CheckHoliday), which I check every time a day has passed. My issue, is when iterating over the array in the CheckHoliday function. I set HolidayName to the Name from the structure, if the current day/month match the day/month for the structure in the current index. Otherwise, I set HolidayName to “None”. Everything works fine, for the most part.

I have an event I broadcast (OnHoliday), and in my test BP I print out "Holiday: " + HolidayName. Now on the tick event in the BP, I am printing the HolidayName constantly, but it only ever prints “None”. I’m having a hard time understanding why 2 different things are printing out, when everything else seems to be right in the universe. My goal is to set the HolidayName string to “None” when it isn’t a holiday, otherwise set it to the current holiday’s name. Here is my CheckHoliday snippet:

void CheckHoliday()
{
	if (HolidayArray.Num() > 0)
	{
		for (int32 i = 0; i != HolidayArray.Num(); ++i)
		{
			if (Day == HolidayArray[i].Day && Month == HolidayArray[i].Month)
			{
				HolidayName = HolidayArray[i].Name;

				OnHoliday.Broadcast();
			}
			else
			{
				HolidayName = "None";
			}
		}
	}
}

I’m really rusty on my C++, but this seems like it should work fine. Any insight would be greatly appreciated!

I think the problem with your code is that even if it is a holiday, HolidayName will be set to “None” because the loop continues iterating. One possibility is to add a break after the broadcast, the other one is the following:

    void CheckHoliday()
     {
         if (HolidayArray.Num() > 0)
         {
             HolidayName = "None";

             for (int32 i = 0; i != HolidayArray.Num(); ++i)
             {
                 if (Day == HolidayArray[i].Day && Month == HolidayArray[i].Month)
                 {
                     HolidayName = HolidayArray[i].Name;
     
                     OnHoliday.Broadcast();
                 }
             }
         }
     }

I don’t know if this solves your problem, but it would be the first step.

Yes indeed! Moving the HolidayName = “None”; outside of the loop fixed it. I’ve smacked myself with this before when working with iterations, heh.

Thanks for the help, much appreciated!