Lumina Posted July 12, 2017 Share Posted July 12, 2017 Hi, I want to add a chance to obtain another loot if a character dig a grave. What is the better way, code-wise, to do it ? As far as i know there are two way : Adding something with prefabpostinit to the grave itself, and check if the character digging has a specific tag, if yes, chance to have another loot. Or Adding something to the character prefab, when doing the action, checking the prefab of the item, and if it's a grave, have a chance to have another loot. The first seems better because of possible compatibility with possible others character with the same tag (or item adding the tag, for example). What do you think ? Do i miss something ? Thanks Link to comment Share on other sites More sharing options...
alainmcd Posted July 12, 2017 Share Posted July 12, 2017 The first option sounds simple and natural, whereas the second... where would you even begin? I don't see any benefit to it. Besides, who (or what) is performing the action? That's where you should find the code. Link to comment Share on other sites More sharing options...
Lumina Posted July 12, 2017 Author Share Posted July 12, 2017 30 minutes ago, alainmcd said: The first option sounds simple and natural, whereas the second... where would you even begin? I don't see any benefit to it. I don't know, it's why i'm asking 30 minutes ago, alainmcd said: Besides, who (or what) is performing the action? That's where you should find the code. The character digging the grave i guess ? Link to comment Share on other sites More sharing options...
JohnWatson Posted July 12, 2017 Share Posted July 12, 2017 (edited) AddPrefabPostInit("mound", function(inst) local ic.workable = inst.components.workable if ic.workable~=nil then local _onfinishcallback_Old = ic.workable.onfinishcallback ic.workable.onfinishcallback = function(inst, worker) if worker:HasTag("taghere") then local lootprefab = {"cutgrass","twigs","rocks","log"} ic.lootdropper:SpawnLootPrefab(lootprefab[math.random(1,#lootprefab)]) end return _onfinishcallback_Old(inst, worker) end end end) Edited July 12, 2017 by JohnWatson asdf Link to comment Share on other sites More sharing options...
alainmcd Posted July 12, 2017 Share Posted July 12, 2017 42 minutes ago, Lumina said: I don't know, it's why i'm asking 42 minutes ago, Lumina said: The character digging the grave i guess ? From the player's perspective, yes, the character is digging the grave, but from a game logic perspective, the grave is the one dropping the loot. If the grave drops loot, it's not intuitive for that code to be in a character's prefab file. 10 minutes ago, JohnWatson said: local ic.workable = inst.components.workable local ic = inst.components 10 minutes ago, JohnWatson said: return _onfinishcallback_Old(inst, worker) return _onfinishedcallback_Old and _onfinishedcallback_Old(inst, worker) Link to comment Share on other sites More sharing options...
JohnWatson Posted July 12, 2017 Share Posted July 12, 2017 (edited) @alainmcd Woops, bad mistakes. Here: AddPrefabPostInit("mound", function(inst) local ic = inst.components if ic.workable~=nil then local _onfinishcallback_Old = ic.workable.onfinishcallback ic.workable.onfinishcallback = function(inst, worker) if worker:HasTag("taghere") then local lootprefab = {"cutgrass","twigs","rocks","log"} ic.lootdropper:SpawnLootPrefab(lootprefab[math.random(1,#lootprefab)]) end return _onfinishcallback_Old and _onfinishcallback_Old(inst, worker) end end end) Edited July 12, 2017 by JohnWatson Link to comment Share on other sites More sharing options...
Lumina Posted July 12, 2017 Author Share Posted July 12, 2017 Thanks both of you for your answers and your help. Link to comment Share on other sites More sharing options...
Lumina Posted July 13, 2017 Author Share Posted July 13, 2017 I tried it but can't manage to make it work. I put string but nothing appears in log, so i'm a little clueless about how to make it work. Link to comment Share on other sites More sharing options...
alainmcd Posted July 13, 2017 Share Posted July 13, 2017 Add print's all around! (because I can't see anything wrong with the code) AddPrefabPostInit("mound", function(inst) local ic = inst.components if ic.workable~=nil then local _onfinishcallback_Old = ic.workable.onfinishcallback ic.workable.onfinishcallback = function(inst, worker) if worker:HasTag("taghere") then local lootprefab = {"cutgrass","twigs","rocks","log"} ic.lootdropper:SpawnLootPrefab(lootprefab[math.random(1,#lootprefab)]) print("extra loot") -- so you know if the loot isn't spawning for some reason else print("nope") -- did you forget to add the tag? or are you trying it with the wrong character? end return _onfinishcallback_Old and _onfinishcallback_Old(inst, worker) end print("mound loaded") -- it's supposed to work, right? end end) Link to comment Share on other sites More sharing options...
Lumina Posted July 13, 2017 Author Share Posted July 13, 2017 My test : AddPrefabPostInit("mound", function(inst) local ic = inst.components if ic.workable~=nil then print("step 1") -- verifying if something is wrong here local _onfinishcallback_Old = ic.workable.onfinishcallback ic.workable.onfinishcallback = function(inst, worker) print("step 2") -- onfinishcallback working ? if worker:HasTag("taghere") then local lootprefab = {"cutgrass","twigs","rocks","log"} ic.lootdropper:SpawnLootPrefab(lootprefab[math.random(1,#lootprefab)]) print("extra loot") -- so you know if the loot isn't spawning for some reason else print("nope") -- did you forget to add the tag? or are you trying it with the wrong character? end return _onfinishcallback_Old and _onfinishcallback_Old(inst, worker) end print("mound loaded") -- it's supposed to work, right? end end) Log : Quote [00:07:58]: step 1 [00:07:58]: mound loaded [00:08:16]: step 1 [00:08:16]: mound loaded [00:08:25]: step 1 [00:08:25]: mound loaded Note : theses prints are created when i spawn a mound, i don't know if it's normal. So there are none of the others prints in the log, neither "step 2" or the two you put for testing. So as far as i can tell the problem happens after this part ? local _onfinishcallback_Old = ic.workable.onfinishcallback ic.workable.onfinishcallback = function(inst, worker) Link to comment Share on other sites More sharing options...
alainmcd Posted July 13, 2017 Share Posted July 13, 2017 No, so far it's working fine. You should see "step 2 " and either "extra loot" or "nope" when you dig up the grave, when onfinishcallback is called. Link to comment Share on other sites More sharing options...
Lumina Posted July 13, 2017 Author Share Posted July 13, 2017 1 minute ago, alainmcd said: No, so far it's working fine. You should see "step 2 " and either "extra loot" or "nope" when you dig up the grave, when onfinishcallback is called. Sorry, i didn't mention i dug the graves, but nothing is showing after that. So i spawn the grave, there are the lines : step 1 mound loaded I dig the grave nothing Which is strange because i don't see why it's not working... Link to comment Share on other sites More sharing options...
alainmcd Posted July 13, 2017 Share Posted July 13, 2017 ...because it's not onfinishcallback, but onfinish. Link to comment Share on other sites More sharing options...
Lumina Posted July 13, 2017 Author Share Posted July 13, 2017 24 minutes ago, alainmcd said: ...because it's not onfinishcallback, but onfinish. I can confirm it's working now. Thanks again for taking the time to search for the error, even if the fact that i wasn't clear didn't help. I must admit i'm curious, so if you have a little more time, could you explain why i should use onfinish when in the mound prefab, it seems to use onfinishcallback ? It's not for the same thing ? Also, how can i change the random part ? I tried to change it to ic.lootdropper:SpawnLootPrefab(lootprefab[math.random(1,10,#lootprefab)]) It gave me an error. I tried this too : ic.lootdropper:SpawnLootPrefab(lootprefab[math.random(0.5,#lootprefab)]) But don't see a difference. You already spent a long time to help me so i would not mind if you don't do more, it's already very helpful Link to comment Share on other sites More sharing options...
alainmcd Posted July 13, 2017 Share Posted July 13, 2017 onfinishcallback isn't referenced anywhere in the code, Workable:WorkedBy only calls onfinish (if it exists and if there's no work left). -- math.random takes zero, one or two arguments: If it's called with no arguments, it will return a number between 0 and 1 (and less than 1). So something like "if math.random() < 0.3 then ..." roughly means "do this 30% of the time". If it's called with a single argument, it will return an integer between 1 and that argument. For example, math.random(6) is practically the same as throwing a regular six-sided die. If it's called with two arguments, it will return an integer between those two numbers. math.random(1, #lootprefab) is the same as math.random(#lootprefab). All arguments should be integers. My understanding is that DS/DST are based on Lua 5.1 which didn't specify how to handle non-integers. And yes, calling math.random with more than two arguments produces an error. I'm not sure what you were trying there. Link to comment Share on other sites More sharing options...
Lumina Posted July 13, 2017 Author Share Posted July 13, 2017 Oh, ok i think i understand. In short, i was trying to make the additional treasure only spawn one on tenth times, but if i understand it well, i must create another test for this, not include it in this function. Link to comment Share on other sites More sharing options...
alainmcd Posted July 13, 2017 Share Posted July 13, 2017 ic.lootdropper:SpawnLootPrefab(lootprefab[math.random(10*#lootprefab)]) Link to comment Share on other sites More sharing options...
Lumina Posted July 13, 2017 Author Share Posted July 13, 2017 Nice, thanks again What this forum would do without you, i wonder. Link to comment Share on other sites More sharing options...
alainmcd Posted July 13, 2017 Share Posted July 13, 2017 Nothing. Klei would just pull the plug. 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