i want to build a realtime multiplayer game with google play services where when a player moves a card on his screen, the other player sees the movement the first player made. Basically, i do not want to pass just a score between players, but actually make them play on the exact same screen and see the movements of their oponents.
I followed this tutorial: https://developers.google.com/games/services/android/realtimeMultiplayer
And it seems like the players send byte arrays between each other. How can i pass the whole game state as byte array?
If it helps to understand: Say i want to build a realtime racing game using google play services, how can i make the players see each others cars and cars' movements?
Is there an example with source code about it so i can study it?
I ran into a similar problem, and I believe I have something for you to check out:
http://www.tutorialspoint.com/java/java_serialization.htm
Unfortunately, I haven't implemented it correctly, but I think I am getting close.
The whole idea of serialization is that you get your game state as an object (Probably make another class like Race State or something) that has all your data, as fields, write it as a byte array, and return that byte array when you are sending the reliable/unreliable message (I still suggest reliable unless you plan to correct for missing/out of order packets).
On the other side (reading the packet) you just accept the byte array (in onMessageReceived()), call getMessage() (which I assumes gets rid of any extraneous transfer data), and read the bytes and cast it as your gamestate object. You should then be able to get() the field data for your game state and use as you need.
Again, it is still a work in progress for me (frustrating, I know), but do not bother sending strings as bytes or encoding, that muddles up the data. I wish the documentation was a little more clear on implementing this, although it assumes most people have worked with network data before.
Feel free to comment with questions, and good luck.
Related
I have been working on a simple game in Unreal Engine 4. I am trying to make it so when a player is hit by a cube they take damage. However, I am stuck on creating a condition. I have previously used:
to set up a condition where a player only takes damage if they are touched by the cube (In my cube pawn blueprints).
This doesn't work however - when trying to set-up my health bar:
This shows that I am now using entirely new variables to attempt to get a successful updating bar.
Without setting it up so when a cube hits the player, the player takes damage, the player will take damage from simply jumping at walking into other surfaces.
I have created a function that successfully updates my current health and max health so I don't need to show, or need help with the maths or updating the widget. Is there a way for me to use the branch to create an if statement that checks the contact is form a cube?
I am quite new to blueprints and have mostly developed through the use of tutorials. If you need clarity on my question or you don't understand what I am asking please leave a comment and I will try to update. I have looked long and hard for an answer, but I have found that Unreal Engine 4 hasn't got many questions that I can tailor the answer to my situation. If the answer is already in another post on this website, comment saying so and I will remove this post.
Thanks for any help you can give me :)
(This also has a itch.io page for me to quickly share to my friends so I will also credit the person who helped me there)
If I've understood your question correctly, I believe you are just asking how to check if the cube has hit the player character and not some other actor.
Instead of using an if statement as you suggested, you can just cast the Other Actor property to your first person character. If the cast is successful then it hit the character, if the cast fails, it hit something else. You can then call the damage function which you said you've already created. Below is a basic example you could use in your cube blueprint. You will also need to make sure you have a collision box surrounding your cube mesh (and your character, but I can already see that in your screenshot).
I currently have two nodes in use, one of which is a boat (polygon) and the other a straight vertical rectangle. The boat needs to be a polygon so that it can be hit via projectiles, however this causes me problems when it collides with the vertical rectangle due to the many sides it's coming into contact with. Hence a piece of code I want ran only once is instead ran multiple times.
Is there anyway to make the collision detection only function for the first contact so that it doesn't repeatedly call?
I'm aware of setting the categoryBitMask to 0 however doing so renders the boat unable to be hit by anything else- something I don't want. Additionally I considered using a boolean variable to only run the code once whilst the boat is passing through, however as previously mentioned, due to the different sides, the code think it's passed through when in fact it's just hitting another side of the boat.
I don't have much code to show here other than setting physics bodies so I don't think it's necessary, but inside didBeginContact() I'm using a categoryBitMask comparison to check for collisions.
I may have not explained this very well, in which case I apologize, my English is not the best!
If anyone can help, it'd be much appreciated, thanks.
You can combine the individual physics bodies of your ship into a single body with
+ (SKPhysicsBody *)bodyWithBodies:(NSArray *)bodies
and then test for contacts with the composite body instead of the individual components.
If you are subclassing your node then simply create a BOOL property for it.
BOOL alreadyHit;
If you are not subclassing, use the SKNode's userData dictionary and create an entry for the same.
I've been using the Game State Management sample which has worked so far. I've hit a snag though: when Tombstoning, the screens are serialised; the only trouble is, the MessageBoxScreen has event handlers for Accepted and Cancelled.
What's the best way to serialise these? I did a bit of research on using Expression Trees but this seemed overly complex for what I wanted to do.
How do you serialise these? Or... What alternative approach do you use to save the state of a screen that contains delegates?
I'd definitely steer clear of attempting to serialize anything remotely resembling a lambda, or for that matter, named methods. Remember: you're storing state, and nothing else.
Depending on how far and wide your various assignments to these delegates are, you might be able to get away with maintaining a Dictionary<String, WhateverDelagateType>, serializing the keys and looking up the callbacks after deserialization.
Another thing to consider--I'm no expert, but reading between the lines it sounds as if you're working towards tombstoning a very temporary modal dialog. Do you really want that? You might be better off bringing your user right to the high scores table, or whatever follows your dialog, on his/her return.
I decided against this. I instead persists game flow as a kind of 'flow chart'.
The flow chart is declared in code and has properties 'LastShape' and 'LastResultFromShape'.
In my code, I rebuild the flow chart definitions each time, something like this:
flowChart.AddShape( "ShowSplash" );
flowChart.AddLine( "MainMenu", ()=>lastResult=="Clicked" || lastResult=="TimedOut");
flowChart.AddShape( "MainMenu");
flowChart.AddLine( #"ShowOptions", ()=>lastResult=="OptionsClicked");
flowChar.AddLine( #"ShowSplash", ()=>lastResult==#"TimedOut");
etc.etc.
The flow goes from the top down, so 'AddLine' relates to the last shape added.
After tombstoning, I just read the last shape and the last result and decide where to go in the flowchart based on that.
I am extreamly new to programming. I am writing a simple game which gives you a score everytime you run the game. I am trying to figure out how to save the scores and recall them to a high score page. I am trying to figure out if saving to a dictionary is the best way, or using an array, or what. Plus, how does the dictionary handle an object that needs to be updated and changed, everytime the high score is beat?
well, the simplest way is just to store them in a text file. How this works is relatively simple in any programming language and most languages will have some documentation online that explains the functions for reading and writing from files. How to actually handle the values in memory (array, list, etc) will depend on the language you are using.
I'm new to Obj-C too, but from a pure programming point of view I'd agree with maqleod. You're problem is twofold: you need a data structure to hold the highscores in memory whilst your game is running. I think an NsMutableArray would serve you best, because it can sort itself for you (an NsDictionary is better when you want to lookup discrete values). On a higher level however, you need to save this data between sessions - data persistency. Thats where saving to a text file comes into play. If you look up / google "archiving objects" you'll find that it' really quite a trivial task in obj-c to save the contents of an object (eg. An array containing highscores) to a file - and of course initialize it again from file next time the game starts up.
I am trying to get my head around how to design the following system, which I think can be defined as a finite state machine:
Say we have a pile of 16 building blocks (towers, walls, gates) together forming a castle. The player can drag the blocks to 16 places on a floorplan and if done right they will see the whole castle. All towers (there's four of them) are equal so they can go on any of the four corners. Same goes for some of the walls.
All in all there are 16 spots on the floorplan where you can put a building block and each of the spots can have 17 "states": empty + either one of the 16 building blocks. Doing some maths this leads to 17^16=a LOT of combinations.
The program starts with an empty floorplan and a pile of building blocks. It should then show a message like "build your own castle, start with the tower". When the user places a tower correctly, it should say "well done, now build all four towers". You get the idea.
Problem is: there are so many things a player can do. Put a block at the wrong place, remove a block, correctly put walls or towers all over the floorplan ignoring the directions given to them, etc.
It would be awesome if I could avoid having to use thousands of if-then statements to decide wether I should take the next step, show an error message or go back to the previous step based on what the player is doing.
How would you describe the NEXT, PREVIOUS and ERROR conditions for every step of the building sequence? Are there any design methods for this? Thanks a lot for your input.
Try to do this declaratively. Define an enum (or possibly classes) describing the kinds of blocks. Define and construct a 4x4 2D array describing the sets of permissible kinds of blocks in each position (implement the sets as lists, bitfields, whatever suits you best). Whenever a player tries to place a block in a position, check whether it is permissible against the 2D array. If you want to have particular messages for a position being correctly filled in, also put those in the same an array.
I don't know if a FSM is really what you are after: what kinds of sequencing constraints are you looking to verify? Does it matter whether towers are built first? From the rest of your description, it sounds like the above goal state description would be more suitable.