Many times there are no players in a game or all are already busy in on-going game sessions. This will let some of the players quit the game as they will have no one to compete with. Situation can be worse in turn based games. For example, you might need 4 players to start the game but there are only 3 players available. So, a better option should be available to handle this situation. In AppWarp S2, developers can create virtual users.
Virtual User is an extension of the User class in AppWarp S2. You can create an object of this class and add it in the room. This will let other players think that there is a new player. The client side code does not have to be changed, the game on client side will treat all these virtual users as they were real game players.
Adding Virtual User
Adding virtual user is very easy. All you have to do is create an object of class ‘VirtualUser’. Then add this user in a room. For example:
IUser user = (IUser) new VirtualUser("FakeUser", myRoom, myZone); myRoom.addUser(user, true);
In the above code, I am creating a virtual user with the name “FakeUser” in room “myRoom” of zone “myZone”. Then I added this virtual user in myRoom with addUser method. Although, we have a specific room in VirtualUser’s constructor still we are adding it to the room because this will generate a notification and send it to all the users in that room. All players will get the notification that a new player has joined the room.
Creating AI for Virtual User
VirtualUser will only act as a dummy user, it won’t do anything of its own. You have to write AI to simulate the behavior of VirtualUser. This will let you create Bots in your game. You can create a wrapper around VirtualUser or write AI logic in a separate class. When you are ready with your AI and want to perform an action on behalf of VirtualUser, simply call the BroadcastChat method of IRoom with the message and name of the bot user. For example:
room.BroadcastChat(botUser.getName(), “Hey! I am Moving”);
Other players will treat it as the real player and react as it was sent by a real player himself. Therefore, you will not have to change the code on client side.
There are lot of use cases for VirtualUser and some of these are as follows.
Generating Users when there are none
In the beginning of the game, there are going to be less players. This can degrade the quality of game experience. Suppose a player comes in your game to play multiplayer mode but if there are no players then you can generate fake users to play against him. This will improve the overall game experience of the player. Example of such a game can be Card Games where you need at least a few players to start the game.
Generating Server Side Enemies
In MMOs or open world games, there are enemies that are generated by the server. You can use VirtualUsers to create enemies. The advantage of this is that enemies will appear to all the players at the same location & time with the same health. For example, this could be a great feature for co-op games.
In many card like games, there is a need of a dealer. Since every client is a player, there are no dealers available. This can be done with Virtual User. You can create a VirtualUser in each room and assign him the AI for dealer.
Definitely a lot can be done with this new feature and I believe it will be very helpful to you, developers. Feel free to share your use cases in comments box below.
We understand how important a sample is. They help you learn better and this is why we have created a basic sample for you. You can grab the sample from here.
In the above sample, we are creating virtual users so if no new players join a room for 30 seconds then the game will start. Since in this sample, we are dealing with turn based rooms, the virtual users will also get their turn. The sample simulates a basic behavior for these virtual users by sending moves on behalf of these virtual users.
Please have look at the code to understand it better.
The VirtualUsers allow you to create fake users in your games. They are very easy to implement and do not require any changes on the client side.