awesome. thanks for the help!
hey what happened to that jar file? and i am attempting to do what you said but for some reason the applet just says applet started and displays a blank white screen.
applet?
- download
- rename to .zip
- extract
Run server,
Run client
i know all that but the link to the download doesnt appear to me anymore. and as for the applet thing my game is an applet but i think i figured it all out so i don’t need the jar anymore.
also one quick thing… if you know a lot about networked gaming could you briefly summarize how lobbies are made? just a simple one where users can chat and possibly challange each other to a game…
If you conceptualise your problem and group actions that are similar The design should be straight forward.
Technically for chat you could do the same thing but on the server aggregate any messages on a queue then at the head of the queue poll and ‘broadcast’ it back to every client. As for lobby like functionality depends on what you want battle.net like: separate chat separate list. Integrated send invite messages to ppl in the chat. I suppose I could draw that up for you but that would only make you slowly more and more depended on others to come up with the design. Diminishing your usefulness for a company and making future job interviews more difficult for yourself. As questions related to these kind of problems usually is the meat on the bone(or w/e the right metaphor is.)
i was not asking you to write the code for me (but its very kind of you to offer) nor am i ever going to work in the field of game programming. im a mechanical engineer major at the university of texas and recently took a class on java and thought it might be fun to develop a game.
i was merely asking conceptually how lobbies are handled. i was thinking the queue thing would be good for messages like you said however how do you handle the list of people in the lobby? Currently with the game i have when a user connects to the server it creates an object out of that player and then it matches players up together and creates a game object and passes the two player objects as parameters. so i was thinking since each user is already an object it wouldn’t be too hard to use maybe a resizable array and as each new user connects to the server add them to the array?
probably not the best way? am i at least heading in the right direction making each new user a player object?
Arrays are like udp you don’t need them unless you know for an unmistakable fact that you do.
Always use an List or a Set or if you write some method that does some function that can be applied to both use a collection - since most of those methods are already made for you: use an List or Set.
Yes from a servers point of view a socket can be seen as abstraction of a connection / remote node / client / player. Since you likely want to store information more information(like name for example) wrap the socket in a object - use the highest possible abstraction and code against it. Put these in a Set and your there. No one ever said anything needs to be hard or complex to work, so yes that’s the right direction.
so if you had to suggest one would you use set or list? whats the difference?
you already addressed my next question and that was giving each player a username.
this is what my ShotPlayer object looks like right now.
import java.io.*;
import java.net.*;
class ShotPlayer extends SocketConnect
{
private ShotDaemon daemon = null;
public ShotPlayer(ShotDaemon server, Socket sock)
{
super(sock);
daemon = server;
}
public void run()
{
daemon.waitForGame(this).playGame(this);
}
public void closeConnection()
{
super.closeConnection();
if(outStream != null)
{
send("GAMEOVER");
}
}
}
whats the best way to link a name to a particular socket? have the server accept the username from the client and then create another object out of the shotplayer and the username? <<< doesnt seem like the best way but was what i was thinking.
Difference between a Set and a List is the same as the mathematical interpretation.
They are both collections, Set is by default unordered and contains no duplicates (SortedSet is an ordered one without duplicates, equality is defined by the equals() method defined in object (override where necessary together with hashcode() -> why they are linked check google) with a List the determining factor is the order and permits duplicates.
Whether null element is allowed is dependent on the implementation.
Implementations to use as defaults are as follows:
List - ArrayList
Set - HashSet
SortedSet - TreeSet
Don’t change them unless profiling dictates that they should be changed.
As for your code, in this particular case I would go for composition. (just google Composition versus Inheritance)
i think im having a bit of a problem with the event dispatcher. im trying to create my lobby with chat and so far i have a text field for the user to type messages in and a text area that displays all messages and a button to send messages with. However, after the client has recieved the message from the server i cannot get it to display with the textarea.append(String) method. i have tried researching it on google but it doesn’t make much sense to me.
any advice? i know for a fact the problem is not the server because if append is called from a button click it will work but not when called in a run method.
It’s probably an event dispatch thread (EDT) problem - when called from the button press actionPerformed method, the code is run from the EDT, but in your own run method it’s not.
Try running the code in SwingUtilities.invokeLater(new Runnable(){public void run(){code…}});
ok but im not using swing im just using regular textField and textArea…
so the method you mentioned i put in my run method?
ok i got it to work using what you said… does it matter that im not using swing though? is there a better method to use?
That method directly calls java.awt.EventQueue Swing utilities just has some additional handy methods. - I wouldn’t be put off too much by swing being in the name.
Is there a particular reason your sticking to awt?
no not really. i was actually thinking of switching to swing but i think when i initially started making a game some people on this forum suggested i stick with awt. Would you suggest using swing?
I know swing components are supposed to be lightweight and there appearance is platform independent so it does seem like i should switch.
Well the general rule of tumb has been that if you use active rendering you should stay away swing.
Dependence on how your gui is set-up. If the components are going to be in your in your active rendering bit I would chose neither as far as widgets are concerned. Make your own as it’s probably less work and ppl don’t expect a game to integrate with an OS anyway. I mean games are suppose to be fun - OS’s are dull. the only thing you should perhaps pay attention to is that those self written widgets should grow as the resolution increases. But since those aren’t ‘part’ of your game you can just draw them bigger.
you pretty much addressed a question i would have asked down the line becuase you are right the supplied textField and buttons are boring. How do you go about creating your own?
as for my lobby system i have a chat system up and running however now i want each client to be able to see a list of the users connected (and possibly at some point have private chat and challange a specific user to a game). im using arraylist on the server side for all users that are connected but how in the world are you supposed to transfer and entire arraylist to the client and also update it as people come and go?
[quote]but how in the world are you supposed to transfer and entire arraylist to the client and also update it as people come and go?
[/quote]
Same way you send anything - serialise to bytes and de-serialise back.
ok, that was just mean. -And might also lead you down the wrong path of using java’s buildin serialisation.
I’ll try to write up some stuff but first I need to catch so deadline’s, I’ll edit / reply to this post a bit later.
Sorry for not following up earlier.
Just draw them as any thing else in your game but don’t make them apart of your logic and draw them a-top of everything else, on mouseclicks check if there is a ‘component’ on that coordinate and dispatch a mouse event to that component. That should make implementing buttons straightforward. as for text fields, have a object in which you track focus, have it contain the object that has focus and what (keyboard)events it’s interested in. Append keys to the string resp remove on backspace. All the tedious stuff like validation etc are probably overkill for your game just filter A-Z + -_ or something for names. As for tracking which ‘component’ should have focus just add additional logic between the ‘check-if-theres-a-component-at-that-coordinate’ and ‘dispatch-event-to-component’
First you must figure what behaviour you demand from client. Is it really that much of a problem if the user list goes out of sync if so how short should syncing be. Since your using tcp with it’s guaranteed delivery as long as your connected, you can also go with sending the chat list on enter and publish modifications(join/leave) to your clients. You can also go hybrid and send modifications and do a complete sync occationally.(Considering your using tcp - going out of sync, given that you can guaranty that publishing is always done, is impossible.)
As for the next step, make sure your familiar with (de)multiplexing - (data and it’s context) vs (data + metadata outside of context).(there are perhaps easier explanations of this concept google) Context takes up no space so it’s ‘free’ - how things are put into context is defined by a protocol.
Some examples:
(if ordered:)
Does cas like pie?
yes.
Is Java hard to learn?
no
(from the context you can derive that the answer follows directly after the question. - human protocol - humans aren’t physic so it could hardly be any other way)
What where the answers to the teacher’s questions?
yes, no
(almost no one is going to answer that way as ppl generally mess up the order - if they remember the questions at all. Also if the questions where asked on different occasions, you might not even be sure you got the same questions)
What where the answers to the teacher’s questions?
Java wasn’t hard to learn.
Cas likes pie.
As you can probably spot if you can make use of the context you need less text to answer.
An more programming-like example would (I gues) be:
‘cas,mark,michael,kev’
vs
‘1 cas’
‘2 mark’
‘3 michael’
‘4 kev’
A slightly more complex example:
‘100 cas,mark,michael,kev’
‘101 cas’
‘102 mark’
‘103 michael’
‘104 kev’
You can derive the same information from both data one just makes use of the context and is smaller. - off-course this only matters if you care about bandwidth esp as constructing information from context might eat away at other resources.
An example would be vectorised graphics, they are smaller but require more processing power to put on the screen.
As you might have picked up from earlier examples transfering a List Strings can be represent a a String itself easily enough. Strings can easily be converted into bytes which can be send. Now you don’t have a list of Strings you probably have a list of player objects. Select first what you actually want to share(which data on the server you might keep the ip adress around on the player object - doesn’t mean you have to send it to your clients) make up a format convert it to string or directly to bytes.
A pointer here:
class Player {
String name;
…
}
Player one = new Player(“Michael”);
one.asMessage() -> “Michael”
Player two = new Player(“Michael”);
one.equals(two) -> false - please note that you did have loss of information here: whilst multiplexing and demultiplexing you lost object identity, you probably don’t care about this but in other similar causes you might care.
Simple solution is to define your object identity differently (override equals + hashcode to use name).
Regarding identifying - say you uniquely define players by firstname lastname it’s probably not a good idea to repeat that every message as that takes up relatively a lot of space esp if your identifier uses a lot of fields, now dba’s figured out long ago that using a unique number is usually more efficient.
A final timbit of information: a integer 1000 takes a lot less bytes as four chars: ‘1’ ‘0’ ‘0’ ‘0’. - again this might or might not matter also if your lazy you can always just compress your stream characters though hand tuned ‘compression’ is probably more efficient.
I would recommend sticking as doing clever things with bytes is probably premature optimalisation.
hope these pointers help, feel free to specify where I messed up -> made it way too obscure.
//edit hmm should probably add a timbit about separators and escape characters data
first of all wow thanks for the help i cant imagine how long it took you to write all that.
creating widgets seems simple enough however textField seems like it could be a bit complicated. For instance scrolling? in the chat i would like each new message to be on a new line and obviously when the text reaches the bottom of the screen or widget it should be scrollable. how do you implement that?
as for transfering the userlist i already have each player objected created with a userName to identify it and yes thats all i need to transfer not the actual object. I believe i will go with when a user connects the server sends the current user list and later on sends an update with a leave/join.
getting into protocol is kinda where things get confusing because the server and client have to send so many different types of messages. im having trouble making the client understand what to do with the data it recieves. it seems like an easy way to do this (tell me if this is even possible) would be to say:
update: userName
remove: userName
message: “message”
etc.
preface the type of message like above or even shorten it to just one letter and a colon to represent the type of message. however after that is there a way to remove a portion of a string? for instance with message: “message” i would have to remove “message:” and display the rest to the user.
sorry if thats a little confusing…