Making a game with MooClick - Part 3
Author: | Flava
|
Submitted: | 3rd November, 2005
|
Views: | 12703
| Rated: |
|
|
Introduction
Well it's been a long wait, but finally I have released part 3 of "Making a game with MooClick". If you have followed parts 1 and 2, then you should have a half finished online game. That's all great, but now you need to add finishing touches. How about a chat system? And being able to use MySQL and PHP to store data? Well this is what will be covered in part 3.
What do you need?
Again, you'll need the game that you made in parts 1 and 2. And first we're going to add a simple chat system so that your users can talk to each other see? You'll need the Rich Edit Text object, and also a normal edit text box. Name the rich edit object as "Chat" (this is where the text will be displayed) and name the edit box "Chat Text" (this will be the place you type your messages!). Make the rich edit object read only, and add any properties you feel necessary.
Starting the chat system
If you read the last articles, then this should be easy as pie! Basically, when enter is pressed a message will be sent to all plays who are on the server - and when a message is received, it will be displayed on the client. So should be easy for those people!
+ Upon pressing "Enter"
+ Chat Text: have input focus
- MooClick: Channel: Send text "<" + P_GetName$( "MooClick" ) + ">" + " " + Edittext$( "Chat Text" ) On Subchannel 1
- Chat: Set text to NewLine$ + "<" + P_GetName$( "MooClick" ) + ">" + " " + Edittext$( "Chat Text" )
- Chat Text: Set text to ""
- Chat: Scroll to end
Okay may look complicated at first but just listen. Firstly, if enter is pressed and the "chat text" box has focus, then we send a message to all players via mooclick. So lets say my MooClick player name is Flava, and the message in "Chat text" is "Hello my name is Flava" - the following would be sent to all players:
<Flava> Hello my name is Flava
If you can get your head around that then great. Your player name is retreived with P_GetName$( "MooClick" ) and the message is retrieved with Edittext$( "Chat Text" ). This message is sent on subchannel 1, as subchannel 0 was used for something else. Therefore if the message is received from subchannel 1, the program will know it's a message for the chat area. The message is only sent to players on the server - it is not sent to yourself. Therefore you need to add the exact same message to the rich edit object using the exact same expression. We then simply erase the "Chat Text" box and make the rich edit object scroll to end (so that we see the latest messages).
+ MooClick: Player: On message
+ P_OnMessage_GetSubchannel( "MooClick" ) = 1
- Chat: Set text to NewLine$ + P_OnMessage_GetText$( "MooClick" )
- Chat: Scroll to end
Pretty simple really. If a message is received and is from subchannel 1 (which is for our chat area) then add the message to the chat box (when you set text to a rich edit object, it just adds the text onto exisiting text so doesn't "overwrite" it all). We then scroll to end so we can see the latest message. This is basically your chat system completed.
EXTRA: Creating commands
+ "Host" Button is clicked
- Set counter "Is Player admin?" to 1
First create a counter. To make things really easy, call it "is player admin?". If the counter is equal to 1, then the user is an admin. If the counter is 0, then the user is a normal player. The host will obviously be admin, so if host button is clicked, counter is set to 1.
+ Upon pressing "Enter"
+ Left$( Edittext$( "Chat Text" ), 6 ) = "/kick"
+ "Is player admin?" counter = 1
+ Chat text: Have input focus
- String parser: Add delimeter " "
- String parser: Set source to "Chat Text"
- MooClick: Channel: Send listGetAt$( "String Parser", 2 ) on subchannel 3
- String parser: Delete delimeter " "
- Chat text: Set text to ""
Okay.. Basically if you press enter and are an admin, and your message begins with "/kick" then first we add a delimeter of " " to string parser. This is just basically a space. This is because when using the /kick command, you will type "/kick Flava" - there is a space after the command which will be the delimeter. We then set string parser's source to the message - and then send a message to all players on subchannel 3 - this message will basically just be the username of the player that is being kicked. (You can use subchannel 2, but for some reason I'm using subchannel 3..). Then delete the delimeter.
+ MooClick: Player: On message
+ P_OnMessage_GetSubchannel( "MooClick" ) = 3
+ P_OnMessage_GetText$( "MooClick" ) = P_GetName$( "MooClick" )
- MooClick: Player: Disconnect
Simple really. If a message is received on subchannel 3, and that message is the same as your MooClick username - then you are disconnected from the server. You could then display a message saying "You have been kicked" or you could change the frame. Just remember - that subchannel 3 would be used for kicks only. There would be ways around this, but I'd find it easier to use different subchannels for different commands. A helpful tip would be to make a note of what each subchannel is for - makes it much easier to debug your game. You could just add a comment in the event editor to do this - but I write them done on a piece of paper. Your choice though
Using MySQL and PHP
Firstly, I am not going to go through teaching you guys PHP. This is because it's far too difficult for me to teach in one article. This will basically concentrate on using MooSock to retrieve data from a PHP page. So you will need to know PHP to do this. I may write a future article on using PHP, but I doubt it. In this article we're going to make a small login system. So you're going to need a MySQL database which has username and passwords stored in them. Then you can create a PHP page that displays the below:
<del-mmf>Message<del-mmf>
Now may seem confusing but this is the easiest way I can find of doing this. I am pretty terrible with using MooSock and webpages, but I may as well tell you what I know anyway. The <del-mmf> text will be used as a delimeter - this is because when MooSock receives a webpage, it includes all the header information and stuff, which we don't want. The message will be either "yes" or "no" - you must code PHP to test the variables "$username" and "$password" and to check whether they are the correct details from the MySQL database. If not, then display "no". If they are correct display "yes".
+ Login button clicked
- MooSock: Connect to "http://www.mysite.com" on port 80
This basically connects to the site which the PHP page is hosted on.
+ MooSock: Is connected
- MooSock: Send text line "GET user.php?username=" + EditText("Username") + "&password=" + EditText("Password") + " HTTP/1.0"
- MooSock: Send text line "Host: www.mysite.com"
- MooSock: Send text line "From: flava@mysite.com"
- MooSock: Send text line "User-Agent: HTTPTool/1.0"
- MooSock: Send text line ""
You must change the above details to fit your site. This will basically connect to the page "user.php" and set the variables $username and $password to whatever is in the text boxes in your game. You must then check the password with the specified username (using PHP) to check that the password is correct. If it is, echo "yes" and if not echo "no".
+ MooSock: On received
- Rich edit: set text to Recv$( "MooSock", 1024 )
This basically sets the rich edit text object to whatever the content of the webpage is. This will include the headers of the page. You will need to user string parser to parse out the message by using the <del-mmf> delimeter. You will then be left with either a "yes" or a "no". Simply test whether its a yes or no, and then tell your program what to do - so if "no", display "Incorrect login info" and if "yes" then continue onward in your game. I know this is very brief - but truthully I am still not fully experienced in this area. Also this would be the more "advanced" type of thing which I know most of you won't be able to have a go at. So this is just to give those of you who are advanced and idea of how to do this.
The finishing touches
Well you have a movement sorted, and a chat system and possibly a login system too. You could now start work on gameplay. For example if you're doing a basic shooter - a message can be sent to all players when "Ctrl" is pressed - this message would include your user ID, your weapon type and position. Then when the message is received - you'd use string parser to split it all up then you'd use your skills to use the information that you have and make that object shoot something. It all becomes easier with experience - after a while you'll begin to get ideas together and how to use them. MooClick is literally just sending messages to each other, and using those messages to display output on-screen. The worst thing is debugging - so make sure you name everything appropriately and put comments in your coding. With practice you should get used to using MooClick.
A few side-notes
You may think that you can make an online game with just using MooClick. But String parser and other extensions become very useful. The extensions are their to increase the "power" of MMF and to help you integrate certain things into your program. As well as learning to use MooClick, try to learn about string parser. String parser is great for splitting up messages - the only problem is that it is difficult to use at first. But after a while you will realise just what the extension is doing, and you'll be a pro at using it. MooSock is another important extension if you plan to use hi-scores and server lists. It's important that you don't try to make an online game straight away if you don't know how to use these extensions. It cannot be done with just MooClick - trust me.
Example file
As i'm so kind and generous and everything, I have put together the code from parts 1, 2 and 3 and made an example file for you. I have not done a login system - but everything else is in this file. It uses some comments to help you through it - but hopefully you won't need any help. Just remember, if you have a firewall or router - you cannot host a server unless ports 1203 are unblocked/forwarded.
Download: http://www.ectoprods.com/flava/mooclickgame.zip
Thanks, if you have any questions or problems - post in the comments area and I will try to answer them best I can.
|
|
|