Cacahuatoforo Posted September 16, 2013 Share Posted September 16, 2013 Can anybody help me with some coding headaches? Here's the deal: I'm working on a special power (a pretty cool one, actually ) for my custom char, wich is like 75% finished, my only big problems are 2, a minor one, and a mayor one... The minor one is that, even though I already have given him an extra inventory slot, AND adjusted the inventory bar scale to make them fit, while the inventory slot IS char-specific, I have no idea how to make the inventory bar scaling char-specific (wilson's inventory bar also gets scaled) , I put this code in modmain.lua: local function inventorybarPostConstruct(self, owner) self.bg:SetScale(1.15,1,1) endAddClassPostConstruct("widgets/inventorybar", inventorybarPostConstruct) The second one is much more complicated... (and I don't know if i't actually possible) I want to make him to automatically pickup (like with the lazy forager) a certain item, and only that item... with this one I don't even know where to start the code... :S I just started to get used to all this coding thing, I have little lua experience, so I'm totally lost with this one... That pretty much covers everything. Any advice? EXTRASLOT: http://postimg.org/image/svs08k6wd/NOSLOTSCALING: http://postimg.org/image/wy3d8ygt7/ Link to comment Share on other sites More sharing options...
Cacahuatoforo Posted October 2, 2013 Author Share Posted October 2, 2013 I'm necro-ing my own thread But seriously... nobody? This is still bugging me to date... Link to comment Share on other sites More sharing options...
squeek Posted October 2, 2013 Share Posted October 2, 2013 First problem can be solved by something like this in your post construct function:if GetPlayer().prefab == "yourcustomcharacter" then self.bg:SetScale(1.15,1,1)endFor the second problem, look at how the lazy forager does it (prefabs/amulet.lua's onequip_orange function). Link to comment Share on other sites More sharing options...
Cacahuatoforo Posted October 3, 2013 Author Share Posted October 3, 2013 For the second problem, look at how the lazy forager does it (prefabs/amulet.lua's onequip_orange function). I already guessed how to make an equippable to just pick a specific item... but I can't "import" the function to the char itself, so that HE is the one who picks items, not the equippable. Link to comment Share on other sites More sharing options...
squeek Posted October 3, 2013 Share Posted October 3, 2013 I already guessed how to make an equippable to just pick a specific item... but I can't "import" the function to the char itself, so that HE is the one who picks items, not the equippable.But the lazy forager already does that. The amulet creates a periodic task that runs a function that gets all entities in the area and sends them to its owner's inventory if possible. The only change you'd need to make is to do a simple entity.prefab == "thethingyouwanttopickup" when looping through the found entities.But do you want this tied to having a certain item equipped (only picking up an item automatically if some other item is equipped), or do you want it an innate ability that the character has? If you want it an innate ability, you can just put the inst:DoPeriodicTask() inside your character's prefab's fn function.Here's untested code of it being an innate ability (this would be in your character's prefab .lua):local PICKUP_ITEM_CHECK_PERIOD = 0.33local PICKUP_RANGE = 4local PICKUP_ITEM_PREFAB = "youritemprefab"-- copied from amulet.lualocal function PickupEffect(inst) local pt = inst:GetPosition() local fx = SpawnPrefab("small_puff") fx.Transform:SetPosition(pt.x, pt.y, pt.z) fx.Transform:SetScale(0.5,0.5,0.5)end-- copied from amulet.lualocal function getitem(player, item) --will only ever pick up items one at a time. Even from stacks. PickupEffect(item) if item.components.stackable then item = item.components.stackable:Get() end player.components.inventory:GiveItem(item)end-- copied from amulet.lualocal function pickup(inst) local pt = inst:GetPosition() local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, PICKUP_RANGE) for k,v in pairs(ents) do if v.prefab == PICKUP_ITEM_PREFAB then -- this can be included in the if statement below; just putting it outside of it for clarity's sake if v.components.inventoryitem and v.components.inventoryitem.canbepickedup and v.components.inventoryitem.cangoincontainer and not v.components.inventoryitem:IsHeld() then if not owner.components.inventory:IsFull() then --Your inventory isn't full, you can pick something up. getitem(inst, v) return elseif v.components.stackable then --Your inventory is full, but the item you're trying to pick up stacks. Check for an exsisting stack. --An acceptable stack should: Be of the same item type, not be full already and not be in the "active item" slot of inventory. local stack = owner.components.inventory:FindItem(function(item) return (item.prefab == v.prefab and not item.components.stackable:IsFull() and item ~= owner.components.inventory.activeitem) end) if stack then getitem(inst, v) return end end end end end endfunction fn() local inst = CreateEntity() -- normal prefab initialization stuff inst.pickuptask = inst:DoPeriodicTask( PICKUP_ITEM_CHECK_PERIOD, function() pickup(inst) end ) return instendIdeally, you'd want to code a "telekinesis" component that has all this logic in it and then you would just add it to your character and set up an item filter for it, but for a single character mod it doesn't really matter (but the amulet should be using a component rather than local functions). Link to comment Share on other sites More sharing options...
Cacahuatoforo Posted October 4, 2013 Author Share Posted October 4, 2013 But do you want this tied to having a certain item equipped (only picking up an item automatically if some other item is equipped), or do you want it an innate ability that the character has? If you want it an innate ability, you can just put the inst:DoPeriodicTask() inside your character's prefab's fn function I already got to make an equippable to only pickup certain item, by adding a tag to the item, and then, the code to check for that tag, it worked like a charm. But I want it to be an innate ability, and I just can't get that right... Here's untested code of it being an innate ability (this would be in your character's prefab .lua) That didn't work , and yeah, I want it to be an innate ability. I tried my best to "fix" it but I'm just getting crashes after crashes.I would like to be of more help than that but this is just not my thing... Link to comment Share on other sites More sharing options...
squeek Posted October 7, 2013 Share Posted October 7, 2013 Alright, got the chance to test this out and fix the bugs. Also, as I've never made a character prefab before, I didn't realize that their fn function was different than other prefabs.Here's working code (the fn function is taken from the sample character): local PICKUP_ITEM_CHECK_PERIOD = 0.33local PICKUP_RANGE = 4local PICKUP_ITEM_PREFAB = "berries" -- copied from amulet.lualocal function PickupEffect(inst) local pt = inst:GetPosition() local fx = SpawnPrefab("small_puff") fx.Transform:SetPosition(pt.x, pt.y, pt.z) fx.Transform:SetScale(0.5,0.5,0.5)end -- copied from amulet.lualocal function getitem(player, item) --will only ever pick up items one at a time. Even from stacks. PickupEffect(item) if item.components.stackable then item = item.components.stackable:Get() end player.components.inventory:GiveItem(item)end -- copied from amulet.lualocal function pickup(inst) local pt = inst:GetPosition() local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, PICKUP_RANGE) for k,v in pairs(ents) do if v.prefab == PICKUP_ITEM_PREFAB then -- this can be included in the if statement below; just putting it outside of it for clarity's sake if v.components.inventoryitem and v.components.inventoryitem.canbepickedup and v.components.inventoryitem.cangoincontainer and not v.components.inventoryitem:IsHeld() then if not inst.components.inventory:IsFull() then --Your inventory isn't full, you can pick something up. getitem(inst, v) return elseif v.components.stackable then --Your inventory is full, but the item you're trying to pick up stacks. Check for an exsisting stack. --An acceptable stack should: Be of the same item type, not be full already and not be in the "active item" slot of inventory. local stack = inst.components.inventory:FindItem(function(item) return (item.prefab == v.prefab and not item.components.stackable:IsFull() and item ~= inst.components.inventory.activeitem) end) if stack then getitem(inst, v) return end end end end end endlocal fn = function(inst) -- choose which sounds this character will play inst.soundsname = "wolfgang" -- a minimap icon must be specified inst.MiniMapEntity:SetIcon( "wilson.png" ) inst.pickuptask = inst:DoPeriodicTask( PICKUP_ITEM_CHECK_PERIOD, function() pickup(inst) end )end Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.
Please be aware that the content of this thread may be outdated and no longer applicable.