Jump to content

Recommended Posts

I thought I knew how to do this, but I guess I don't.

I'm just trying to give my character an Item when the game runs. I put this in the character's .lua file under the local fn = function(inst)

inst.component.inventory:GiveItem(SpawnPrefab("spear"))

As soon as the world loads, the game just closes and kicks me back to desktop. No error report or anything.

 

I swear this is supposed to work, isn't it? Is this not how you spawn items? Am I doing something wrong?

Not exactly

 

local spear = SpawnPrefab("spear")

inst.components.inventory:GiveItem(spear) 

 

Would work. Because the first command is what spawns the item, and when it's called in the lines after that, it's just saying what to do with the entity. That may be a poor way of explaining it because I'm bad at werds.

 

But, to have a character start with an item, that line of code is rather redundant. 

 

Assuming you're using a template, you should see:

 

local start_inv = 
{
 
}
 
If not, adding it just at the top of the prefab - generally just under the asset list - will let you tell the character what item to start with.
 
In your case, it would look like:
 
local start_inv = 
{
         "spear"
}
 

 

Edited by Mr. Tiddles

 

Not exactly

 

local spear = SpawnPrefab("spear")

inst.components.inventory:GiveItem(spear) 

 

Would work. Because the first command is what spawns the item, and when it's called in the lines after that, it's just saying what to do with the entity. That may be a poor way of explaining it because I'm bad at werds.

 

But, to have a character start with an item, that line of code is rather redundant. 

 

 

 

OHHH, I see what I was doing now.

After trying out your code for a while and still getting crashes, I eventually realized why neither of them were working.

I was using "inst.component" instead of "inst.componentS"  (that tricky S)

so now my original code: "inst.components.inventory:GiveItem(SpawnPrefab("spear"))" works just fine with the added S!

 

But, uh, your version of the code still crashes the game when I apply the mod. No error log. I dunno why.

I guess I don't need it now though. 

But thanks for your help!

 

 

Oh, and it's not for a starting item. It's for something more complicated, I just needed to figure out why I couldn't spawn anything first d:

OHHH, I see what I was doing now.

After trying out your code for a while and still getting crashes, I eventually realized why neither of them were working.

I was using "inst.component" instead of "inst.componentS"  (that tricky S)

so now my original code: "inst.components.inventory:GiveItem(SpawnPrefab("spear"))" works just fine with the added S!

 

But, uh, your version of the code still crashes the game when I apply the mod. No error log. I dunno why.

I guess I don't need it now though. 

But thanks for your help!

 

 

Oh, and it's not for a starting item. It's for something more complicated, I just needed to figure out why I couldn't spawn anything first d:

 

It did? Actually, makes sense it would. I just assumed that wasn't a thing because I too missed the missing 's'.

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
×
  • Create New...