Jump to content

Using recipe.sortkey with updated crafting menu?


Recommended Posts

Cheers, I maintain Large Containers and I'd like to restore the behaviour it had with old crafting menu, where each of the larger containers would appear immediately after the respective vanilla container in the crafting menu.

This was achieved by reaching into AllRecipes after calling AddRecipe, and setting the sortkey value of the mod recipe to the sortkey value of the vanilla container + 0.1.

  local ingredients =
    ...
  local config = {
    ...
  }
  local rec = AddRecipe2("largesaltbox",ingredients,TECH.SCIENCE_TWO,config,{'STRUCTURES','CONTAINERS','COOKING'})
  if GLOBAL.AllRecipes.saltbox ~= nil then
    rec.sortkey = GLOBAL.AllRecipes.saltbox.sortkey + 0.1
  else
    print "ERROR: Couldn't sort largesaltbox recipe after regular saltbox"
  end

Currently, the code still runs without issues and using c_announce to inspect the sortkey values tells me that the sortkey is set, but the modded recipes still appear at the very end of the list.
image.png.52004f484775088053f5348c4252f900.png

Is there any way to reorder these?

Link to comment
Share on other sites

I don't think sortkey is used anymore in the new system, but I could be wrong.

From what I understand (and what worked for me), you now need to sort the recipe in a certain way for each filter it's in. For example, taking CONTAINERS as the filter, you both need to sort the recipe in the GLOBAL.CRAFTING_FILTERS.CONTAINERS.default_sort_values table and the GLOBAL.CRAFTING_FILTERS.CONTAINERS.recipes table.

The former table uses recipe names as the key and the recipe index as the value. So a recipe in the table could be ["saltbox"] = 64 (random number, but just to give an example), the key being ["saltbox"] and the value being the index 64. It's really easy in this case to add your item to be after the respective vanilla item, you just do what you already did with the sortkey and take the same index but add a fraction to it, something like this:

GLOBAL.CRAFTING_FILTERS.CONTAINERS.default_sort_values.largesaltbox = GLOBAL.CRAFTING_FILTERS.CONTAINERS.default_sort_values.saltbox + 0.01

In the other table, it's a little trickier. The table is inverted, so instead the recipe index is the index of the table and the value is the recipe name, like this [64] = "saltbox". You'd need to iterate through the recipes and find at what index the recipe you want to be placed before your item is and also remove the already existing entry for your item, since it'd automatically be there.

I'll just share the function I made to make this process a million times easier for myself, anyone that finds this thread can use it in their own projects, I don't mind! Credit or no credit, it's up to your discretion:

-- Position the recipes after a specific vanilla item in those filters
local function SortModdedRecipe(recipe, filter, reference)
	local default_sort_values = GLOBAL.CRAFTING_FILTERS[filter].default_sort_values
	if default_sort_values[reference] ~= nil then
		default_sort_values[recipe] = default_sort_values[reference] + 0.01
	end
	
	local recipes = GLOBAL.CRAFTING_FILTERS[filter].recipes
	local pos_to_remove = nil
	local new_pos = nil
	for i,v in ipairs(recipes) do
		if v == recipe then
			pos_to_remove = i
		end
		if v == reference then
			new_pos = i + 1
		end
	end
	table.remove(recipes, pos_to_remove)
	table.insert(recipes, new_pos, recipe)
end

-- The format is: your recipe name, the filter, the name of the recipe it should be placed after
-- Make sure to use this for each filter you want the recipe in!
SortModdedRecipe("largesaltbox", "COOKING", "saltbox")
SortModdedRecipe("largesaltbox", "CONTAINERS", "saltbox")
SortModdedRecipe("largesaltbox", "STRUCTURES", "saltbox")
Edited by ariadnesGambit
  • Thanks 1
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...