Jump to content

Returning to coding, Simple container problem


halomantis

Recommended Posts

Back in the day (before build 33) I had a simple little log sled.  A container that only allowed logs to be stored in it.  I accomplished this by basically making a container that had 0 capacity and using a function to put the logs in the sled.  Which worked great.  But after build 33 this function seemed to die.  Let me include the full scripts.

first the script for the log sled and putting logs into it.  There is a "Placebo" item as the result, it is removed automatically.  Also LogSledTest is called to verify the player really has a Log Sled so I don't have to use it as part of the recipe.

item LogSled
    {
        WeightReduction    	=    99,
        Weight    		=    9,
        Type    		=    Container,
        Capacity    		=    0,
        DisplayName    		=    Wooden Log Sled,
        Icon    		=    LogSled,
   }
   
/************************RECIPES************************/

	recipe Put Log In Sled
	{
		Log,
		
		SkillRequired:Woodwork=1,
		Result:Placebo,
		OnTest:LogSledTest,
		CanBeDoneFromFloor:true,
		OnCreate:PutLogInSled,
		Category:Carpentry,
	}

 

 

Next the functions (No laughing)

function PutLogInSled()
	
	local player = getSpecificPlayer(0);
	MaxLogSledCount = player:getPerkLevel(Perks.Woodwork) * 4;
	
	print("MaxLogSledCount: " .. MaxLogSledCount);
	
	LogSledCount = 0;
	for i = 0, getPlayer():getInventory():getItems():size() - 1 do
		if getPlayer():getInventory():getItems():get(i) ~= nil and getPlayer():getInventory():getItems():get(i):getType() == "LogSled" then
			BagID = getPlayer():getInventory():getItems():get(i)
			for t = 0, BagID:getInventory():getItems():size() - 1 do
				if BagID:getInventory():getItems():get(t):getType() == "Log" then
					LogSledCount = LogSledCount + 1;
				end
			end
		end
	end
	
	LogInventoryCount = 0;
	for i = 0, getPlayer():getInventory():getItems():size() - 1 do
		if getPlayer():getInventory():getItems():get(i):getType() == "Log" then
			LogInventoryCount = LogInventoryCount + 1;
		end
	end
	
	print("LogSledCount: " .. LogSledCount);
	print("LogInventoryCount: " .. LogInventoryCount);
	
	while LogInventoryCount > 0 and LogSledCount < MaxLogSledCount do	
		for i = 0, getPlayer():getInventory():getItems():size() - 1 do
			if getPlayer():getInventory():getItems():get(i) ~= nil and getPlayer():getInventory():getItems():get(i):getType() == "LogSled" then
				print("Made it this far");
				container = getPlayer():getInventory():getItems():get(i);
				container:getInventory():AddItem("Base.Log");
				LogSledCount = LogSledCount + 1;
			end
		end
		if getPlayer():getInventory():contains("Log") then	
			getPlayer():getInventory():Remove("Log");
			LogInventoryCount = LogInventoryCount - 1;
		end
				
		print("LogSledCount: " .. LogSledCount);
		print("LogInventoryCount: " .. LogInventoryCount);
	end
	if LogInventoryCount > 0 then
		player:Say("I can't figure out how to add more logs")
	end
end

function LogSledTest()
	if getPlayer():getInventory():contains("LogSled") ~= false then
		return true
	else
		return false
	end
end

function DLH_Functions.ErasePlacebo()
	if getPlayer():getInventory():contains("Placebo") then	
		getPlayer():getInventory():Remove("Placebo");
	end
end

Events.OnTick.Add(DLH_Functions.ErasePlacebo);

All the prints kick off EXCEPT for the "Made it this far" which shows that I am somehow not able to get the id of the logsled or something to that effect.  

 

Long winded and likely an easy fix, but the poo I am throwing at the wall isn't sticking yet.

Link to comment
Share on other sites

Wow ... the code preview of the new forum is ugly as hell :o

 

Instead of using get( i ) have you tried iterating over the items?

 

for i = 1, item in ipairs( getPlayer():getInventory():getItems() ) do
	-- Do some really cool things here, like saving the world or eating sushi.
end

Not sure if you'll find anything specifically related to your mod, but I am doing some inventory operations in:

Might be worth a look :)

 

Edited by RoboMat
Link to comment
Share on other sites

Also here's a small code hint (I used to do this all the time back when I started):

function LogSledTest()
	if getPlayer():getInventory():contains("LogSled") ~= false then
		return true
	else
		return false
	end
end

Can be replaced with:

function LogSledTest()
    return getPlayer():getInventory():contains("LogSled") ~= false
end

Since the evaluation already spits out the necessary boolean :)

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...