Jump to content

What Does This Code Do? And a LUA Error


Recommended Posts

I have seen this in a few pieces of code now, and I'm curious as to what it does.

 



        if not TheWorld.ismastersim then
            return inst
        end


I've been testing some items I have in game, some of them don't have this piece in there.  When I try to spawn an item on someone elses' server (I know I'm not supposed to be able to) I get a lua error, other normal items don't throw such an error.

 

I'm wondering if the above code is meant to prevent that

 

Error in a pic below, heading to class or I'd write it out more clearly.

 

post-626173-0-95261300-1429710770_thumb.

 

If no responses when I get back I'll rewrite it out instead of attaching a file.

 

Thanks guys.

Link to comment
Share on other sites

That code basically is saying: "from here on, server side only code".

 

You can either be a host or a client.

 

If you are the host, your world will be the master simulation, everything that happens there is what is actually happening.

If you are a client, your world will be a copy of the simulation.

 

If you are the host, and an entity is created in your world, it will be fully created, with components and all, and the clients will be told to spawn one too, and to attach replicas instead of components, to have some functionality.

 

If you are a client, and you spawn an entity in your world (when you tried), THAT code you see avoids crashing. With that code, you would get an entity that is initialized in the C side, and is halfway networked because there is no such entity on the server. So you would see something, but couldn't interact with it in any meaningful way. Nobody else in the server, host or other clients, would be able to interact with it.

 

What happened on the crash? Your game tried to run the onowner function, because you had the component attached to the item. However, because it was client side, the component didn't have any replicas, so when it tried to call the SetOwner function from the inventoryitem replica, it crashed, because there was none. If you attach the component, and not the server, then you will have to attach a replica too, because the game won't do it for you. :-)

 

If you have something that runs server side AND client side, such as a widget, then you don't need that code.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...