Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Lord Bailey's Guide to Forge Remastered

LordBaileyMorgar edited this page Jun 15, 2020 · 12 revisions

Greetings fellow developers,

Before we get started I will be expecting that you understand basic C# and how the Unity editor works. You should not look at this as a complete expert level full guide but a helpful leg up to do some common things. Another thing you should note is that I am not presenting myself as a master of Forge so if you know of a better way by all means do that instead, this is just my way.

It is advised to have a backup of your generated files in case if you need to roll back

I will update this guide as I learn so it may feel empty for a time, but will grow as I do.

So Hello World! Let's make a multiplayer game.

How to destroy a player's object after they disconnect

if (networkObject.Owner.Disconnected) { networkObject.Destroy(); } Note: Should be called by all peers and not just the owner.

How to get the player who sent a RPC uint id

args.Info.SendingPlayer

How to spawn an object

NetworkManager.Instance.InstantiatePlayerCube();

Note: You need to setup the object in the NCW (That menu where you can set values) before you try to make a script. So After you make some thing like SuperAwesomePlayer you'll then create a script with SuperAwesomePlayerBehavior in the inheritance with BeardedManStudios.Forge.Networking.Generated; as a using statement. Next drag your model into unity, assign the script to it and make it a prefab. Then go into the bearded man studios prefab folder and find the networkManager prefab, You'll find a slot inside of it if you click on it, Drag and drop your prefab into the SuperAwesomePlayer slot. Esstentally it's a Gameobject Array which I'm sure you know all about. Now if you did everything right it should spawn. @!LordBailey if you need help.

2nd Note: Who ever instantiated the object is considered it's Owner!

How to know when player connected

` protected override void NetworkStart() { base.NetworkStart();

}

` This should work, just make sure you don't put anything before base.NetworkStart(); I know there is also other ways, But currently I haven't found it. So if you do know a way then please @!LordBailey so I can update this. Sharing is caring :)

How do I target only my player?

if(networkObject.isOwner)

The Server?

if(networkObject.isServer)

How to sync movement

First make sure you have two fields inside of your NCW for that object you made that should be position Vector3 rotation Quaternion

then inside of the Update code you'll want to do the following

` if(!networkObject.isOwner) { transform.position = networkObject.position; transform.rotation = networkObject.rotation; return;

//This says if you're not the owner of the object (The one that spawned it) you'll just update from the network. }

else { networkObject.position = transform.position; networkObject.rotation = transform.rotation;

//This says if you are the owner (Players that spawned this object) you'll set the network values based. }

//After this you can add whatever kind of movement code locally, Note you can also send RPCs to do movement but this is the most simple way that I've found.`

How to Send an RPC

Inside of the NCW under the object you're trying to send an RPC from (SuperAwesomePlayer) you'll need to add a RPC by clicking on that button [RPC].

Give it a name like sayHello

Then click the [+] and set a string in the new slot. The name of this slot doesn't matter at all but you should name it something that will remind you what it's for later.

After this SAVE & COMPILE

Go back to your script for (superAwesomePlayer) you'll see the name of the script has an error. Click on the name of the script inside of the script to the left of the superAwesomePlayerBehavior and then press| CTRL + . |at the same time. Then click the extract method option presented and it will give you a new method.

After this inside of method you can write Debug.Log(args.GetNext()); which will be called when the RPC is sent.

Now inside of Update do the following string hello = "Hello World"); networkObject.SendRPC(RPC_HELLO,Recievers.All,hello);

Run the app and you'll see Hello World being printed a bunch of times.

I am lost, Where do I go for help?

https://discord.gg/h2HYkye and you can @!LordBailey or @NFMynster

Home

Getting Started
Network Contract Wizard (NCW)
Network Object
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
NetWorker
Master Server
Web Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
Forge Networking Alloy
Steamworks
Clone this wiki locally