Jump to content

Recommended Posts

  • Developer

Hello,

Why are you trying to write a mod for that ? Do you wanna learn how to mods, or you just need this mod ? ^^

If you just need a mod for Starting Inventory, see Start Item

 

If you want to write your own mod, yes all Mod code must be in Modmain.lua, the modinfo file is for "info" about the mod :p

I guess I didn't ask correctly.

Ok I'm learning to write a mod.

I want it to be able to have "config" button. This I put in modinfo.lua.

Inside config, let's say I want "Starting Inv"

Option 1 = "None"

Option 2 = "Sword only"

Option 3 = "A lot!"

 

Now when user selects option 1, starting inv would be empty.

For option 2 --> starts with a sword (I made one, let's call it "mysword")

For option 3 --> starts with a sword, flint, and so on

 

Is this possible?

Thanks again.

@SenL
As far as I know yes, GetModConfigData() only works in modmain.

If you want to have a configurable starting inventory you can modify the OnNewSpawn function of, let's say, Wilson, and give him his new starting inventory there, like so:
 

local starting_inventory ={}local inv_option = 1 --GetModConfigData("inv_option")local prefabs = {}if inv_option == 1 then    starting_inventory = {"carrot","carrot","carrot"}else    starting_inventory = {"berries","berries","berries"}endif starting_inventory ~= nil then    for i, v in ipairs(starting_inventory) do        table.insert(prefabs, v)    endendAddPrefabPostInit("wilson", function(inst)    if inst.OnNewSpawn then        inst.old_OnNewSpawn = inst.OnNewSpawn    end        inst.OnNewSpawn = function(inst)        if inst.components.inventory ~= nil then            inst.components.inventory.ignoresound = true            for i, v in ipairs(starting_inventory) do                inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab(v))            end            inst.components.inventory.ignoresound = false        end        if inst.old_OnNewSpawn then            return inst:old_OnNewSpawn(inst)        end    endend)

@SenL, while Ryuushu's method is good it is not as efficient as you can make it.

 

The most efficient way to do this is by doing the following:

 

modmain.lua

local starting_inventory = GetModConfigData("inv_start")local function StartInv(inst)	if inst.OnNewSpawn then        inst.old_OnNewSpawn = inst.OnNewSpawn    end         inst.OnNewSpawn = function(inst)        if inst.components.inventory ~= nil then            inst.components.inventory.ignoresound = true            for i, v in ipairs(starting_inventory.split(starting_inventory, ',')) do                inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab(v))            end            inst.components.inventory.ignoresound = false        end        if inst.old_OnNewSpawn then            return inst:old_OnNewSpawn(inst)        end    end    return instendfor _, v in ipairs(CHARACTERLIST) do	AddPrefabPostInit(v, StartInv)end 

  

 

modinfo.lua

configuration_options = {	{	    name = "inv_start",	    label = "Startng Inventory",	    options = 	    {	        {description = "None", data = ""},	        {description = "Flint", data = "flint,flint"},            },            default = "",        },} 

 

Simply put the prefab name separated by a comma and the code in the modmain.lua file will do the rest.

 

~Kzisor/Ysovuka

Wow amazing. How did you guys come up with this... I'm looking at the code and couldn't understand some of them.

1) What are (or how do these work) "inst.OnNewSpawn" and "inst.old_OnNewSpawn" work?

 

2) In "for _, v in ipairs(CHARACTERLIST) do" ...

2a) what does _ mean?

2b) what is CHARACTERLIST?

 

Thanks again, I'll try yours Kzisor.

Wow amazing. How did you guys come up with this... I'm looking at the code and couldn't understand some of them.

1) What are (or how do these work) "inst.OnNewSpawn" and "inst.old_OnNewSpawn" work?

 

2) In "for _, v in ipairs(CHARACTERLIST) do" ...

2a) what does _ mean?

2b) what is CHARACTERLIST?

 

Thanks again, I'll try yours Kzisor.

 

1) The code provided will only work on newly generated servers, upon initial login to the server.

2a) _ is a dummy variable which is not meant to be used; it's the key variable from the ipairs (key, value)

2b) CHARACTERLIST is the variable for the default characters. This will add the starting inventory to all the default characters.

 

See blow on how to make it only work for specific characters or for all characters.

 

Hm something's missing from my code. It's not working. Maybe missing CHARACTERLIST?

 

If you're trying this on a specific character, use the following line.

 

AddPrefabPostInit("ENTER CHARACTER PREFAB NAME HERE", StartInv) 

 

To make this work for all mod characters use the following:

 

for _,v in ipairs(MODCHARACTERLIST) do    AddPrefabPostInit(v, StartInv)end 

 

If you want to make it work for all characters period, use both for-do loops which includes CHARACTERLIST and MODCHARACTERLIST and anytime a character is spawned in the world it will be given the starting inventory.

 

EDIT:

In order to make sure this works properly you must generate a new world every time you make changes to the code.

Edited by Kzisor

 

I got crash error "bad argument #1 to 'ipairs' (table expected, got nil)"

 

Code I have:

for _, v in ipairs(MODCHARACTERLIST) do
    AddPrefabPostInit(v, StartInv)
end 

 

 

Did you put the configuration options in your modinfo.lua? Did you also set it in the configuration menu?

Yes.

 

Modinfo:

configuration_options ={	{		name="BK_StartingInventory"		,label="Starting Equip"		,options = {			{data="bksword,bkgoblinsack",description="Default"}			,{data="bksword,bkgoblinsack,flint",description="Basic"}			,{data="bksword,bkgoblinsack,flint,torch,trap,goldnugget,twigs,cutgrass,dragonfruit_cooked",description="More!"}		}		,default="Basic"	},}

Modmain:

local starting_inventory = GetModConfigData("BK_StartingInventory") local function StartInv(inst)    if inst.OnNewSpawn then        inst.old_OnNewSpawn = inst.OnNewSpawn    end          inst.OnNewSpawn = function(inst)        if inst.components.inventory ~= nil then            inst.components.inventory.ignoresound = true            for i, v in ipairs(starting_inventory.split(starting_inventory, ',')) do                inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab(v))            end            inst.components.inventory.ignoresound = false        end        if inst.old_OnNewSpawn then            return inst:old_OnNewSpawn(inst)        end    end     return instend for _, v in ipairs(MODCHARACTERLIST) do	AddPrefabPostInit(v, StartInv)end 

@SenL

@Kzisor

Instead using CHARACTERLIST and MODCHARACTERLIST you should be using AddPlayerPostInit, the mod loading order affects MODCHARACTERLIST.

modmain.lua

local starting_inventory = GetModConfigData("inv_start")AddPrefabPostInit("wilson", function(inst) -- AddPlayerPostInit(function(inst) if you want to add it to all characters    if inst.OnNewSpawn then        inst.old_OnNewSpawn = inst.OnNewSpawn    end        inst.OnNewSpawn = function(inst)        if inst.components.inventory ~= nil then            inst.components.inventory.ignoresound = true            for i, v in ipairs(starting_inventory.split(starting_inventory,",")) do                inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab(v))            end            inst.components.inventory.ignoresound = false        end        if inst.old_OnNewSpawn then            return inst:old_OnNewSpawn(inst)        end    endend)
modinfo.lua

configuration_options ={    {        name = "inv_start",        label = "Starting inventory",        options =        {            {description = "Carrots", data = "carrot,carrot,carrot"},            {description = "Berries", data = "berries,berries,berries"},        },        default = "carrot,carrot,carrot"    }}
I'll stick to tables rather than string splitting >>;

Edit: Tables don't work, split does. Code modified to reflect the change.

Edited by Ryuushu
*snip*

 

Your modinfo.lua is not correct. Syntax is important in all programming languages.

 

Try this in your modinfo.lua file:

configuration_options ={	{		name="BK_StartingInventory",label="Starting Equip",options = {			{data="bksword,bkgoblinsack",description="Default"},			{data="bksword,bkgoblinsack,flint",description="Basic"},			{data="bksword,bkgoblinsack,flint,torch,trap,goldnugget,twigs,cutgrass,dragonfruit_cooked",description="More!"},		},		default="bksword,bkgoblinsack",	},}

Note: Default is the actual default value, not the default description you want it to have.

 

@Ryuushu, tables did not save properly whenever I tested using them, otherwise I would have stuck with the simpler approach. Did you test this before posting it or is this just theory code?

 

On a secondary note, I had no issue with split working without requiring util.

Edited by Kzisor

I used Ryuushu's. It works for me.

I'm now trying to use your's. Changed modinfo to 

configuration_options ={    {        name="BK_StartingInventory"        ,label="Starting Equip"        ,options = {            {description="Default",data="bksword,bkgoblinsack"}            ,{description="Basic",data="bksword,bkgoblinsack,flint"}            ,{description="More!",data="bksword,bkgoblinsack,flint,torch,trap,goldnugget,twigs,cutgrass,dragonfruit_cooked"}        }        ,default="bksword,bkgoblinsack"    },}

but it crashes at load. Same error "bad argument #1 to 'ipairs' (table expected, got nil)".

 

Here is more to modmain:

AddMinimapAtlas("images/map_icons/barbarianking.xml")AddModCharacter("mycharacterbk")AddPrefabPostInit("bksword")AddPrefabPostInit("bkgoblinsack")--AddPrefabPostInit("bkpekkapiece")local starting_inventory = GetModConfigData("BK_StartingInventory")local function StartInv(inst)    if inst.OnNewSpawn then        inst.old_OnNewSpawn = inst.OnNewSpawn    end           inst.OnNewSpawn = function(inst)        if inst.components.inventory ~= nil then            inst.components.inventory.ignoresound = true            for i, v in ipairs(starting_inventory.split(starting_inventory, ',')) do                inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab(v))            end            inst.components.inventory.ignoresound = false        end        if inst.old_OnNewSpawn then            return inst:old_OnNewSpawn(inst)        end    end      return instend  for _, v in ipairs(MODCHARACTERLIST) do    AddPrefabPostInit(v, StartInv)end 

Changed the comma placement in modinfo... not sure why it requires to be at the end.

configuration_options ={    {        name="BK_StartingInventory",        label="Starting Equip",        options = {            {description="Default",data="bksword,bkgoblinsack"},            {description="Basic",data="bksword,bkgoblinsack,flint"},            {description="More!",data="bksword,bkgoblinsack,flint,torch,trap,goldnugget,twigs,cutgrass,dragonfruit_cooked"},        },        default="bksword,bkgoblinsack",    },}

Testing now...

@SenL, that is really strange that MODCHARACTERLIST isn't working for you. You can always just use the AddPrefabPostInit or the AddPlayerPostInit method that Ryuushu gave. Either way, if Ryuushu's code works and saves the selection properly that is all that matters. I will need to do more testing to see if I can figure out why it doesn't work on my end.

Curses, I broke it lol.

I went back to Ryuushu's style and it crashes on "for i, v in ipairs(starting_inventory) do"

 

Modmain:

 

local starting_inventory = GetModConfigData("BK_StartingInventory")AddPrefabPostInit("mycharacterbk", function(inst)if inst.OnNewSpawn then        inst.old_OnNewSpawn = inst.OnNewSpawn    endinst.OnNewSpawn = function(inst)        if inst.components.inventory ~= nil then            inst.components.inventory.ignoresound = true            for i, v in ipairs(starting_inventory) do -----------------crashes here                inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab(v))            end            inst.components.inventory.ignoresound = false        end        if inst.old_OnNewSpawn then            return inst:old_OnNewSpawn(inst)        end    endend)

Modinfo:

configuration_options ={	{		name="BK_StartingInventory",		label="Starting Equip",		options = {			{description="Default", data={"bksword","bkgoblinsack"}},			{description="Basic", data={"bksword","bkgoblinsack","flint"}},			{description="More!", data={"bksword","bkgoblinsack","flint","torch","trap","goldnugget","twigs","cutgrass","dragonfruit_cooked"}},		},		default={"bksword","bkgoblinsack"},	},}

 

@Kzisor,@SenL,  Aren't MODCHARACTERLIST, etc in the GLOBAL space? (e.g. GLOBAL.MODCHARACTERLIST).

 

Last I tried tables in mod config, they worked really strangely, so I'd avoid them. I think the issue is with saving them, but I'm not sure about that.

Yes I encountered issue with this when playing with friend (same character). So far I've commented them out and just use regular start_inv table in the mycharacter.lua

 

Side question: I added "backpack" in start_inv but it didn't give me backpack when I host a new server. Why?

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