Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Network.Instantiate used to be simple. Any tips?

Discussion in 'Multiplayer' started by Homicide, Feb 27, 2018.

  1. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    I have been trying for a couple hours, and just no luck.

    I have a random tile generated map that works great offline, and i want to replicate the generated version into the match / game room i create.

    I've tried NetworkServer.Spawn(go);, ive tried Network.Instantiate, and i've tried NetworkManager.Instantiate

    nothing seems to work. I've even dragged and dropped 20 tiles in to the Managers Spawnable objects, though i think that's ridiculous, because i want to generate the map at runtime., randomly, from several possible tile sets. The idea was to choose tile set in lobby, generate random map , or ten if you don't like em , then launch into game and serve / play on said map.

    I once was able to do this with ease with network instantiate, and i don't recall the need for a networkView on every single cell. Though i keep seeing mentions of every object needing NetworkID now.

    Is there any simple method to spawn objects to the network after i have created a room/ or started a match?

    Or any links to simple network Spawning tutorials?

    thanks guys.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The way the Unity HLAPI works is you create prefabs that you put a NetworkIdentity component on the root of. You add them to the spawnable objects list on the NetworkManager. Then on the server you instantiate them locally like any other prefab, and then pass the gameobject that was returned from Instantiate to the Spawn method to actually spawn that object from the server's scene onto the clients. It sounds like you're missing a step or so. Otherwise post your code you're trying.

    It doesn't sound very optimal for what you're trying to do though, as this workflow was intended for spawning some monsters, players, etc, but not necessarily for constructing levels. You might want to consider serializing the level, sending that as a Message to the clients, and having them reconstruct the level locally.
     
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    Thanks man , i actually just had my first successful spawn by doing that ...

    GameObject obj = (GameObject)Instantiate... etc
    NetworkServer.Spawn(obj)... and it worked. lol

    But yea, i'm not sold much on the loading 850 tiles into the damn spawnable objects box. Seems not very easy or intuitive.

    Perhaps if there's a way to reassign the whole array of objects at runtime? Is this possible? I wouldn't be so bad off just throwing a List or array at the spawnable objects ... hmm, something to ponder.
     
  4. Ellernate

    Ellernate

    Joined:
    Aug 25, 2017
    Posts:
    81
    You can assign objects to the spawnables list during runtime, however they will have to be prefabs. Prefabs are assigned asset ids in the editor, which is what is sent along the "Network.Spawn()" message so that remote clients know which object to spawn.

    For generating new objects during runtime, like I do in my game, you will need to spawn an empty object with a script attached and use syncvars, syncstructs, or messagebase to sync various properties such as colors and random seeds. Then when that data is received, you can reconstruct it locally on the client.
     
    Homicide likes this.
  5. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    Could you point me to an example snippet of assigning at runtime?

    Also... wont that mess up the other players if there list isnt the same?
     
  6. Ellernate

    Ellernate

    Joined:
    Aug 25, 2017
    Posts:
    81
    Code is in the docs: https://docs.unity3d.com/ScriptReference/Networking.ClientScene.RegisterPrefab.html

    Yes, if a client for some reason doesn't have an object registered at a received asset id, then it will throw an error and nothing will be created locally on the client. However, since this is using prefabs there shouldn't be a case where an object is missing.

    If you plan on creating random objects at runtime, then you should manually sync the data and construct locally
     
    Homicide likes this.
  7. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    Aight, cheers mate.

    Im curious if you have any advice for how you would go about creating a a random tile based map, for eg: 64 tiles, 8x8 like a chess board.

    Each tile i ahve has logic attached that as it places itself it knows to look behind to the side and verify it can fit, and so in the end i end up with 64 perfect tiles tucked into a GameObject{} that atm, i am spawning through the network Spawn abilities.

    It doesnt seem optimal though , and i would like to have for eg: 5-10 tile themes, that can create random maps that vary between 4x4 to 8x8.

    As i said , i already have written my generator and it works fine, but making it work for the network has been tricky so far.

    All tips are welcome.
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Off the top of my head, I'd position all the tiles on the server as you are now but without any networking. Then after you run all your logic to properly position them, store everything about each tile including the position they ended up in as a struct. Then I'd use a SyncListStruct to send the entire layout to all connecting clients. The clients then place all the tiles exactly as specified by the items in the SyncListStruct, and shouldn't need to run any of the placement logic that was run on the server.
     
    Homicide likes this.
  9. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    Everything you're saying makes sense and is the most logical approach, but i'm a little too new at the C# and UNet i think to quite understand the actual code implementation of such a task. Honestly, i was impressed i could build my little tile map generator, though i still end up with sectors that are unreachable occasionally. Baby steps huh. :p

    Thanks