grimmsdottir Posted May 13, 2015 Share Posted May 13, 2015 Hello, I am trying to create a character who has a blaster. I was wondering if it is possible to check if the player has a certain item(charged batteries), then remove a certain number of those batteries, and then add back in uncharged batteries? Any help would be much appreciated. Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/ Share on other sites More sharing options...
Mobbstar Posted May 13, 2015 Share Posted May 13, 2015 (edited) You can use player.components.inventory:FindItem(itemtestfunction) to iterate the players inventory, it returns the first item found. "itemtestfunction" receives every item as parameter once and has to return true if the item matches the criteria. Then, if there is an item found, you can simply use inst:Remove() and give a new item to the player using player.components.inventory:GiveItem(SpawnPrefab("myitem")) Edited May 13, 2015 by Mobbstar Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637244 Share on other sites More sharing options...
grimmsdottir Posted May 14, 2015 Author Share Posted May 14, 2015 (edited) @Mobbstar Alright, thanks for the help. However, I am not quite understanding what you mean by "itemtestfunction" receives every item as parameter once and has to return true if the item matches the criteria. Edited May 14, 2015 by grimmsdottir Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637396 Share on other sites More sharing options...
Seiai Posted May 14, 2015 Share Posted May 14, 2015 (edited) an example:player.components.inventory:FindItem(function(item) return item.prefab=="battery" end)so the game will just go over each item in your inventory and use this function on them:function(item) return item.prefab=="battery" endand if this returns true(in this case, if the prefabname of your item is "battery") then FindItem returns that item. Edited May 14, 2015 by Seiai Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637408 Share on other sites More sharing options...
grimmsdottir Posted May 14, 2015 Author Share Posted May 14, 2015 Thanks for replying, but I am still not quite able to pull it off. Right now, I have this in my code:local function onattack_blaster() ThePlayer.components.inventory.FindItem(function(item) return item.prefab=="item_battery_charged" end) endand it crashes with this in the log the moment I call that function[00:01:09]: [string "scripts/components/inventory.lua"]:362: attempt to index local 'self' (a function value)LUA ERROR stack traceback:scripts/components/inventory.lua:362 in (field) FindItem (Lua) <361-374> self = function - ../mods/DSTmod-Orion_Castilio/scripts/prefabs/item_blaster.lua:23 fn = nil../mods/DSTmod-Orion_Castilio/scripts/prefabs/item_blaster.lua:23 in (field) onprojectilelaunch (Lua) <21-25>scripts/components/weapon.lua:91 in (method) LaunchProjectile (Lua) <87-105> self = inst = 112033 - item_blaster(LIMBO) (valid:true) projectile = fire_projectile onprojectilelaunch = function - ../mods/DSTmod-Orion_Castilio/scripts/prefabs/item_blaster.lua:21 hitrange = 10 _ = table: 222704E8 damage = 0 attacker = 111502 - orion (valid:true) target = 112097 - robin (valid:true)scripts/components/combat.lua:720 in (method) DoAttack (Lua) <701-763> self = hiteffectsymbol = torso laststartattacktime = 10.633333887905 lastwasattackedtime = 0 playerdamagepercent = 1 nextbattlecrytime = 18.41927706244 pvp_damagemod = 0.5 GetGiveUpString = function - scripts/prefabs/player_common.lua:10 forcefacing = true keeptargettimeout = 0 defaultdamage = 10 inst = 111502 - orion (valid:true) losetargetcallback = function - scripts/components/combat.lua:248 GetBattleCryString = function - scripts/prefabs/player_common.lua:1379 battlecryenabled = true _ = table: 2AF9F9B8 hitrange = 2 target_override = 112097 - robin (valid:true) weapon = nil projectile = nil stimuli = nil instancemult = nil targ = 112097 - robin (valid:true) weapon = 112033 - item_blaster(LIMBO) (valid:true)scripts/actions.lua:542 in (field) fn (Lua) <541-544> act = Attack 112097 - robin With Inv:112033 - item_blaster(LIMBO) (valid:true)scripts/bufferedaction.lua:22 in (method) Do (Lua) <19-35> self (valid:true) = action = table: 1211C5D8 invobject = 112033 - item_blaster(LIMBO) (valid:true) onfail = table: 36A90CF0 doerownsobject = true autoequipped = true target = 112097 - robin (valid:true) options = table: 36A90D40 doer = 111502 - orion (valid:true) onsuccess = table: 36A90638scripts/entityscript.lua:1193 in (method) PerformBufferedAction (Lua) <1185-1203> self (valid:true) = DynamicShadow = DynamicShadow (21A8DE00) inlimbo = false GetMoistureRateScale = function - scripts/prefabs/player_common.lua:97 SetCameraDistance = function - scripts/prefabs/player_common.lua:1033 ScreenFlash = function - scripts/prefabs/player_common.lua:1087 player_classified = 111503 - player_classified (valid:true) playercolour = table: 3F9FD0C0 AnimState = AnimState (21A8D4A0) Light = Light (21A8F8A0) OnRemoveEntity = function - scripts/prefabs/player_common.lua:526 pendingtasks = table: 3FD3B088 ShowHUD = function - scripts/prefabs/player_common.lua:1027 sg = sg="wilson", state="attack", time=0.27, tags = "attack,abouttoattack,notalking,autopredict," IsHUDVisible = function - scripts/prefabs/player_common.lua:1017 ApplyScale = function - scripts/prefabs/player_common.lua:1102 SetGhostMode = function - scripts/prefabs/player_common.lua:442 CanExamine = function - scripts/prefabs/player_common.lua:20 HUD = HUD OnWakeUp = function - scripts/prefabs/player_common.lua:950 Transform = Transform (21A8D200) actionreplica = table: 3FD3AB60 batteriesnum = 0 blasterstun = false blasterincinerate = false lower_components_shadow = table: 3FD39FD0 GetMaxMoisture = function - scripts/prefabs/player_common.lua:87 prefab = orion updatecomponents = table: 2AFA5318 ShakeCamera = function - scripts/prefabs/player_common.lua:1047 OnDespawn = function - scripts/prefabs/player_common.lua:968 Physics = Physics (21A90480) MiniMapEntity = MiniMapEntity (21A8F1C0) IsFreezing = functionAm I still not using it correctly? Take note that I am modding for DSTlog.txt Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637425 Share on other sites More sharing options...
Mobbstar Posted May 14, 2015 Share Posted May 14, 2015 Use a colon (inventory:FindItem) Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637437 Share on other sites More sharing options...
grimmsdottir Posted May 14, 2015 Author Share Posted May 14, 2015 fml tencharacters Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637441 Share on other sites More sharing options...
grimmsdottir Posted May 14, 2015 Author Share Posted May 14, 2015 Alright, now I have a new problem. The I can find, remove and replace the batteries. However, the batteries are and should be stackable, and it is removing the whole stack of batteries. It does however, only remove one battery if there is only one in a stack. How would I make it so that it only removes a certain number of batteries?local function onattack_blaster(inst, attacker, target, skipsanity) if ThePlayer.components.inventory:FindItem(function(item) return item.prefab=="item_battery_charged" end) then ThePlayer.components.talker:Say("I have a charged battery") local batt = ThePlayer.components.inventory:FindItem(function(item) return item.prefab=="item_battery_charged" end) batt:Remove() ThePlayer.components.inventory:GiveItem(SpawnPrefab("item_battery")) else ThePlayer.components.talker:Say("I do not have a charged battery") end end Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637452 Share on other sites More sharing options...
Mobbstar Posted May 14, 2015 Share Posted May 14, 2015 Alright, now I have a new problem. The I can find, remove and replace the batteries. However, the batteries are and should be stackable, and it is removing the whole stack of batteries. It does however, only remove one battery if there is only one in a stack. How would I make it so that it only removes a certain number of batteries? I see. Luckily the stackable component has a function for that:inst.components.stackable:Get(1) --returns one item of the stack Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637453 Share on other sites More sharing options...
Seiai Posted May 14, 2015 Share Posted May 14, 2015 if u still have problems, look for something similar. like for example food. which u consume with the Eater component's function Eat. Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637455 Share on other sites More sharing options...
grimmsdottir Posted May 14, 2015 Author Share Posted May 14, 2015 Oh, alrighty then, I guess that wraps it up. Thanks to the 2 of you. Link to comment https://forums.kleientertainment.com/forums/topic/53907-detect-and-remove-an-item-from-inventory/#findComment-637456 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