Network Methods

Hello everyone ! i am not too familiar with javascript and i have to say it’s giving me headaches to understand how I can achieve certain things compared to c#, and i would really appreciate a little more in depth documentation on certain use-cases especially about the networking part and how to access certain variables and method or expose them.
For instance using the networking class of needle engine, how do I access the local id ? How do I make a prefab, respond in a certain way, whether it represents the local instance or a remote instance? how do i expose variables to that prefab? say with a singleton ? ( e.g. if instance is local find object of type something and call method A )
How do I request ownership of a network object ? how do I iterate over all players in the room and eventually follow an instance in spectator mode ?
Thanks !

Original Post on Discord

by user 700879811085598831

Hi, did you have a chance to read the typescript and unity dev starter documentation?

  • For networking: your id is accessible via this.context.connection.connectionId
  • for ownership use the class Ownership model (you can look at the SyncedTransform class in typescript for example as a reference). Ill make a note to add more documentation about that
  • all players are users in this.context.connection, it has methods for that

Just in case you didnt see it here is the networking docs - but i’ll made sure we add more info to it this week so just ask all questions here, that will help to add it to the docs later Networking | Needle Engine Documentation

Thanks @marcel :cactus: for the answers. I did go through the documentation and I did do some digging into the .ts scripts but as I said earlier : it is me failing to understand the basic concepts in typescript and it bridging with c#. For instance : this.context.connection.connectionId is that static or do I have to import context ? like in c# if something is static I can access it anywhere, and if it isn’t I have to declare its namespace and/or assign it in the editor or find in through a GetComponent function or such. How do I do that in Typescript with needle engine?
I think I understand that we can expose in unity with c# certain function and variables like so

c#
public string @myString = “Hello World”;
public void myFunction(){}

Typescript
@serializable()
myString: string = “Hello World”;
myFunction() {
}

but how do I reference another typescipt ? or search objects for their typescript components ?

I think i need to be told how to think and approach a project when building on Needle Engine.
Is there any code-oriented needle engine tutorial you 'd recommend ?

by user 700879811085598831

Hello again @marcel :cactus: ! I just found your video https://youtu.be/uf5UK0bLHlY
it would be amazing if your could make some more such videos on simple concepts like dealing with inputs, subscribing to other typescript script’s events, accessing the DOM. etc … :pray:

by user 700879811085598831

Something that I have been thinking about and would rewuire for multiuser usage : how would I assign a role upon connection to a room ? like host and guests. ? can I do that by checking require room parameter box and then somehow include the role in the url ? what would be the correct approach ?

by user 700879811085598831

That is mostly the same in typescript. Like this.context refers to the context every script instance has which gives you access to a lot of the needle engine systems (e.g. networking). We have a context per instance because one web page can potentially have multiple needle engine instances running.
Static exists the same in typescript.
You can get components very similar to Unity with either this.gameObject.getComponent calls if you do it from a component (the docs have the full list) or via the static methods on the gameobject class as GameObject.getComponent

You can reference another component the same way by either using getComponent or findObjectOfType or by reference with e.g.

@serializable(MyComponentType)
myReference? : MyComponentType;

Here you see it with the animation component being referenced:

You can find all of the that in documentation for typescript and custom components. But i agree it would be good to add videos too! I’ll make a note. And great that you found that video and it helped you, thanks for the feedback :slightly_smiling_face: