Lumina Posted July 16, 2017 Share Posted July 16, 2017 Hi again I want to spawn chest in the world when doing an action (digging something), and i want chest to contain some items, like treasures chest in shipwrecked. But i don't manage to understand what part of the SW code is useful for what i want and what part is here for others things. Code of SW code relevant as far as i understand : https://pastebin.com/U3cyWLyd https://pastebin.com/RZq4awue I inst.SetRandomTreasure = function(inst) inst:Reveal() local treasures = GetTreasureLootDefinitionTable() local treasure = GetRandomKey(treasures) inst.loot = treasure end This part seems to be about what i want (choosing one of the chest in the list of the second file. But i'm not sure of what i will need else. Also, maybe it's better to use another way ? I'm not sure scenario could work in this case.. Thanks for any help. Link to comment Share on other sites More sharing options...
alainmcd Posted July 16, 2017 Share Posted July 16, 2017 The SW code looks very convoluted. Try this instead: chest = SpawnPrefab("treasurechest") chest.components.container:GiveItem(SpawnPrefab("rocks")) From the dospawnchest function in data/scripts/prefabs/minotaur.lua. Link to comment Share on other sites More sharing options...
Lumina Posted July 16, 2017 Author Share Posted July 16, 2017 Ok. And if i want to do pack of loot, like, for example, one chest will give armor+spear, one will give golden axe and golden shovel, another will give 10 manures and a pitchfork, and i choose one randomly ? I know how to choose between a table a random drop now, but not how to choose a random "pack". Link to comment Share on other sites More sharing options...
alainmcd Posted July 16, 2017 Share Posted July 16, 2017 (edited) local loots = { { {item = "spear"}, {item = "armorwood"} }, { {item = "manure", count = 10}, {item = "pitchfork"} } } local chest = SpawnPrefab("treasurechest") local loot = loots[math.random(#loots)] for _, items in pairs(loot) do for _, v in pairs(items) do local item = SpawnPrefab(v.item) if v.count and item.components.stackable then item.components.stackable:SetStackSize(v.count) end chest.components.container:GiveItem(item) end end 100% untested. The chest1 and chest2 are unnecessary in this example, mostly just for clarity. You could give them fancier names to tweak them more easily or to set chance percentages. Or, or, or... Edited July 16, 2017 by alainmcd Link to comment Share on other sites More sharing options...
Lumina Posted July 16, 2017 Author Share Posted July 16, 2017 Thanks a lot, i will test it Link to comment Share on other sites More sharing options...
Lumina Posted July 16, 2017 Author Share Posted July 16, 2017 For the moment i obtain an error about this line : local loot = loots(math.random(#loots)) bad argument #1 to 'random' (interval is empty) My attemps to fix it didn't work. Link to comment Share on other sites More sharing options...
alainmcd Posted July 16, 2017 Share Posted July 16, 2017 (edited) Ah, yeah, you can't name the "chests" or the length operator will complain. Simple solution: remove "chest1 =" and "chest2 =". I've updated the example above and corrected a mistake in the same line you mentioned (loots is a table, not a function). If you do want to use names, you can try something like... Spoiler local loots = { chest1 = { {item = "spear"}, {item = "armorwood"} }, chest2 = { {item = "manure", count = 10}, {item = "pitchfork"} } } local chestnames = {} for k, _ in pairs(loots) do table.insert(chestnames, k) end local chest = SpawnPrefab("treasurechest") local loot = loots[chestnames[math.random(#chestnames)]] for _, items in pairs(loot) do for _, v in pairs(items) do local item = SpawnPrefab(v.item) if v.count and item.components.stackable then item.components.stackable:SetStackSize(v.count) end chest.components.container:GiveItem(item) end end Again, untested. Edited July 16, 2017 by alainmcd Link to comment Share on other sites More sharing options...
Lumina Posted July 16, 2017 Author Share Posted July 16, 2017 Thanks again, i will test your solutions Link to comment Share on other sites More sharing options...
Lumina Posted July 16, 2017 Author Share Posted July 16, 2017 Ok, for the moment it seems to work fine. I just had to change one line : local item = SpawnPrefab(v.item) in local item = SpawnPrefab(items.item) (Why, don't ask me, but i got an error without that, and it seems to follow the logic in minotaur.lua). Link to comment Share on other sites More sharing options...
alainmcd Posted July 16, 2017 Share Posted July 16, 2017 Oops. Copy-pasting without checking. You don't need the second for-loop at all, then. Change the last block of code to: for _, items in pairs(loot) do local item = SpawnPrefab(items.item) if items.count and item.components.stackable then item.components.stackable:SetStackSize(items.count) end chest.components.container:GiveItem(item) end Link to comment Share on other sites More sharing options...
Lumina Posted July 16, 2017 Author Share Posted July 16, 2017 (edited) I changed a few things, using minotaur.lua as template : local loot = loots[math.random(#loots)] for _, items in pairs(loot) do local item = SpawnPrefab(items.item) if item ~= nil then if type(items.count) == "table" and item.components.stackable ~= nil then item.components.stackable:SetStackSize(math.random(items.count[1], items.count[2])) elseif items.count and item.components.stackable then item.components.stackable:SetStackSize(items.count) end end chest.components.container:GiveItem(item) end If i understand well, this part allows to have random stacks : if type(items.count) == "table" and item.components.stackable ~= nil then item.components.stackable:SetStackSize(math.random(items.count[1], items.count[2])) And your part allows to take "count" in account for fixed amount elseif items.count and item.components.stackable then item.components.stackable:SetStackSize(items.count) end { {item = "poop", count = 10}, {item = "pitchfork"}, {item = "gears", count = {3, 6}} }, (example loot with fixed amount and random amount) I tested some differents cases, so the basics are fine. Again, thanks Edited July 16, 2017 by Lumina Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now