Using a Unity standalone as a backend

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

narfi
Posts: 14
Joined: 30 Jan 2015, 20:22

Using a Unity standalone as a backend

Postby narfi » 05 Feb 2015, 17:42

I would like to use Unity as a backend for the physics and AI (and possibly authoritative movement)
Probably be using the mmoroom, but possibly just a normal room as I do not expect rooms to be too large. (a few players and a couple dozen npcs)

From reading your docs it seems that mmoitems can only be spawned through extensions?
Is there a way for one user (the backend instance) to spawn/control multiple users in a room with their individual user variables?
Is there a way for one user (the backend instance) to control the user variables of other users? (to make it authoritative?)

I am just a hobbiest, and still learning alot, but reading as much as I can. I just want to figure out the best way for me at my stage of learning to do something :)
My last burst of studying had been with photon, but I was wanting to focus more on mobile this time around and read that it wouldn't work on android, so I chose smartfox.
Turns out smartfox doesn't either unless you get Unity Pro, or what I did which is buy Good 'ol Sockets from the unity asset store, which I believe would also allow me to use photon as well.
(I already have smartfox installed on a linux server though, and would rather not switch back to using a different server again(which photon requires) unless I have to)

So, here is my rough plan of attack, all of which I will be modifying as I go and learn more about the proper processes.

Doing everything through unity.
Backend will be standalone without any need for human input, it will run the NPCs, AI, physics, etc...)
The client sends input to the backend through the server. (updating user variables?)
The client predicts movement based on their own input.
The backend moves them, and sends back their actions/position/etc... (updating their user variables?)
The client smoothly adjusts the difference between their predicted movement and what the backend sets.
The client places and moves other players, NPCs, and AI according to where the backend sets, and smoothly adjusts them between updates.


Is this logical?
Is this possible with smartfox?

Thanks for your time :)

narfi
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using a Unity standalone as a backend

Postby Lapo » 05 Feb 2015, 18:04

Hi,
yes what you're thinking of doing has been discussed in other threads and I think it has been employed in several games with success.

From reading your docs it seems that mmoitems can only be spawned through extensions?
Is there a way for one user (the backend instance) to spawn/control multiple users in a room with their individual user variables?
Is there a way for one user (the backend instance) to control the user variables of other users? (to make it authoritative?)

Technically you can spawn MMOItem from the Extension code, so what you need to do is just send specific requests from your "Unity Server Component" (USC) to the Extension that will do the job. Similarly you can manipulate anybody's variables, but again it will be the SFS java code that will do it.

The flow would be: USC --> SFS Extension --> Game clients
The USC runs its logic, tells the server Extension code what to do (i.e. spawn MMOItem), which in turn updates the game clients.

So bottom line, it is both logical and possible. :)

Cheers

ps = About Android and Unity PRO you're right, but that's a limitation with the free license of Unity. Essentially they don't provide socket support in Android/iOS with the free edition.
Lapo
--
gotoAndPlay()
...addicted to flash games
narfi
Posts: 14
Joined: 30 Jan 2015, 20:22

Re: Using a Unity standalone as a backend

Postby narfi » 05 Feb 2015, 18:37

I have 0 experience with Java, but from your answer it seems that C# from unity only is not possible/practical, that I will need atleast some work on an extention for what I need?
I dont mind learning and working towards something, I just want to make sure I understand the right approach.

Basically what I read from your answer is that,

My backend client scripts will send a request to the extension to spawn the mmoitems (npcs).
My backend client scripts will be able to read the player variables directly from sfs to control them locally on the backend (no need for an extension)
My backend client scripts will send update requests to the extension for all movement/etc.... updates for the players and mmoitems(npcs)

player client scripts send input directly to sfs through player variables (no need for an extension)
player client scripts get AOI info for all players and mmoitems directly from sfs (no need for an extension)

So for my Extension I will need functions for,
Spawning Items/NPCs
Destroying Items/NPCs
Updating Item variables
Updating player variables

Is this correct?

Also, how would I handle AOI for the backend, as I want it to be able to see ALL of the players/items in the room not just in a given area?
narfi
Posts: 14
Joined: 30 Jan 2015, 20:22

Re: Using a Unity standalone as a backend

Postby narfi » 11 Feb 2015, 03:09

So I have been having some fun learning how to write extensions, and toying with how things work.

I still have not figured out how to go about giving the master client info on users and items in the whole room without limiting it to an AOI.

Also do you have the source for your fps and mmo extensions? It would help me learn faster if i could look at those.

So two specific questions.

1. How to address the need for a single user (master client) to have an AOI that covers the entire room.
2. Link to the source code for your fps and mmo extensions.

thanks,

narfi
narfi
Posts: 14
Joined: 30 Jan 2015, 20:22

Re: Using a Unity standalone as a backend

Postby narfi » 11 Feb 2015, 08:55

I see that server side mmoroom allows getting all room items and users (which in turn could be fed to my master client). I just want to clarify that it's not built in client side before I progress further.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using a Unity standalone as a backend

Postby Lapo » 11 Feb 2015, 11:28

narfi wrote:So I have been having some fun learning how to write extensions, and toying with how things work.

I still have not figured out how to go about giving the master client info on users and items in the whole room without limiting it to an AOI.

That's a problem, because that data resides on the server side and it can get pretty huge. Moving it back and forth between client and server is not an easy task.
I would recommend delegating the operations in the MMORoom to the server side logic. It's the only sensible way I can think of

Also do you have the source for your fps and mmo extensions? It would help me learn faster if i could look at those.

Yes, absolutely. Download the Unity Example Pack here:
http://www.smartfoxserver.com/download/sfs2x#p=examples
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using a Unity standalone as a backend

Postby Lapo » 11 Feb 2015, 11:29

narfi wrote:I see that server side mmoroom allows getting all room items and users (which in turn could be fed to my master client). I just want to clarify that it's not built in client side before I progress further.

Yes that's correct and it's the main advantage of the MMORoom on the client side.
Lapo

--

gotoAndPlay()

...addicted to flash games
narfi
Posts: 14
Joined: 30 Jan 2015, 20:22

Re: Using a Unity standalone as a backend

Postby narfi » 12 Feb 2015, 06:49

I am getting more into server api questions now, shoudl I start a new thread in that section, or is it ok to keep my progress/questions in this single thread?

I am trying to basically duplicate the PROXIMITY_LIST_UPDATE but with info for the entire room and send it just to my master client.
Java is new for me so still learning(sorry)

First question:
In C# scripts I have always used Start(), Awake(), Update(), FixedUpdate(), and OnGUI(). It seems that init() is similar to Start() or Awake() which is simple enough, but some quick googling doesnt show me the equivelant to Update() or FixedUpdate(), is it main() ? (thats what I have gone with for now, but not got far enough in to test it yet)

Second question: How to I put lists into an SFSObject?

Here is what I have so far,

Code: Select all

public class MyExtension extends SFSExtension
{
   
   MMORoom room;
   
    @Override
    public void init()
    {
       room = (MMORoom)this.getParentZone().getRoomByName("TestRoom");

       
        trace("Hello, this is my first SFS2X Extension!");
        //addRequestHandler("TestRoomVar", TestRoomVar.class);
        addRequestHandler("AddNPC", AddNPC.class);
        addRequestHandler("SetNPC", SetNPC.class);
    }


   public void main()
   {
      UpdateMasterList();
   }
   
   List<User> oldUserList = new ArrayList<User>();

   List<BaseMMOItem> oldItemList = new ArrayList<BaseMMOItem>();
   
   
   
   public void UpdateMasterList()
   {
      List<User> currentUserList = room.getPlayersList();
      List<BaseMMOItem> currentItemList = room.getAllMMOItems();
      
      List<User> addedUsers = currentUserList;
      List<User> removedUsers = oldUserList;
      List<BaseMMOItem> addedItems = currentItemList;
      List<BaseMMOItem> removedItems = oldItemList;
      
      addedUsers.removeAll(oldUserList);
      removedUsers.removeAll(currentUserList);
      addedItems.removeAll(oldItemList);
      removedItems.removeAll(currentItemList);
      
      boolean isNewMaster = false;
      User newMaster;
      User master;
      
      for(User user : addedUsers){
         if (user.getName() == "Server")
         {
            isNewMaster = true;
            newMaster = user;
         }         
      }
      

      for(User user : currentUserList)
         {
            if (user.getName() == "Server")
            {
               master = user;
            }
         }
      
      

      
      if (isNewMaster)
      {
         sendNewLists(newMaster);
      }else
      {
         sendLists(master);
      }
      
      
   }


   private void sendLists(User master)
   {
      
      ISFSObject obj = new SFSObject();
      
      obj.put??????????("addedUsers", addedUsers);
      obj.put??????????("removedUsers", removedUsers);
      obj.put??????????("addedItems", addedItems);
      obj.put??????????("removedItems", removedItems);
      
      send("MASTER_LIST_UPDATE",obj,master)
      
   }


   private void sendNewLists(User newMaster)
   {
      ISFSObject obj = new SFSObject();
      
      obj.put??????????("addedUsers", currentUserList);
      obj.put??????????("removedUsers", new ArrayList<User>());
      obj.put??????????("addedItems", currentItemList);
      obj.put??????????("removedItems", new ArrayList<BaseMMOItem>());
      
      send("MASTER_LIST_UPDATE",obj,newMaster)
      
   }

   

   
}


So, 2 questions for now,

1. Is main() the proper java equivelant to the c# Update() I am used to?
2. How to I put lists into an SFSObject in my simulation of the PROXIMITY_LIST_UPDATE?


3. of course suggestions on anything else is welcome as well :P
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using a Unity standalone as a backend

Postby Lapo » 12 Feb 2015, 11:26

narfi wrote:I am getting more into server api questions now, shoudl I start a new thread in that section, or is it ok to keep my progress/questions in this single thread?

I am trying to basically duplicate the PROXIMITY_LIST_UPDATE but with info for the entire room and send it just to my master client.
Java is new for me so still learning(sorry)

I am confused.
The PROXIMITY_LIST_UPDATE tells you which other users are within your AOI range. In other words which users you can at this moment interact with.

If you need the info for all the Room then you don't need a PROXIMITY_LIST_UPDATE, nor an MMORoom. You just need a regular Room where anyone will receive events from anyone.

First question:
In C# scripts I have always used Start(), Awake(), Update(), FixedUpdate(), and OnGUI(). It seems that init() is similar to Start() or Awake() which is simple enough, but some quick googling doesnt show me the equivelant to Update() or FixedUpdate(), is it main() ? (thats what I have gone with for now, but not got far enough in to test it yet)

Start(), Awake(), Update(), FixedUpdate(), and OnGUI() are Unity events.
There is no correspondent on the server / Java side.
Update, FixedUpdate and OnGui have to do with rendering which of course doesn't happen on the server side.


Second question: How to I put lists into an SFSObject?

Using an SFSArray if the list contains objects of different types, or a type array... such as putIntArray(...), putUtfStringArray(...) etc...
See the docs for all the details.
Lapo

--

gotoAndPlay()

...addicted to flash games
narfi
Posts: 14
Joined: 30 Jan 2015, 20:22

Re: Using a Unity standalone as a backend

Postby narfi » 12 Feb 2015, 12:24

Lapo wrote:I am confused.
The PROXIMITY_LIST_UPDATE tells you which other users are within your AOI range. In other words which users you can at this moment interact with.

If you need the info for all the Room then you don't need a PROXIMITY_LIST_UPDATE, nor an MMORoom. You just need a regular Room where anyone will receive events from anyone.

I apologize for the confusion, I am confused by your answers as well :)
As the title of the thread and first few posts show, the intent would be to have the 'master client' running local to the smartfox server in order to run physics and AI logic. In order to do this, the 'master client' needs to see the entire room and is not restricted by bandwith bottlenecks, where the actual players connecting over the internet should enjoy the benefit of the mmoroom and areas of interest.

I asked if this was a logical approach and you said yes.
I asked how to do it and you said it can only be done on the server side.
I did it on the server side and you tell me it is wrong.
One of us isn't communicating well and it is probably me, so again I apologize for that.


Lapo wrote:Start(), Awake(), Update(), FixedUpdate(), and OnGUI() are Unity events.
There is no correspondent on the server / Java side.
Update, FixedUpdate and OnGui have to do with rendering which of course doesn't happen on the server side.

Again I apologize for using the incorrect terminology.
The question remains though. What is the proper way to execute something every 'cycle' (whatever your java extensions uses for cycles)

Lapo wrote:Using an SFSArray if the list contains objects of different types, or a type array... such as putIntArray(...), putUtfStringArray(...) etc...
See the docs for all the details.

Thank you, I have been reading, rereading and rereading. It was still not clear to me which is why I posted my example which shows the lists are are lists of users, and lists of mmoitems, and why I asked specifically about them. I will reread and reread again.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using a Unity standalone as a backend

Postby Lapo » 12 Feb 2015, 14:44

narfi wrote:
Lapo wrote:I asked if this was a logical approach and you said yes.
I asked how to do it and you said it can only be done on the server side.
I did it on the server side and you tell me it is wrong.

No I don't think I did that, but I have possibly misunderstood your previous post.

One of us isn't communicating well and it is probably me, so again I apologize for that.

No problem.
The only part where I am confused is this, from your previous post:
I am trying to basically duplicate the PROXIMITY_LIST_UPDATE but with info for the entire room and send it just to my master client.
Java is new for me so still learning(sorry)

I don't understand why you need to "duplicate" it, where you need to duplicate it also.
Can you expand on this?

Lapo wrote:Start(), Awake(), Update(), FixedUpdate(), and OnGUI() are Unity events.
There is no correspondent on the server / Java side.
Update, FixedUpdate and OnGui have to do with rendering which of course doesn't happen on the server side.

Again I apologize for using the incorrect terminology.
The question remains though. What is the proper way to execute something every 'cycle' (whatever your java extensions uses for cycles)

Technically there are no cycles on the server side, at least not in basic Extensions.
Extensions simply respond to events, typically client requests and internal events.

If you want to create a looping interval-based event a là Update() in Unity you can start a scheduled task using the TaskScheduler:
http://docs2x.smartfoxserver.com/Gettin ... wtos#item7


Lapo wrote:Using an SFSArray if the list contains objects of different types, or a type array... such as putIntArray(...), putUtfStringArray(...) etc...
See the docs for all the details.

Thank you, I have been reading, rereading and rereading. It was still not clear to me which is why I posted my example which shows the lists are are lists of users, and lists of mmoitems, and why I asked specifically about them. I will reread and reread again.

SFSObject/Array support a number of primitive data types: byte, float, double, int, long, string, their array forms etc...

Other than that you won't be able to transmit any type of class from one end to another without some sort of (custom) serialization.
In particular User objects are highly complex (and nested) objects with references to inner objects that are not even serializable (such as a SocketConnection)

You will have to figure out a way to only extract the information that you need to send from the User object and serialize it to some other simplified type, which then can be transmitted and deserialized back on the other end. Hope it makes sense. It might not, if you've never done this before :)

Similarly with MMOItems. The class already provides a toSFSArray() which serializes the object for clients.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
narfi
Posts: 14
Joined: 30 Jan 2015, 20:22

Re: Using a Unity standalone as a backend

Postby narfi » 12 Feb 2015, 19:27

Lapo wrote:If you want to create a looping interval-based event a là Update() in Unity you can start a scheduled task using the TaskScheduler:
http://docs2x.smartfoxserver.com/Gettin ... wtos#item7

Perfect Thanks, I will read more on that.

Lapo wrote:You will have to figure out a way to only extract the information that you need to send from the User object and serialize it to some other simplified type, which then can be transmitted and deserialized back on the other end. Hope it makes sense. It might not, if you've never done this before :)

Not a problem, I can just as easily send just the IDs and then get all the information client side from those IDs. I was just thinking of doing it this way since that is how the proximity list update did it, but after reflection, I am guessing that is done in the client api as well and not actually sent over the network as User and Item lists.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Lapo wrote:

No I don't think I did that, but I have possibly misunderstood your previous post.
[/quote]

Lapo wrote:The only part where I am confused is this, from your previous post:
I am trying to basically duplicate the PROXIMITY_LIST_UPDATE but with info for the entire room and send it just to my master client.
Java is new for me so still learning(sorry)

I don't understand why you need to "duplicate" it, where you need to duplicate it also.
Can you expand on this?


This seems to be a fundamental concept that I have wrong, and that worries me.
I will go into detail again why/how I am trying to do this.

A normal room sends user entered and user exited signals to ensure that each player knows what other players are in the room.

An MMORoom sends proximity list updates to let each player know what is in their AOI. This helps the players client by reducing the need for as much network traffic as well as system resources.

A normal room does not have server items. (or does it? I will have to read back on that again)
An MMORoom has MMOItems that can be set up as server controlled NPCs if done properly.

I want to manage all of the NPCs(mmoitems), Their AI, Their physics, and in time probably even the players themselves through an authoritative approach.
Terminology may be my issue here....

Master Client is what I have been calling the single Stand Alone Unity application that will manage the NPCs, AI, etc.... will be on a local connection (in time) with the smartfox server and will be in charge of sending ALL transform and most variable updates to the smartfox server.

Player Client is what I have been calling the Stand Alone Unity applications (built for windows and android specifically but perhaps others in time). It will send player control variables and/or player transforms to the smartfoxserver depending on my stage of learning and the stage of the project. It will receive all information from the smartfoxserver and in time do its work in making it pretty and attractive. (for example changing animations based on movement, environment, and other variables)

The Player Client only needs to know about its AOI as it does not control any serious logic itself.
However, the Master Client controls ALL the logic, and thus requires ALL the information to make those decisions.

The Player Client uses PROXIMITY_LIST_UPDATE well and as intended.
The Master Client requires something more like a normal room with Joined and Left notifications, but also needs it for items as well.

The way I understand it, this need is very similar to the PROXIMITY_LIST_UPDATE except that it is not for the Master Clients AOI (it doesnt have one as it has not 'character' in the game, and never sets a position) but more where the Master's AOI encompasses the entire room.

Here is a scenario that perhaps explains it better.

The Master Client Logs on.
The Master Client Joins the room. (hopefully before any players do, but it is irrelevant)
The Master Client receives a list (my 'Master List') of all the players and items in the room.
The Master Client instantiates them in its physical world and takes control of them, each sending updates back to the server.
The Master Client tells the server to add an mmoitem at x,y,z which it does.
The Server sends the Master Client my 'Updated Master List'
The Master Client then Instantiates any added players or items and removes any removed players or items.
The Master Client controls them and continues sending their info to the server.
The player clients continue getting their AOI updates and 'playing' the game.

In my mind this seems logical, and I thought you had said it was logical up above when I first described it.

Here is a most beautiful mspaint rendition of what I am talking about:

Image
Attachments
smsmmo.png
(10.44 KiB) Not downloaded yet
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using a Unity standalone as a backend

Postby Lapo » 13 Feb 2015, 09:40

Thanks for the clarification.
I think it makes sense and I don't see particular problems.

Your Extension will react to any event in the Zone such as User login, join room, disconnection etc. Each of these notifications can be relayed to the Master Client so that it's in synch in realtime with whatever is happening.

Since the Extension can see anyone in the MMORoom, so does the MasterClient because the Extension can (again) send notifications to it about any change in the Room.

Similarly the MasterClient will run its own logic and send commands to the Extension which in turn will execute them and cause updates on the Player Client(s).

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 26 guests