Jump to content

How to create items inside containers and keep them synced?


Recommended Posts

I'm trying to mess with a new container in a crockpot-like prefab, and one of the important steps is generating the recipe outcome, which I have to do from the inside of the code (I need the flexibility of outputting multiple products at once)

I tried using the lootdropper component but it didn't seem to drop the product in the right place.

I have tried to use the GiveItem method but it crashed last time (I don't know if it's related to not having a src_pos argument or if I just set it wrong, but it executed and then crashed on the container definition file, so I know I sent something wrong)

I then tried to replicate the relevant code, and trimmed it until it ran. It works fine in the master, but the container doesn't sync anymore in the client because I had to delete all PushEvent calls (they also crashed from nil indexing, just as the above remark, possibly because I failed to initialize the prefab I'm spawing correctly)

Finally, coming back to debug it, I discover that it doesn't sync and now I'm in need of a different approach. So, how do I create items inside containers and then keep them synced? Do I need to initialize the item correctly? Do I need a different function? Or I should just use GiveItem and maybe I did something else wrong?

Link to comment
Share on other sites

Okay, I took a look at the code, traced the calls and it seems that the problem is the lack of a player to give a PushEvent command to.

How can I refer to a player in the prefab context? Should I just pass the player when the action starts?

Link to comment
Share on other sites

The missing call and the crash problem is that the sync happens via a pair of Push/ListenForEvent with the event "itemget".

Event methods are bound to a player entity (the container code does self.inst.components.inventoryitem.owner just to call this)

If I want code to generate an item by myself, there is no player to access and therefore there is no valid entity on which to call PushEvent.

How do I fix this?

Edited by AndRay
Link to comment
Share on other sites

EDIT: I read the wrong line and call; and the right call is

self.inst:PushEvent("itemget", { slot = in_slot, item = item, src_pos = src_pos })

as called by the container method (so self is the container itself)

I call this not from the container component, because god no I don't ever want to hard code a copy of a container this way, but from the "stewer" component.

So inst down here means the object itself.

inst:PushEvent crashes because the method is nil. But inst.components.container:PushEvent and inst.components.container.inst:PushEvent crash in the game's container files (container_replica and container_classified which are not in the same folder) with the following error message:

calling 'set' on bad self (table expected, got string)

Also, for completeness, inst.components.container:GiveItem crashes on container.lua due to the code attempting to index my passed item for its components (which do not exist)

Edited by AndRay
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...