Now Property - Displaying the date (SOLVED)

Started by Zoolander, Sat 15/04/2006 14:10:11

Previous topic - Next topic

Zoolander

When getting the date using the Now Property, is it possible for the display to read April 15, 2006 instead of 15,04,2006?  How would that be done?

DoorKnobHandle

Yes, you just need to parse the information you get.

Look at this (for AGS 2.71):

Code: ags

String return_date ( )
// this function will return the date in a descriptive, long way
{
	// longdate is a string that we will fill with the long description
	// of the date ( for example "April 15, 2006" )
	String longdate;
	
	// shortdate is used to retrieve information about the current date
	// from AGS ( for example "15/04/06" )
	DateTime *shortdate;
	
	// first of all, we need to get the current time in short format
	shortdate = DateTime.Now;
	
	// now we need to express the month
	
	if ( shortdate.Month == 1 )
	// if it is january
		longdate = "January";
	
	else if ( shortdate.Month == 2 )
	// if it is february
		longdate = "February";
	
	else if ( shortdate.Month == 3 )
	// if it is march
		longdate = "March";
	
	else if ( shortdate.Month == 4 )
	// if it is april
		longdate = "April";
		
	else if ( shortdate.Month == 5 )
	// if it is may
		longdate = "May";
	
	else if ( shortdate.Month == 6 )
	// if it is june
		longdate = "June";
	
	else if ( shortdate.Month == 7 )
	// if it is july
		longdate = "July";
		
	else if ( shortdate.Month == 8 )
	// if it is august
		longdate = "August";
		
	else if ( shortdate.Month == 9 )
	// if it is september
		longdate = "September";
	
	else if ( shortdate.Month == 10 )
	// if it is october
		longdate = "October";
	
	else if ( shortdate.Month == 11 )
	// if it is november
		longdate = "November";
		
	else if ( shortdate.Month == 12 )
	// if it is december
		longdate = "December";
	
	// next, we will need to simply add the current day
	longdate = longdate.Append ( longdate.Format ( " %d", shortdate.DayOfMonth ) );
	
	// finally add the year
	longdate = longdate.Append ( longdate.Format ( ", %d", shortdate.Year ) );

	// we are done, we only need to return longdate
	return longdate;
}


This function does it all. That's quite basic actually. I just wrote it in a few minutes and tested - surprisingly - it works!!1! :=

To use it, you could for example put this inside your game_start ( )-function:

Code: ags

Display ( return_date ( ) );


Simple as that, really.

SSH

or do

Code: ags

String monthmap[13];

function game_start() {
monthmap[1]="January";
//etc...
}

12

Zoolander

I can't seem to get this to work for me.  I'm using version 2.7, does that make any difference? 

And I have a few questions:

Quote from: dkh on Sat 15/04/2006 14:28:55
String return_date ( )
Does this go at the beginning of the global script?  And I assume it should have a semicolon at the end?

Quote from: dkh on Sat 15/04/2006 14:28:55
   // longdate is a string that we will fill with the long description
   // of the date ( for example "April 15, 2006" )
   String longdate;
   
   // shortdate is used to retrieve information about the current date
   // from AGS ( for example "15/04/06" )
   DateTime *shortdate;
   
   // first of all, we need to get the current time in short format
   shortdate = DateTime.Now;
   
   // now we need to express the month
   
   if ( shortdate.Month == 1 )
   // if it is january
      longdate = "January";
   
   else if ( shortdate.Month == 2 )
   // if it is february
      longdate = "February";
   
   else if ( shortdate.Month == 3 )
   // if it is march
      longdate = "March";
   
   else if ( shortdate.Month == 4 )
   // if it is april
      longdate = "April";
      
   else if ( shortdate.Month == 5 )
   // if it is may
      longdate = "May";
   
   else if ( shortdate.Month == 6 )
   // if it is june
      longdate = "June";
   
   else if ( shortdate.Month == 7 )
   // if it is july
      longdate = "July";
      
   else if ( shortdate.Month == 8 )
   // if it is august
      longdate = "August";
      
   else if ( shortdate.Month == 9 )
   // if it is september
      longdate = "September";
   
   else if ( shortdate.Month == 10 )
   // if it is october
      longdate = "October";
   
   else if ( shortdate.Month == 11 )
   // if it is november
      longdate = "November";
      
   else if ( shortdate.Month == 12 )
   // if it is december
      longdate = "December";
   
   // next, we will need to simply add the current day
   longdate = longdate.Append ( longdate.Format ( " %d", shortdate.DayOfMonth ) );
   
   // finally add the year
   longdate = longdate.Append ( longdate.Format ( ", %d", shortdate.Year ) );

   // we are done, we only need to return longdate
   return longdate;
All this goes in the GameStart function, am I right?

And I want to be able to look at a calendar in a room to get the correct date.  Would this go in the "Look" interaction editor:
Quote from: dkh on Sat 15/04/2006 14:28:55
Display ( return_date ( ) );

It's probably simple stuff if I could just get my head around it.
Thanks.

Ashen

In this case, String return_date() is essentially a function declaration (notice the { two lines down?) as AGS can return all the main variable types (int, String, and pointers like Character, Object, etc). It'd work exactly the same if it was function return_date(), but the feeling is that declaring it as the type it returns makes logical sense (personally, I think it just confuses people who don't know about it - like just now).

So, all the rest of it doesn't go in the game_start function - it's already in it's own function (again, see the } at the bottom of dkh's code?). It can go anywhere in the script you like, provided it's before the first time you call it. If it'll only be used in one room, you can put it in that room's script. If you want it to be globally used, it'll go in the Global Script, and will need to be imported into the Script Header, as with any custom function (being a String, you might have to export it first, as well).

To use it you'd call it like you said, with a Display or Character.Say command in the interaction of the calander object/hotspot.
I know what you're thinking ... Don't think that.

Gilbert

Also, he mentioned he's using V2.7 so the codes would definitely not work, because String types weren't introduced yet (also as far as I know, old string can't be used as a return type), so the codes need revising anyway.
I can do that but I'm currently too lazy to do so. :P

DoorKnobHandle

Ah, yes. In this case, I would definately advise you, Zoolander, to upgrade to 2.71. It's very unlikely that the update is going to cause you any trouble and it will allow my posted function to work. If you really want to stay with 2.7 you'd need to change several lines of code in my function, but I don't have 2.7 floating around anymore and am as well too lazy to look everything up. :P

Zoolander

I've downloaded version 2.71 and put the String return_date ( ) function into the room's script.  Everything now works perfect.
 
On a related question, I wanted the display to read "The date is (the date)."
So I used this method:
Code: ags

string thedate="The date is";
thedate = thedate.Append(return_date());
Display(thedate);

But then I get this error:
"Cannot assign value to sting, use StrCopy."

Why would I get that error message?

DoorKnobHandle

Glad to hear that you got everything working! :)

To your question: you need to use the new String, not the old string (note the capitalization). BUT, actually, you can just do this:

Code: ags

Display ( "The date is: %s", return_date ( ) );


If you really want to do it the complicated way, then you'd need to:

Code: ags

String thedate="The date is ";
thedate = thedate.Append ( return_date ( ) );
Display ( thedate );

Zoolander


monkey0506

I realize the issue is resolved...but...

Quote from: Ashen on Sun 16/04/2006 14:27:14It'd work exactly the same if it was function return_date(), but the feeling is that declaring it as the type it returns makes logical sense (personally, I think it just confuses people who don't know about it - like just now).

No, that's not the way it works Ashen.  The keyword function is internally defined as int (#define function int), so functions using the word function can return only integral types (bool, enum, char, short, int).

Now that does cover pretty much all the types offered by AGS, so for the majority of cases it might be easier to just use function.  But in order to return a pointer, you must the type of pointer you are returning (note that the String type is included here because when defining a String, a pointer is automatically created to point to an internalstring, all of which doesn't really matter to the end user, but is worthy of note in this case); and to return a floating point decimal value (float), you have to use the word float as the return type.

Furthermore, although it was never "supported," returning old-style strings did in fact work (at least with AGS 2.62 and later).

So if anything is confusing people, it seems to me to be the usage of the word function.  Now that we can return pointer-types and float-types, I think the keyword has developed into a sort of pseudo-deprecated state (it's not actually deprecated, it still serves exactly the same purpose and function as before, but the position it was built to fulfill has grown to something larger than the keyword can handle).

SMF spam blocked by CleanTalk