Jump to content

Have a building option choose between different items


gaunti12

Recommended Posts

Hi. I am currently working on the building mod and after I looked at the new log walls I want to implement something like it is done there with rope, strings and ripped sheets.

 

In essence I want to have a building option that works with different items as a requisite, but needs only one of them.

 

This is a test if I get it working. The option in particular is not that important. I tried different approaches, none of them worked. I am currently at this:

 

The option in the submenu is working, it looks if one of the gastanks is in the inventory, and if one is, the option gets "white".

 

    -- add Small Propane Tank
    local smallPropaneTankOption = subMenuIndustrial:addOption("Small Propane Tank", worldobjects, BuildingMod.onSmallPropaneTank,  square, player);
    local tooltip = BuildingMod.canBuild(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,smallPropaneTankOption, player);
    if not (getSpecificPlayer(player):getInventory():contains("EmptyGasBottle") or getSpecificPlayer(player):getInventory():contains("EmptyGasBottleBBQ") or getSpecificPlayer(player):getInventory():contains("GasBottle") or getSpecificPlayer(player):getInventory():contains("GasBottleBBQ") or getSpecificPlayer(player):getInventory():contains("PropaneTank")) and not BuildingMod.cheat then
        tooltip.description = tooltip.description .. " <RGB:1,0,0>" .. getItemText("Propane Tank") .. " 0/1 ";
        smallPropaneTankOption.onSelect = nil;
        smallPropaneTankOption.notAvailable = true;
    else
        tooltip.description = tooltip.description .. " <RGB:1,1,1>" .. getItemText("Propane Tank") .. " 1 ";
    end
    tooltip:setName("Small Propane Tank");
    tooltip.description = "Put Gas Tank on the ground. Serves no function but a way to dispose of the empty gas bottles. " .. tooltip.description;
    tooltip:setTexture("location_shop_fossoil_01_47");

 

But when I click it, nothing happens. In the console it says:

function: onSmallPropaneTank -- file: BuildingMod.lua line # 2341
function: onMouseUp -- file: ISContextMenu.lua line # 78
attempted index: getInventory of non-table: null-----------------------------------------
STACK TRACE

 

The line 2341 is in the onSmallPropaneTank function. It's the line with the first "if not"

BuildingMod.onSmallPropaneTank = function(worldobjects, player)
    -- sprite, northSprite
    local decoration = BMDecoration:new("Small Propane Tank", "location_shop_fossoil_01_47", "location_shop_fossoil_01_55");
    decoration.blockAllTheSquare = false;
    decoration.renderFloorHelper = true;
    decoration.canPassThrough = true;
    decoration.player = player;
    if not (getSpecificPlayer(player):getInventory():contains("EmptyGasBottleBBQ") or getSpecificPlayer(player):getInventory():contains("GasBottle") or getSpecificPlayer(player):getInventory():contains("GasBottleBBQ") or getSpecificPlayer(player):getInventory():contains("PropaneTank")) then
        decoration.modData["need:Recycling.EmptyGasBottle"] = "1";
    elseif not (getSpecificPlayer(player):getInventory():contains("EmptyGasBottle") or getSpecificPlayer(player):getInventory():contains("GasBottle") or getSpecificPlayer(player):getInventory():contains("GasBottleBBQ") or getSpecificPlayer(player):getInventory():contains("PropaneTank")) then
        decoration.modData["need:Recycling.EmptyGasBottleBBQ"] = "1";
    elseif not (getSpecificPlayer(player):getInventory():contains("EmptyGasBottleBBQ") or getSpecificPlayer(player):getInventory():contains("EmptyGasBottle") or getSpecificPlayer(player):getInventory():contains("GasBottleBBQ") or getSpecificPlayer(player):getInventory():contains("PropaneTank")) then
        decoration.modData["need:Recycling.GasBottle"] = "1";
    elseif not (getSpecificPlayer(player):getInventory():contains("EmptyGasBottleBBQ") or getSpecificPlayer(player):getInventory():contains("EmptyGasBottle") or getSpecificPlayer(player):getInventory():contains("GasBottle") or getSpecificPlayer(player):getInventory():contains("PropaneTank")) then
        decoration.modData["need:Recycling.GasBottleBBQ"] = "1";
    elseif not (getSpecificPlayer(player):getInventory():contains("EmptyGasBottleBBQ") or getSpecificPlayer(player):getInventory():contains("EmptyGasBottle") or getSpecificPlayer(player):getInventory():contains("GasBottle") or getSpecificPlayer(player):getInventory():contains("GasBottleBBQ")) then
        decoration.modData["need:Base.PropaneTank"] = "1";
    end
    -- allow the item to be dragged by mouse
    getCell():setDrag(decoration, player);
end

 

I'd tried a positive condition ("if" instead of "if not") before, but that didn't work and the "getSpecificPlayer(player):getInventory():contains("xxx")" allways uses an "if not", so I went for that. It looks better if I use only "if", like this:

 

BuildingMod.onSmallPropaneTank = function(worldobjects, player)
    -- sprite, northSprite
    local decoration = BMDecoration:new("Small Propane Tank", "location_shop_fossoil_01_47", "location_shop_fossoil_01_55");
    decoration.blockAllTheSquare = false;
    decoration.renderFloorHelper = true;
    decoration.canPassThrough = true;
    if getSpecificPlayer(player):getInventory():contains("EmptyGasBottle") then
        decoration.modData["need:Recycling.EmptyGasBottle"] = "1";
    elseif getSpecificPlayer(player):getInventory():contains("EmptyGasBottleBBQ") then
        decoration.modData["need:Recycling.EmptyGasBottleBBQ"] = "1";
    elseif getSpecificPlayer(player):getInventory():contains("GasBottle") then
        decoration.modData["need:Recycling.GasBottle"] = "1";
    elseif getSpecificPlayer(player):getInventory():contains("GasBottleBBQ") then
        decoration.modData["need:Recycling.GasBottleBBQ"] = "1";
    elseif getSpecificPlayer(player):getInventory():contains("PropaneTank") then
        decoration.modData["destroy:Base.PropaneTank"] = "1";
    end
    decoration.player = player;
    -- allow the item to be dragged by mouse
    getCell():setDrag(decoration);
end

But with that I get the same error message.

 

Atm I think the problem is not if I use a positive or negative condition, but something else, and I am at a loss what this could be.

 

Any help would be much appreciated. I am beginning to understand the lua code and my try and error method in this particular case hasn't got me very far.

Link to comment
Share on other sites

subMenuIndustrial:addOption("Small Propane Tank", worldobjects, BuildingMod.onSmallPropaneTank,  square, player);

I believe that will pass worldobjects, square, player to onSmallPropaneTank but your second argument in onSmallPropaneTank is player

Try:

BuildingMod.onSmallPropaneTank = function(worldobjects, square, player)

To better explain the error message you're seeing "getInventory of non-table: null" says that you're trying to call getInventory() on a null variable which is probably what getSpecificPlayer() is returning since it's not actually being passed a player.

I think you can use something like print("Debug: player "..tostring(player)); print("Debug: specificplayer"..tostring(getSpecificPlayer(player))) in the future to help pin it down.

Link to comment
Share on other sites

Yeah!!! That was it! I wasn't aware that the order in which the arguments are is important, too. Thank you very very much for the fast response!

 

The "print("Debug: player "..tostring(player)); print("Debug: specificplayer"..tostring(getSpecificPlayer(player)))" has to go into the lua file I suppose.

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
×
×
  • Create New...