代码来源于“易宁”大佬的分享,仅供学习,不要直接复制粘贴。

原帖链接:http://bbs.3dmgame.com/thread-3859071-1-1.html



二八二.肉食店(在猪房买卖肉类)

	用MT管理器打开游戏目录/assets/DLC0002/scripts/prefabs/pighouse.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容:

local function ShouldAcceptItem(inst, item)
	if item.prefab == "meat" or item.prefab == "smallmeat" or item.prefab == "fish" or item.prefab == "eel" or item.prefab == "drumstick" or item.prefab == "bird_egg" or item.prefab == "froglegs" or item.prefab == "monstermeat" or item.prefab == "goldnugget" then
	   return true
	end
	return false
end
local function OnGetItemFromPlayer(inst, giver, item)
	if item.prefab == "meat" or item.prefab == "smallmeat" or item.prefab == "fish" or item.prefab == "eel" or item.prefab == "drumstick" or item.prefab == "bird_egg" or item.prefab == "froglegs" or item.prefab == "monstermeat" then
	   local goldnugget = SpawnPrefab("goldnugget")
	   giver.components.inventory:GiveItem(goldnugget)
	end
	if item.prefab == "goldnugget" then
	   local names = {"meat","smallmeat","fish","eel","drumstick","bird_egg","froglegs","monstermeat"}
	   local name = names[math.random(#names)]
	   local meat = SpawnPrefab(name)
	   giver.components.inventory:GiveItem(meat)
	end
end
	inst:AddComponent("trader")
	inst.components.trader.onaccept = OnGetItemFromPlayer
	inst.components.trader:SetAcceptTest(ShouldAcceptItem)

	即可将各种肉类卖给猪房(拿着肉类左键点猪房),获得1个黄金,黄金会自动打入账户(主角物品条)。也可以给猪房黄金,买入随机品种的肉类,可以买卖的肉类包括大肉、小肉、鱼、鳗鱼、鸡腿、鸟蛋、蛙腿、疯肉