Jump to content

I've Created a Black Hole


Recommended Posts

The title says it all... Well, not really. Anyway, I was trying to make my character have a special backpack that acted as a backpack and armor, and it works in a sense. You can store stuff in the backpack and it absorbs damage, but when the backpack breaks all the items get sucked into a black hole and are never seen again (the items inside vanish with the backpack when it breaks). I tried adding the code

local function onbreak(inst)
    if inst.components.container then
        inst.components.container:DropEverything()
        inst.components.container:Close()
        inst:RemoveComponent("container")
    end
end

and it worked in the sense that the game didn't crash, but the items still vanish.... Can someone help me make it so the backpack doesn't destroy the items upon it's own death? I'm not good with code... but at least I tried? ;-;

Link to comment
Share on other sites

Try

local function onbreak(owner, data)
    local armor = data ~= nil and data.armor or nil
    if armor and armor.components.container then
        armor.components.container:DropEverything()
        armor.components.container:Close()
        armor:RemoveComponent("container")
    end
end

local function onequip(inst, owner) 
    ...
    inst:ListenForEvent("armorbroke", onbreak, owner)
end

local function onunequip(inst, owner) 
    ...
    inst:RemoveEventCallback("armorbroke", onbreak, owner)
end

 

Link to comment
Share on other sites

I just deleted the '...'s just to mess with stuff and now it works but the textures don't display. Which in general is a much larger(or at least more annoying) problem... I'll be adding the '...'s back in now....

Edited by Scoobie101
Link to comment
Share on other sites

7 minutes ago, Muche said:

Yeah, I assumed you already have onequip and onunequip functions for your item. That pseudo-code meant to add that one line at the end of that particular function.

Umm... ok? Sure? I have no clue what you're talking about, although I can confirm that I already have the lines:

local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_body", "swap_pack", "backpack")
    owner.AnimState:OverrideSymbol("swap_body", "swap_pack", "swap_body")
    inst.components.container:Open(owner)
end

local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")
    owner.AnimState:ClearOverrideSymbol("backpack")
    inst.components.container:Close(owner)
end

if that has anything to do with what you said... I think it does....

EDIT

I think I got it... Lemme try some MAGIC....

Edited by Scoobie101
Link to comment
Share on other sites

I changed the script so that it looks like this:

local function onbreak(owner, data)
    local armor = data ~= nil and data.armor or nil
    if armor and armor.components.container then
        armor.components.container:DropEverything()
        armor.components.container:Close()
        armor:RemoveComponent("container")
    end
end

local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_body", "swap_wool_sack", "backpack")
    owner.AnimState:OverrideSymbol("swap_body", "swap_wool_sack", "swap_body")
    inst.components.container:Open(owner)
    inst:RemoveEventCallback("armorbroke", onbreak, owner)
end

local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")
    owner.AnimState:ClearOverrideSymbol("backpack")
    inst.components.container:Close(owner)
    inst:RemoveEventCallback("armorbroke", onbreak, owner)
end

but now I'm back to square one in that the items get sucked into a black hole upon the backpack being broken. Did I do something wrong?

Link to comment
Share on other sites

onequip should add the listener, everything else is as planned:

local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_body", "swap_wool_sack", "backpack")
    owner.AnimState:OverrideSymbol("swap_body", "swap_wool_sack", "swap_body")
    inst.components.container:Open(owner)
    inst:ListenForEvent("armorbroke", onbreak, owner)
end

 

Link to comment
Share on other sites

Okay, I changed that part that was suppose to say ListenForEvent, but now it says: attempt to call index field 'container' (a nil value) on line 65, which would be:  inst.components.container:Close(owner) in onunequip when the backpack is broken in game....

Link to comment
Share on other sites

It seems the onunequip function is called after the armorbroke event is pushed. You could try removing armor:RemoveComponent("container") from onbreak function, or add a check in onunequip function:

if inst.components.container ~= nil then
    inst.components.container:Close(owner)
end

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...