Exception encountered (after date function)

Started by pcj, Thu 26/08/2004 03:35:22

Previous topic - Next topic

pcj

I received the following exception:

Quote---------------------------
Illegal exception
---------------------------
An exception 0xC0000005 occured in ACWIN.EXE at EIP = 0x0044D1FE ; program pointer is -42, ACI version 2.61.747, gtags (2039,36)

AGS cannot continue, this exception was fatal. Please note down the numbers above, remember what you were doing at the time and notify CJ on the Tech forum.

(Global script line 108)

Line 108 is the last line of this function:

Code: ags
function Now(int type){ // Returns the current time/day/date/year by the computer clock.
if (type == 1) { // Current year
int Raw_Seconds = GetRawTime(); 
int Raw_Minutes = Raw_Seconds / 60;
int Raw_Hours = Raw_Minutes / 60;
int Raw_Days = Raw_Hours / 24;
int Raw_Years = Raw_Days / (365 + (1/4));
int Year = Raw_Years + 1970;
return Year;
}
else if (type == 2) { // Current time
string TimeNow;
string TimeNow2;
string TimeNow3;
int hour = GetTime(1);
int minute = GetTime(2);
string AMPM;
if (hour > 12) {
hour-=12;
StrCopy(AMPM, "pm");
}
else if (hour == 0) {
hour=12;
StrCopy(AMPM, "am");
}
else { StrCopy(AMPM, "pm"); }
if (minute < 10) StrFormat(TimeNow2, "0%d", minute);
else StrFormat(TimeNow2, "%d", minute);
StrFormat(TimeNow, "%d", hour);
StrCat(TimeNow, ":");
StrCat(TimeNow, TimeNow2);
StrFormat(TimeNow3, " %s", AMPM);
StrCat(TimeNow, TimeNow3);
SetGlobalString(49,TimeNow);
}
else if (type == 3) { // Current date
int month = GetTime(5);
int day = GetTime(4);
string date1;
string date2;
string date3;
StrFormat(date1, "%d-", month);
StrFormat(date2, "%d-", day);
StrFormat(date3, "%d", Now(1));
StrCat(date1, date2);
StrCat(date1, date3);
SetGlobalString(49,date1);
}
else if (type == 4) { // Current day (e.g., Monday, Tuesday)
int Year = Now(1) - 2000;
Year += (Year / 4);
int MonthNum;
if (GetTime(5) == 1) MonthNum = 6;
else if (GetTime(5) == 2) MonthNum = 2;
else if (GetTime(5) == 3) MonthNum = 2;
else if (GetTime(5) == 4) MonthNum = 5;
else if (GetTime(5) == 5) MonthNum = 0;
else if (GetTime(5) == 6) MonthNum = 3;
else if (GetTime(5) == 7) MonthNum = 5;
else if (GetTime(5) == 8) MonthNum = 1;
else if (GetTime(5) == 9) MonthNum = 4;
else if (GetTime(5) == 10) MonthNum = 6;
else if (GetTime(5) == 11) MonthNum = 2;
else if (GetTime(5) == 12) MonthNum = 4;
int DayCode = Year + MonthNum + GetTime(4);
if ((Now(1) == 2004 || Now(1) == 2008 || Now(1) == 2012 || Now(1) == 2016 || Now(1) == 2020 || Now(1) == 2024 || Now(1) == 2028 || Now(1) == 2032 || Now(1) == 2036 || Now(1) == 2040 || Now(1) == 2044 || Now(1) == 2048) && (GetTime(5) == 1 || GetTime(5) == 2)) DayCode--;
while (DayCode > 7) {
Ã,  DayCode -= 7;
}
while (DayCode < 1) {
Ã,  DayCode += 7;
Ã,  }
string Day;
if (DayCode == 1) StrCopy(Day, "Monday");
else if (DayCode == 2) StrCopy(Day, "Tuesday");
else if (DayCode == 3) StrCopy(Day, "Wednesday");
else if (DayCode == 4) StrCopy(Day, "Thursday");
else if (DayCode == 5) StrCopy(Day, "Friday");
else if (DayCode == 6) StrCopy(Day, "Saturday");
else if (DayCode == 7) StrCopy(Day, "Sunday");
else StrFormat(Day, "%d", DayCode);
SetGlobalString(49,Day);
}
}


It used to work, and I haven't changed it (besides having to change the return strings to global strings after upgrading to 2.61, which might be the cause).

How do I fix this?Ã,  Thanks in advance.
Space Quest: Vohaul Strikes Back is now available to download!

Edwin Xie

I don't know but the brackets look like they are misplaced.
Moving at superhigh speed getting to the planet called Earth. But it is boxed in white......thing.....

pcj

Well, it used to work, and unless bracket placement has been changed (or I copied and pasted incorrectly), it should be fine.
Space Quest: Vohaul Strikes Back is now available to download!

Gilbert

One problem I see was that you used recursive calls (eg. you used Now(1) within it to find the year), I think that's not supported, you need to fix the codes.

Moreover quick checking your codes proved it won't work as expected, as AGS supports only integer arithmetic, so that (365+(1/4)) calculation in Raw_Year will not work as expected (1/4=0, so that expression is just 365), I'm too lazy to muck up some fix for it :P


pcj

Quote from: Gilbot V7000a on Thu 26/08/2004 04:36:19
One problem I see was that you used recursive calls (eg. you used Now(1) within it to find the year), I think that's not supported, you need to fix the codes.

Weird, ok.

QuoteMoreover quick checking your codes proved it won't work as expected, as AGS supports only integer arithmetic, so that (365+(1/4)) calculation in Raw_Year will not work as expected (1/4=0, so that expression is just 365), I'm too lazy to muck up some fix for it :P

Dang, I had asked a while back about the double type, that's what they said to do.  Weird.
Space Quest: Vohaul Strikes Back is now available to download!

Pumaman

strange, the code looks ok, I'm not sure why that's causing a crash. Did you manage to resolve the problem?

pcj

Space Quest: Vohaul Strikes Back is now available to download!

Pumaman

If you'd like to upload a game which has the crash, I can look into it and attempt to find the cause.

pcj

Well, it happens even on a blank room (when the game starts), so I'll just post the global script here:

http://vsb.bpcent.com/vsbglobal.txt

Hope that works for you.  Thanks again for your help.
Space Quest: Vohaul Strikes Back is now available to download!

Gilbert

#9
Alright, I think I had fixed it, I still don't know the real reason but I think it may be a stack overflow trying to do those multiple Now(1) (recurrsive) calls, I managed to trim the code down a bit (mainly eliminated multiple calls to functions and the like) and it seemed to work (I think I had fixed that year calculation, but I'm not sure, you need to spot errors when the date is close to the beginning/end of a year). Though after fixing the codes seemed to work, I don't like recurrsive function calling so I separated the year calculation into another function called GetYear() (Now(1) will still return the year):
function GetYear(){
Ã,  int Raw_Seconds = GetRawTime(); 
Ã,  int Raw_Minutes = Raw_Seconds / 60;
Ã,  int Raw_Hours = Raw_Minutes / 60;
Ã,  int Raw_Days = Raw_Hours / 24;
Ã,  int Raw_Years = ((Raw_Days-731) * 4) / 1461;
Ã,  int Year = Raw_Years + 1972;
Ã,  return Year;
}


function Now(int type){ // Returns the current time/day/date/year by the computer clock.
  if (type == 1) return GetYear(); //return GetYear(); // Current year
  else if (type == 2) { // Current time
    string TimeNow;
    int hour = GetTime(1);
    int minute = GetTime(2);
    string AMPM;
    if (hour >= 12) {
      hour-=12;
      StrCopy(AMPM, "pm");
    } else {
      if (hour == 0) hour=12;
      StrCopy(AMPM, "am");
    }
    StrFormat(TimeNow, "%d:%02d %s",hour,minute,AMPM);
    SetGlobalString(49,TimeNow);
  } else if (type == 3) { // Current date
    int month = GetTime(5);
    int day = GetTime(4);
    string date1;
    StrFormat(date1,"%d-%d-%d",GetTime(5),GetTime(4),GetYear());
    SetGlobalString(49,date1);
  } else if (type == 4) { // Current day (e.g., Monday, Tuesday)
    int Year = GetYear();
    int tmpYear=Year - 2000;
    tmpYear += (tmpYear / 4);
    int month=GetTime(5);
    int MonthNum;
    if (month == 1) MonthNum = 6;
    else if (month == 2) MonthNum = 2;
    else if (month == 3) MonthNum = 2;
    else if (month == 4) MonthNum = 5;
    else if (month == 5) MonthNum = 0;
    else if (month == 6) MonthNum = 3;
    else if (month == 7) MonthNum = 5;
    else if (month == 8) MonthNum = 1;
    else if (month == 9) MonthNum = 4;
    else if (month == 10) MonthNum = 6;
    else if (month == 11) MonthNum = 2;
    else if (month == 12) MonthNum = 4;
    int DayCode = tmpYear + MonthNum + GetTime(4);
    if (((Year%4)==0)&&(month == 1 || month == 2)) DayCode--;
    while (DayCode > 7) {
      DayCode -= 7;
    }
    while (DayCode < 1) {
   Ã,  DayCode += 7;
Ã,    }
    string Day;
    if (DayCode == 1) StrCopy(Day, "Monday");
    else if (DayCode == 2) StrCopy(Day, "Tuesday");
    else if (DayCode == 3) StrCopy(Day, "Wednesday");
    else if (DayCode == 4) StrCopy(Day, "Thursday");
    else if (DayCode == 5) StrCopy(Day, "Friday");
    else if (DayCode == 6) StrCopy(Day, "Saturday");
    else if (DayCode == 7) StrCopy(Day, "Sunday");
    else StrFormat(Day, "%d", DayCode);
    SetGlobalString(49,Day);
  }
}


Note that I simplified that (Now(1)==2004||Now(1)==2008...) part a lot by just checking if the year is divisible by 4, it's actually not a real accurate calculation as actually not all years # divisible by 4 have 366 days (same problem to the year calculation), but I think auch a year won't happen again in the near future (I think it's probably in 2100, I had forgotten the criteria).

Pumaman

Hmm, I pasted your global script over mine, and it didn't crash for me.

Anything in particular that makes it happen?

Also, just as a tip, StrFormat can work like this:

StrFormat(buffer, "%02d:%02d:%02d", hours, minutes, seconds);


pcj

Thanks, guys.

Gilbot:
Interesting fixes.  Yes, that leap year thing doesn't apply to years divisible by 100, so 2100 won't be a leap year (not that I should have to worry about someone playing it THAT far into the future :o ), but years divisible by 400 will be leap years (e.g., 2000, 2400).  This should work for my purposes.  I'll try it and see if it gets rid of the problem.

CJ:
Nothing in particular causes it.  It shows up when the game is loaded.
Space Quest: Vohaul Strikes Back is now available to download!

Pumaman

Is the game small enough to upload? It might be other factors at work as well as the script ,that are causing the problem.

pcj

Space Quest: Vohaul Strikes Back is now available to download!

Gilbert

#14
I didn't try your .ags yet, but I did get a crash when I tried the posted script:
I just started a new game with default template, then copy the scripts to the header and global script, commenting some of the lines to make the script compile because of missing buttons, etc. And it didn't crash.
BUT, after that I added Display lines before all the returns to check the Year, etc. set, and then I tried to run it, since upon starting the game the script called for Now() twice, and as Now(1) itself was called recurrsively several times in the function itself, it came up displaying the year several times (which was strange to me, as I thought displaying something in game_start() should not work, as not even a room is loaded), but after the 4th or 5th time it crashed. So I blame that to some memory management problem, which was called by repeated recurrsive calls (plus displaying something uses memory I think), and after I changed the codes (like above) it won't crash for me anymore.

EDIT: Hmmm I just tried your compiled game, but it complains about line 186, SetLabelText: Invalid object number.

pcj

Mmm...

Forgot to set the labels when I copied over the global script to a "blank" game - SetLabelText's not really my problem...

Thanks for looking into that recursive thing.  I'll change it like you suggested.
Space Quest: Vohaul Strikes Back is now available to download!

SMF spam blocked by CleanTalk