News GUI control for receiving news content from a web server
by Nathan Martin · 09/02/2002 (12:17 pm) · 39 comments
Download Code File
Well here is an on-line version of the news control's file data and I'll explain after it:
Now that data should be saved into a file named NewsGui.gui. Now the content of the file news.txt which is needed for all of this to work smoothly:
Now the content of news_test.hfl:
Content of about_test.hfl:
Now you have the following files NewsGui.gui, news.txt, news_test.hfl, and about_test.hfl. Upload the last three files onto a web server and put the first file which is NewsGui.gui into your game's /[mod]/client/ui/ directory and don't forget to implement it into the game by editing init.cs file that's located one directory up from where you placed the news control at. Add this line in init.cs file:
That will have the game loadup and process the news control for usage.
Now edit the NewsGui.gui control file and scroll down to the point where you see:
You need to edit both of those variable's values to your web server's address and path to where the news files are kept. Then save the file. After you have done all of this create a button somewhere in your game and put this line in command property:
Now click on that new button and tada! If you did everything correctly it should have worked!
Sorry for the lack of comments in the code and the lack of information in this resource since this is my first resource posting I am not really sure what all I should disclose and it's pretty late right now, so I'm very tired.
I've also uploaded the files needed for all of this just in case someone is too lazy to copy and paste into notepad(or your favorite text editor) to do all of this.
I hope you all like this resource since I know this one could have helped me out a lot when I needed to know how to use HTTPObject.
ScreenShot:

Well here is an on-line version of the news control's file data and I'll explain after it:
//--- OBJECT WRITE BEGIN ---
new GuiControl(NewsDlg) {
profile = "GuiDefaultProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "0 0";
extent = "640 480";
minExtent = "8 8";
visible = "1";
helpTag = "0";
new GuiWindowCtrl() {
profile = "GuiWindowProfile";
horizSizing = "center";
vertSizing = "center";
position = "75 36";
extent = "618 459";
minExtent = "300 200";
visible = "1";
helpTag = "0";
text = "Game News!";
maxLength = "255";
resizeWidth = "1";
resizeHeight = "1";
canMove = "1";
canClose = "1";
canMinimize = "1";
canMaximize = "1";
minSize = "50 50";
closeCommand = "Canvas.popDialog(NewsDlg);";
new GuiScrollCtrl() {
profile = "GuiScrollProfile";
horizSizing = "right";
vertSizing = "height";
position = "8 26";
extent = "132 400";
minExtent = "8 8";
visible = "1";
helpTag = "0";
willFirstRespond = "1";
hScrollBar = "alwaysOff";
vScrollBar = "dynamic";
constantThumbHeight = "0";
childMargin = "0 0";
new GuiTextListCtrl(NewsList) {
profile = "GuiTextListProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "1 1";
extent = "130 8";
minExtent = "8 8";
visible = "1";
helpTag = "0";
enumerate = "0";
resizeCell = "1";
columns = "0";
fitParentWidth = "1";
clipColumnText = "0";
};
};
new GuiButtonCtrl() {
profile = "GuiButtonProfile";
horizSizing = "right";
vertSizing = "top";
position = "8 432";
extent = "133 21";
minExtent = "8 8";
visible = "1";
command = "GetNews(NewsList.getRowTextById(NewsList.getSelectedId()));";
helpTag = "0";
text = "Refresh News";
groupNum = "-1";
buttonType = "PushButton";
};
new GuiScrollCtrl() {
profile = "GuiScrollProfile";
horizSizing = "width";
vertSizing = "height";
position = "146 26";
extent = "465 426";
minExtent = "8 8";
visible = "1";
helpTag = "0";
willFirstRespond = "1";
hScrollBar = "alwaysOn";
vScrollBar = "alwaysOn";
constantThumbHeight = "0";
childMargin = "0 0";
new GuiMLTextCtrl(NewsText) {
profile = "GuiMLTextProfile";
horizSizing = "width";
vertSizing = "bottom";
position = "1 1";
extent = "457 14";
minExtent = "8 8";
visible = "1";
helpTag = "0";
lineSpacing = "2";
allowColorChars = "0";
maxChars = "-1";
autoSizeWidth = true;
autoSizeHeight = true;
};
};
};
};
//--- OBJECT WRITE END ---
function NewsDlg::onWake(%this)
{
// Web server to connect to in order
// to download the news. Note ':80'
// part must be left there or else
// this will not work.
$NewsHTTPHost="mywebserver.com:80";
// Directory on the web server where
// the news files are kept:
$NewsHTTPPath="/path_to_news_dir/";
}
//----------------------------------------
function GetItemList()
{
NewsList.entryCount = 0;
NewsList.clear();
%newsc = new HTTPObject(ItemList){};
%newsc.get($NewsHTTPHost, $NewsHTTPPath @ "news.txt", "");
}
function ItemList::onLine( %this, %line )
{
if(%line $= "[done]")
{
if($NewsName $= "")
{
NewsList.setSelectedRow(0);
} else {
%index = NewsList.findTextIndex($NewsName);
NewsList.setSelectedRow(%index);
//$NewsName="";
}
} else {
NewsList.addRow( NewsList.entryCount, %line );
%line = strreplace( %line, " ", "_" );
%line = strlwr( %line );
NewsList.fileName[NewsList.entryCount] = %line @ ".hfl";
NewsList.entryCount++;
}
}
function ItemList::onConnectFailed()
{
MessageBoxOK("ERROR","Couldn't connect to news server to receive news!"@
" The news server might be down for right now\n\nPlease try again later"@
" at least 20 minutes from now. That will give it enough time to come back up.\n\nSorry :(");
}
function ItemList::onConnected()
{
}
function ItemList::onDisconnect()
{
}
//----------------------------------------
function NewsList::onSelect(%this, %row)
{
GetNewsFile(%this.fileName[%row]);
}
function getNews(%NewsName)
{
if(%NewsName !$= "") { $NewsName=%NewsName; }
NewsList.entryCount = 0;
NewsList.clear();
GetItemList();
//NewsList.setSelectedRow(0);
Canvas.pushDialog(NewsDlg);
}
function GuiMLTextCtrl::onURL(%this, %url)
{
// Check to see if the user clicked on
// a section selection command link and
// not an actual URL.
%x = strpos( %url, ".." );
if(%x !$= -1 ) {
// yep it's a section selection command link!
// Now let's see what section we should select
// to automatically jump to that section.
%call = getSubStr( %url, 0, %x );
if(%call $= "news") { getNews(getSubStr( %url, %x + 2, strlen(%url) - %x )); }
} else {
// nope, it's just a URL. Therefore
// open the user's default web browser
// to that URL link that's provided.
gotoWebPage( %url );
}
}
//-------------------------------------------
function GetNewsFile(%filename)
{
%newsi = new HTTPObject(TheNews){};
%newsi.get($NewsHTTPHost, $NewsHTTPPath @ %filename, "");
}
function TheNews::onLine( %this, %line )
{
if(%line $= "[done]")
{
NewsText.setText($newslines);
$newslines="";
} else {
$newslines=$newslines @ "\n" @ %line;
}
}
function TheNews::onConnectFailed()
{
}
function TheNews::onConnected()
{
}
function TheNews::onDisconnect()
{
}Now that data should be saved into a file named NewsGui.gui. Now the content of the file news.txt which is needed for all of this to work smoothly:
News Test About Test [done]
Now the content of news_test.hfl:
<just:center><lmargin%:5><rmargin%:95><font:Arial Bold:25>News Test! <just:left> <font:Arial Bold:20>[00/01/2002 @ 1:33 AM (CST)] - <a:t2n.org>TRON</a><font:Arial:16> <font:Lucida Console:12> This is a news test of my news control which I scripted out myself! I hope you are happy with the results... :p Also <a:news..About Test>click here</a> to find info about this test. [done]
Content of about_test.hfl:
<just:center><lmargin%:5><rmargin%:95><font:Arial Bold:25>Abouts Test! <just:left> <font:Arial Bold:20>[00/01/2002 @ 1:33 AM (CST)] - <a:t2n.org>TRON</a><font:Arial:16> <font:Lucida Console:12> This is a news test of my news control which I scripted out myself! Which I use the HTTPObject to 'pull' the news right off of a web server which I think is the smartest move to take to provide a news model for on-line games. It's simple and web-based dynamically generated news could easily be implemented just by tweating around with the news script a bit and of course build a news system that works on the web server such as using PHP. [done]
Now you have the following files NewsGui.gui, news.txt, news_test.hfl, and about_test.hfl. Upload the last three files onto a web server and put the first file which is NewsGui.gui into your game's /[mod]/client/ui/ directory and don't forget to implement it into the game by editing init.cs file that's located one directory up from where you placed the news control at. Add this line in init.cs file:
exec("./ui/NewsGui.gui");That will have the game loadup and process the news control for usage.
Now edit the NewsGui.gui control file and scroll down to the point where you see:
function NewsDlg::onWake(%this)
{
// Web server to connect to in order
// to download the news. Note ':80'
// part must be left there or else
// this will not work.
$NewsHTTPHost="mywebserver.com:80";
// Directory on the web server where
// the news files are kept:
$NewsHTTPPath="/path_to_news_dir/";
}You need to edit both of those variable's values to your web server's address and path to where the news files are kept. Then save the file. After you have done all of this create a button somewhere in your game and put this line in command property:
getnews();
Now click on that new button and tada! If you did everything correctly it should have worked!
Sorry for the lack of comments in the code and the lack of information in this resource since this is my first resource posting I am not really sure what all I should disclose and it's pretty late right now, so I'm very tired.
I've also uploaded the files needed for all of this just in case someone is too lazy to copy and paste into notepad(or your favorite text editor) to do all of this.
I hope you all like this resource since I know this one could have helped me out a lot when I needed to know how to use HTTPObject.
ScreenShot:

About the author
By day I work as an embedded microcontroller programmer for mainly Blackfin MCU/DSPs, and by night, and free time too, I'm a hobbyist game developer.
#2
09/02/2002 (12:38 pm)
This is a great idea! I'm not sure how or if I could ever use it, but I love the concept of in-game news.
#3
One little problem I had: it should read
getnews("News Test");
or
getnews("About Test");
otherwise you will get an "Unable to connect..." error...
09/02/2002 (2:02 pm)
Yeah, very cool idea!! :) Thanks a lot for sharing it!One little problem I had: it should read
getnews("News Test");
or
getnews("About Test");
otherwise you will get an "Unable to connect..." error...
#4
I would think you could use it just to announce updates, maps, message of the day (MOTD), and perhaps even other games the team is working on.
Question: Does the http gui allow clickable graphics? Like a 'normal' web page would?
09/02/2002 (2:16 pm)
Great resource...I would think you could use it just to announce updates, maps, message of the day (MOTD), and perhaps even other games the team is working on.
Question: Does the http gui allow clickable graphics? Like a 'normal' web page would?
#5
Stefan: That's very odd for it to give Unable to connect errors. It should auto-select the first section if it cannot find the one you've stated in the getnews parameter.
hmmmm, I wonder what the problem is though. I just tested it and it works like it's suppose to when there's a bogus value in getnews parameter or no value at all. If anyone comes up with a code fix/patch then please let me know and I'll apply it to this resource.
09/02/2002 (3:48 pm)
Thank you all for your comments :)Stefan: That's very odd for it to give Unable to connect errors. It should auto-select the first section if it cannot find the one you've stated in the getnews parameter.
hmmmm, I wonder what the problem is though. I just tested it and it works like it's suppose to when there's a bogus value in getnews parameter or no value at all. If anyone comes up with a code fix/patch then please let me know and I'll apply it to this resource.
#7
Compiling fps/client/ui/NewsGui.gui...
fps/client/ui/NewsGui.gui Line: 124 - Syntax error.
>>> Advanced script error report. Line 247.
>>> Some error context, with ## on sides of error halt:
// Directory on the web server where
// the news files are kept:
$NewsHTTPPath="/news/"
}##
##
function GetItemList()
{
NewsList.entryCount = 0;
NewsList.clear();
>>> Error report complete.
Executing fps/client/ui/NewsGui.gui.
fps/client/ui/NewsGui.gui Line: 124 - Syntax error.
>>> Advanced script error report. Line 247.
>>> Some error context, with ## on sides of error halt:
// Directory on the web server where
// the news files are kept:
$NewsHTTPPath="/news/"
}##
##
function GetItemList()
{
NewsList.entryCount = 0;
NewsList.clear();
>>> Error report complete.
Didnt find a mistake. :(
09/03/2002 (8:47 am)
Im getting this error:Compiling fps/client/ui/NewsGui.gui...
fps/client/ui/NewsGui.gui Line: 124 - Syntax error.
>>> Advanced script error report. Line 247.
>>> Some error context, with ## on sides of error halt:
// Directory on the web server where
// the news files are kept:
$NewsHTTPPath="/news/"
}##
##
function GetItemList()
{
NewsList.entryCount = 0;
NewsList.clear();
>>> Error report complete.
Executing fps/client/ui/NewsGui.gui.
fps/client/ui/NewsGui.gui Line: 124 - Syntax error.
>>> Advanced script error report. Line 247.
>>> Some error context, with ## on sides of error halt:
// Directory on the web server where
// the news files are kept:
$NewsHTTPPath="/news/"
}##
##
function GetItemList()
{
NewsList.entryCount = 0;
NewsList.clear();
>>> Error report complete.
Didnt find a mistake. :(
#8
Answer: I really have no idea since I cannot seem to get GuiMLTextCtrl to display any images at all. So I cannot test to see.
Thanks again all.
09/03/2002 (9:06 am)
Eric - Question: Does the http gui allow clickable graphics? Like a 'normal' web page would?Answer: I really have no idea since I cannot seem to get GuiMLTextCtrl to display any images at all. So I cannot test to see.
Thanks again all.
#9
09/03/2002 (9:09 am)
Christian: It looks like you copied and pasted the newsgui.gui file. Try the zipped up version instead which you can download by clicking on the news_gui.zip URL located right next to 'Code File:' which is at the top of this page.
#10
09/03/2002 (9:45 am)
Ok there was a ";" missing behind *$NewsHTTPPath="/path_to_news_dir/"*. But now I have a question, how can I make links and such. Can I just make them to "hfl" files or to .rar .zip aso. too?
#11
getNews();
on the console, you get a connection error.
However, if you call
getNews("");
on the console (notice the quotes), it works...
Or you can call
getNews("About Test");
which also works...
So I guess, the button command automatically sends the latter command (with quotes), whereas the console doesn't ...
if that makes any sense... ;)
Anyhow, really cool idea!
09/03/2002 (9:57 am)
Just for the records... it seems to work if you call it from some button, but if you callgetNews();
on the console, you get a connection error.
However, if you call
getNews("");
on the console (notice the quotes), it works...
Or you can call
getNews("About Test");
which also works...
So I guess, the button command automatically sends the latter command (with quotes), whereas the console doesn't ...
if that makes any sense... ;)
Anyhow, really cool idea!
#12
Also I don't think you can get the HTTPObject to download files and such since I've heard torque doesn't support binary files via file transfers or something like that. I read about that on a forum on this site I believe. What I do is place a URL in a news file and when a user clicks on it that opens the user's default web browser to download the file, which I think it's best to do it that way anyway.
Stefan: lol, that's really an odd thing with the console and the actual GUI handling function parameters. I'll have to keep that in mind. Thanks man. :)
09/03/2002 (10:31 am)
Christian: Thank you so much for letting me know, I've corrected the typo on this resource page and reuploaded the zip file with corrections. Thanks.Also I don't think you can get the HTTPObject to download files and such since I've heard torque doesn't support binary files via file transfers or something like that. I read about that on a forum on this site I believe. What I do is place a URL in a news file and when a user clicks on it that opens the user's default web browser to download the file, which I think it's best to do it that way anyway.
Stefan: lol, that's really an odd thing with the console and the actual GUI handling function parameters. I'll have to keep that in mind. Thanks man. :)
#13
09/03/2002 (5:17 pm)
Kick ass control, thanks !
#14
09/03/2002 (7:50 pm)
Enjoy! :)
#15
09/05/2002 (1:58 pm)
Wow, thanks for this. This is even better than the way i was going to try and do it. my way was implementing sql and calling it off of the server but doing it like that would need me to do something with it. this is great.
#16
09/05/2002 (2:38 pm)
Yeah, plus the fact you can easily implement the news system with a server-side language such as PHP and use PHP to pull news articles off of a mysql database server very easily which of course is what I'm soon going to do for mine. :)
#17
NewsList.fileName[NewsList.entryCount] = %line @ ".hfl";
to call a .php file that is listed in the news.txt
i think thats what your saying. that would be cool, then you could have multiple news sections.
09/05/2002 (8:15 pm)
@ Nathan - I think i understand where your going. so you'll change this lineNewsList.fileName[NewsList.entryCount] = %line @ ".hfl";
to call a .php file that is listed in the news.txt
i think thats what your saying. that would be cool, then you could have multiple news sections.
#18
instead of:
NewsList.fileName[NewsList.entryCount] = %line @ ".hfl";
it's
NewsList.fileName[NewsList.entryCount] = %line;
then instead of:
%newsi.get($NewsHTTPHost, $NewsHTTPPath @ %filename, "");
it's
%newsi.get($NewsHTTPHost, $NewsHTTPPath, "page=" @ %filename);
Also change:
%newsc.get($NewsHTTPHost, $NewsHTTPPath @ "news.txt", "");
to
%newsc.get($NewsHTTPHost, $NewsHTTPPath @ "news.php", "page=sections");
Now you can easily control basically everything dealing with the news system via PHP script and if you wish, intergrate it with a SQL database since PHP natively supports database connections of many kinds. Pretty cool huh? :)
09/05/2002 (8:27 pm)
Yeah something like that. Here's what I am doing:instead of:
NewsList.fileName[NewsList.entryCount] = %line @ ".hfl";
it's
NewsList.fileName[NewsList.entryCount] = %line;
then instead of:
%newsi.get($NewsHTTPHost, $NewsHTTPPath @ %filename, "");
it's
%newsi.get($NewsHTTPHost, $NewsHTTPPath, "page=" @ %filename);
Also change:
%newsc.get($NewsHTTPHost, $NewsHTTPPath @ "news.txt", "");
to
%newsc.get($NewsHTTPHost, $NewsHTTPPath @ "news.php", "page=sections");
Now you can easily control basically everything dealing with the news system via PHP script and if you wish, intergrate it with a SQL database since PHP natively supports database connections of many kinds. Pretty cool huh? :)
#19
10/12/2002 (11:47 am)
I just keep getting crashes .... question on the $newshttphost and path variable: do you include the http:// ... like, if I was using http://999.999.999.999 as the path and host, would that work? Cause that causes my game to crash :/
#20
:
and do not include http:// or else it does cause some wierd issues.
So it should be like this:
$newshttphost="255.255.255.255:80";
-or-
$newshttphost="www.mysite.net:80";
If it still crashs then please tell me the Torque Game engine version you are using like the official 1.1.2 release or HEAD? As I do always I recommend people to only use official releases and not download the HEAD since it's so full of issues and not stable enough to be published for an official release.
10/12/2002 (3:31 pm)
the syntax should only be and do not include http:// or else it does cause some wierd issues.
So it should be like this:
$newshttphost="255.255.255.255:80";
-or-
$newshttphost="www.mysite.net:80";
If it still crashs then please tell me the Torque Game engine version you are using like the official 1.1.2 release or HEAD? As I do always I recommend people to only use official releases and not download the HEAD since it's so full of issues and not stable enough to be published for an official release.

Torque Owner Tim Gift