Jump to content

Add Item to Starting Inventory


Recommended Posts

Basically, I am just trying to add an item of my own creation to the starting inventories of a set of characters. I've verified my item exists by spawning it with the console. I have tried adding it using AddPrefabPostInit but I haven't figured out what to put in the function itself to add the item. If anyone can advise me on how to do this I would appreciate it, as my searches turned up nothing useful. Thanks.

 

Link to comment
Share on other sites

Here's how I give starting items to new players:

local function playerSpawner(PlayerSpawner, inst)
    inst:ListenForEvent("ms_newplayercharacterspawned", function(a, b)
		b.player.components.inventory:GiveItem(GLOBAL.SpawnPrefab("name_of_your_item"))
	end)
end

AddComponentPostInit("playerspawner", playerSpawner)

If you only want a certain character to get the item, I think you can get the player character's name with b.player.prefab.

Link to comment
Share on other sites

Hey, sorry it took so long for me to reply, been sick (and I forgot I posted this >.>).

I actually got this working on my own, turned out I had a simple problem and the method I was using was fine once I fixed it (in my case the issue was one of mod priorities). Thanks for the reply though!

Link to comment
Share on other sites

Okay, it turns out my mod crashes the game if it is run on a server that is using caves.

The offending line is simply this: inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab("mpower"))

The error returned (if caves were generated) is "attempt to index field 'inventory' (a nil value)"

I even tried changing the line to this: inst:DoTaskInTime(5, function(inst)    inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab("mpower")) end)

In this case the player spawns, after 5 seconds the item appears in their inventory. And then the game crashes with the same error. So... I have no idea what is going on? The inventory is obviously there. Any help would be appreciated.

Link to comment
Share on other sites

29 minutes ago, meltorefas said:

Okay, it turns out my mod crashes the game if it is run on a server that is using caves.

The offending line is simply this: inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab("mpower"))

The error returned (if caves were generated) is "attempt to index field 'inventory' (a nil value)"

I even tried changing the line to this: inst:DoTaskInTime(5, function(inst)    inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab("mpower")) end)

In this case the player spawns, after 5 seconds the item appears in their inventory. And then the game crashes with the same error. So... I have no idea what is going on? The inventory is obviously there. Any help would be appreciated.

Check if the player referenced has the inventory.

if(player.components.inventory) then <add item> end

Link to comment
Share on other sites

22 hours ago, CarlZalph said:

Check if the player referenced has the inventory.

if(player.components.inventory) then <add item> end

Yeah, I tried that, at which point it no longer gives them the item, but again only if Caves were generated. The thing is, other items from their starting inventory appear just fine, and they can pick stuff up. Their inventory is obviously *there*, but for some reason when running with Caves generated it just acts like it isn't (at least for this one script in modmain).

Link to comment
Share on other sites

I... pretty much already did, above? But if you really want:

local function add_mpower (inst)
	inst:ListenForEvent("itemlose", OnDropCheckMPower)
	inst:DoTaskInTime(5, function(inst)	inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab("mpower")) end)
end

As I said, it works fine in a forest world, but if I include caves it crashes AFTER the item appears in the inventory, claiming that 'inventory' is a nil value.

Just to be absolutely clear: The second line of the function is the one returning the error, because it references inst.components.inventory. I have no idea what causes it to fail when caves are included in worldgen.

Edited by meltorefas
Clarification
Link to comment
Share on other sites

6 hours ago, meltorefas said:

As I said, it works fine in a forest world, but if I include caves it crashes AFTER the item appears in the inventory, claiming that 'inventory' is a nil value.

Just to be absolutely clear: The second line of the function is the one returning the error, because it references inst.components.inventory. I have no idea what causes it to fail when caves are included in worldgen.

Because you are most likely using

AddPlayerPostInit(add_mpower)

and you either have "all_clients_require_mod = true" in modinfo (so clients load it), or for some reason server side mods get enabled client side (like when you host a server with caves, from the game client).

When you host a server without caves, you become the server and play inside it. When you host a server with caves, your game opens two dedicated servers, and makes you join one like in a LAN, so you stay out of the server and you send it inputs.

Therefore, the inventory check will be inevitably done client side, where the inventory component doesn't exist. Therefore, crash.

Use

local function add_mpower(inst)
	if inst.components.inventory then
		inst:ListenForEvent("itemlose", OnDropCheckMPower)
		inst:DoTaskInTime(5, function(inst) inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab("mpower")) end)
	end
end

 

Link to comment
Share on other sites

On 7/20/2016 at 11:08 AM, DarkXero said:

Because you are most likely using


AddPlayerPostInit(add_mpower)

and you either have "all_clients_require_mod = true" in modinfo (so clients load it), or for some reason server side mods get enabled client side (like when you host a server with caves, from the game client).

When you host a server without caves, you become the server and play inside it. When you host a server with caves, your game opens two dedicated servers, and makes you join one like in a LAN, so you stay out of the server and you send it inputs.

Therefore, the inventory check will be inevitably done client side, where the inventory component doesn't exist. Therefore, crash.

Use


local function add_mpower(inst)
	if inst.components.inventory then
		inst:ListenForEvent("itemlose", OnDropCheckMPower)
		inst:DoTaskInTime(5, function(inst) inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab("mpower")) end)
	end
end

 

I am using AddPrefabPostInit, but otherwise yes. Your explanation makes sense, I understand the error now.

Adding an If statement was the first thing I tried when I encountered the problem, and all it did was prevent the item from being added (which, while it did prevent the crash, also failed to accomplish the mod's purpose). However, I have changed things since then, so I tried the If statement again just now and the it worked fine. So I guess something else was causing the error the first time I tried it, which threw me off. At any rate, thanks for the explanation, and for the incentive to try it again.

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...