All 4 entries tagged Computery Stuff
No other Warwick Blogs use the tag Computery Stuff on entries | View entries tagged Computery Stuff at Technorati | There are no images tagged Computery Stuff on this blog
August 20, 2005
Neat little function
I am doing a lot of changing of elements' text within a html document using javascript, I had this little function that would simply replace the text node of an element given, this was fine, until I wanted to add formatting, namely line breaks, our old friend,.
So I whipped up this little babe, which works a treat:
function changeElementsTextNode(aElement,text) {
if (aElement) {
if (aElement.childNodes) {
for (var i = aElement.childNodes.length-1;i >= 0;i--) {
aElement.removeChild(aElement.childNodes[i]);
}
var textarr = text.split("\n");
for (var i = 0;i < textarr.length;i++) {
var aNode = document.createTextNode(textarr[i]);
aElement.appendChild(aNode);
if (i != textarr.length-1) {
var aNode = document.createElement("br");
aElement.appendChild(aNode);
}
}
}
}
}
just thought I'd share.
AJAX debugging
Writing about web page http://blog.monstuff.com/archives/000252.html
Just found this very cool piece of script, basically it uses the Greasemonkey extension to firefox to replace the XMLHttpRequest object with a debuging version. The script appears as a floating div which shows you exactly what XMLHttpRequest is doing on ANY webpage!
If only I'd known about this sooner I'd have less debugging code in my script for TLH2, oh well, can't hurt can it, my stuff works in ie/opera.
javascript > vbscript
Just thought I'd have a rant about vbscript, firstly let me spell it out:
vbscript sucks
Firstly, the language syntax is awful, let me explain:
1. No curly brackets, so you have to write code like:
if (someTest) then
someFunc()
end if
which not only takes longer but, I think is less clear.
2. There are no clear bitwise and logical operators, sure AND and OR exist, but vbscript decides when it wants to use these as bitwise, or logical operators, This in my view is a bad thing, every time you lose control of something as a programmer it should send a shiver up your spine. In javascript we have & and && or | and || respectively bitwise and logical and and or.
3. Subs and Functions, vbscript has this notion of a function that doesn't return anything, and calls it a sub, while javascript doesn't really care if you don't return a value from your function. While you may think subs are a good idea, you have to write silly code like this:
someSub firstParam,secondParam
or
call someSub(firstParam,secondParam)
thats right, if you use subs you can't use brackets when calling unless you use the special 'call' keyword, utterly stupid.
4. Compound operators, or lack thereof. In javscript I can write:
thisVal += 10;
but in vbscript I'm forced to do:
thisVal = thisVal + 10
I think the top one is much clearer, and this brings me neatly onto the next point,
5. Assignment and Equivalence, the operators for these two functions are the same in vbscript, =, but in javascript we have = and == respectively. How can i assign a value in a if staement in vb, I can't.
6. Too much english, IMHO, vbscript uses natural language too much, and while its a nice safety net, it looks plain ugly and makes it harder to read code.
Conclusion
If you must, learn to program using vbscript, but move quickly on to javascript or at least C syntax style languages. If you want learn a language thats has a slightly easier to pick up systax than C or java, try delphi, its way faster too.
AJAX web development
So I started a little web development project over the summer (will be released soon) and thought I'd write a little about it for fun.
It's the second incarnation of a small online game I wrote a few years back when I was first getting into server side web development. So firstly a little history lesson:
When I got my first taste of web development I quickly became interested in making more than just static web pages, at the time all I knew was javascript. I would follow the tutorials in my favourite PC mag, PC Plus, and try do clever little things like making a popup window and populating it with some pretty html. However I was constantly running up against a wall, I needed server side scripting!
I read about this thing called ASP, and started to try doing things with it, quickly I had a database connected and a simple user authentication system set up. These things were really cool, there's nothing quite like the feeling of creation.
Then, my favourite online game of the time, Planetarion, went to a pay-only business model. My friends and I were distraught! I thought I could do a better job though, with my knowledge of some ASP I was convinced that I could improve on Planetarion.
The Last Hope was born, a play on 'A New Hope' as it was originally to be a star trek vs star wars game, but it quickly evolved into something else, just two fictional races at war.
Development was slow as I only had limited spare time, and doing things in ASP quickly turned into a right bitch. My original pages were slow, difficult to follow and used bad coding techniques. My later pages were much faster, expandable and more secure. Crowning achievement of TLH was the 'ticker', which actually makes the game work, it adds resources, works out battles etc, it was originally written in vbASP, but that quickly became a nightmare, so it was written in jsASP which is so much better. My advice, never ever use vbASP, use jsASP all servers support it and it features more advanced language features, not to metion a proper syntax (see my other blog entry). Back to the ticker, it stood at over 1600 lines of code, all streamlined so it ran very quickly, it worked almost perfectly first time too.
That was really the end of TLH active development, I lost interest in the game and when someone offered to buy the code for their own game, I sold. So TLH lives on as 'Planetwars', which from time to time I have to update and fix bugs in the code, which kinda sucks, since the code is mostly horrible and reminds me of how badly I used to code. But I've always wanted to follow up TLH with a new game, with more features and more gameplay elements. So,
TLH2 was born, I decided that games like this are at there best when you have to play with others against others that are also playing together. In TLH this was enforced by alliances, galaxies of planets and the two races, in TLH2 I will take a different tack. Players will control one of several colonies on a planet, so you will share a planet with others, and you will need to work together to do well in the game. There are a lot of gameplay features that I plan, but I'll save those for another entry, but the biggest change in THL2 is the user interface.
I decided to implement TLH2's UI using AJAX (hate that term) basically instead of reloading the entire page when a user wants to do something, I use javascript to call a behind the scenes XMLHttpRequest object, this returns data from the server, which is then parsed by more javascript and the client side is updated accordingly. The system took a while to build but now it's there actual development is really quick, you don't have to create new pages everytime, with new request hadling code, you just add a few new functions and some new case statements, and you're done!
The benefits are that its much faster on the front end, the entire page doesn't have to be reloaded and so less rendering has to be carried out by the browser, also the bandwidth savings are immense.
Anyways I plan to blog more about this, maybe someone will be interested.