Hi im sure its a simple mistake but I need your help to solve this. Im simply getting a list of items then if each item is in the blueprints data list, there's 30% chances to be dropped as a blueprint. Else just drop the item.
This codes works but I noticed that once it got the 30% chances, it just get all items that are in the bp data to drop as bp (when I just want that single item to have 30% chance). So it looks like that the 30% happens to get ALL items found in databp instead of just ONE item at a time.
Idea (what I want):
item 1: not in bpdata => drop normal
item2: in bpdata => 30% chances => fail, drop normal item
item3: in bpdata => 30% chances => success, drop as blueprint item
item4: not in bpdata => drop normal
item5: in bpdata => 30% chances => fail, drop normal item
etc.
This code (I need fix):
item 1: not in bpdata => drop normal
item2: in bpdata => 30% chances => success, drop as blueprint item
item3: not in bpdata => drop normal
item4: in bpdata => Forcing blueprint because was previously found success
item5: in bpdata => Forcing blueprint because was previously found success
etc.
Code:var items = new List<Item>(); ......skipping codes.... if (items != null) { foreach (var item in items) { //Blueprints add if (storedDrops.Blueprints.Contains(item.info.shortname)) { var itemid = item.info.itemid; var rand = new Random(); var number = rand.Next(100); if (number <= 30) // 30 = 30% chance { Item itembp = ItemManager.CreateByName("blueprintbase", 1); itembp.blueprintTarget = itemid; itembp.MoveToContainer(corpse.containers[0]); } else item.MoveToContainer(corpse.containers[0]); } else item.MoveToContainer(corpse.containers[0]); } }
Need code fix (random) foreach item to bp
Discussion in 'Rust Development' started by FireBurst, Jun 20, 2018.
-
Code:
foreach (var item in items) { Item blueprint; if (storedDrops.Blueprints.Contains(item.info.shortname) && (UnityEngine.Random.Range(0, 1) <= 0.3f)) { // I didn't check, but I believe by default the amount is 1. blueprint = ItemManager.CreateByName("blueprintbase"); blueprint.blueprintTarget = item.info.itemid; blueprint.MoveToContainer(corpse.containers[0]); continue; } item.MoveToContainer(corpse.containers[0]); }
Code:items.ForEach(x => x.MoveToContainer(corpse.containers[0]));
Last edited by a moderator: Jun 20, 2018 -
Hi thanks for your reply. I tried both
Code:foreach (var item in items) { Item blueprint; if (storedDrops.Blueprints.Contains(item.info.shortname) && (UnityEngine.Random.Range(0, 1) <= 0.3f)) { // I didn't check, but I believe by default the amount is 1. blueprint = ItemManager.CreateByName("blueprintbase"); blueprint.blueprintTarget = item.info.itemid; blueprint.MoveToContainer(corpse.containers[0]); continue; } item.MoveToContainer(corpse.containers[0]); }
Code:items.ForEach(x => { if (storedDrops.Blueprints.Contains(x.info.shortname) && (UnityEngine.Random.Range(0, 1) <= 0.3f)) { var itemid = x.info.itemid; Item itembp = ItemManager.CreateByName("blueprintbase", 1); itembp.blueprintTarget = itemid; itembp.MoveToContainer(corpse.containers[0]); } else x.MoveToContainer(corpse.containers[0]); });
-
OK that is very strange and I don't know why this minor change affect my solution.... As soon as I add a Puts(""); in each case to log and see what happens, the blueprint applies perfectly to each item (30% success or not). But when I don't add a Puts, it just overwrite all items found in data to a blueprint (global 30% chance instead of individual item chance).
Code:Item itembp; int count = items.Count; var maxRetry = 20; for (int i = 0; i < count; ++i) { if (maxRetry == 0) break; if (items[i] == null) { --maxRetry; --i; continue; } if (storedDrops.Blueprints.Contains(items[i].info.shortname)) { var itemid = items[i].info.itemid; var rand = new Random(); var number = rand.Next(100); if (number <= 30) // 30 = 50% chance { Puts("success"); itembp = ItemManager.CreateByName("blueprintbase", 1); itembp.blueprintTarget = itemid; itembp.MoveToContainer(corpse.containers[0]); } else { Puts("not success"); items[i].MoveToContainer(corpse.containers[0]); } } else { Puts("not in bp data"); items[i].MoveToContainer(corpse.containers[0]); } }
EDIT: I just tried to keep only 1 Puts (no need to keep the 3 ones), it still works. So when there's atleast one Puts, it works, if I take all away, it just screw things up
Is there something I can do to replace the Puts without spamming my console? to have the same "benefic" effect that makes my plugin work the way I want? this is just too crazy for me to understand why this still affects my script logics.Last edited by a moderator: Jun 21, 2018 -
If you post the full code I can help you.
-
Solved... maybe someone can explain why this matters?
BUGGED code:
Code:var rand = new Random(); var number = rand.Next(100); if (number <= 30) // 30 = 30% chance { Puts("success");} // converts ALL items on success (BAD logic) itembp = ItemManager.CreateByName("blueprintbase", 1); itembp.blueprintTarget = itemid; itembp.MoveToContainer(corpse.containers[0]); }
Code:var number = random.Next(100); if (number <= 30) // 30 = 30% chance { Puts("success");}// converts THIS item only on success (THE logic) itembp = ItemManager.CreateByName("blueprintbase", 1); itembp.blueprintTarget = itemid; itembp.MoveToContainer(corpse.containers[0]); }
Last edited by a moderator: Jun 25, 2018 -
What's that stray } doing after your Puts();?
If the code compiles as shown above, I think the Puts(); is conditional and the rest will happen always. -
-
Idk, maybe it's just a type error in the post but I spotted you have Puts("success");} and thought I'd point it out.
-