[JSON/LUA] Entity being created in wrong form

Hey there, everyone! I’ll try to make it quick this time :slight_smile:

Is there a way to reference the entity form of something with the uri or alias address? For example, if I want to reference the “stonehearth:furniture:stone_chair” item - but specifically its iconic form - is there a way to do it?

Why I want to know this: on a script (LUA), I’m using the

function entities.create_entity(ref, options)

from Radiant ‘entities.lua’ to create an entity and then

function StorageComponent:add_item(item, force_add, owner_player_id)

to add that entity inside a storage. The item has several entity forms, however (it is a placeable piece of furniture) and instead of placing the iconic form, the game is trying to place the root form and instead I’m getting the error I found here on this bit of “storage_component.lua”:

elseif iconic and item == root then
      radiant.verify(false, 'cannot add %s to storage because it is the root form!', root)
      return false

I don’t know if it makes much sense without more context, I could share more but in simple terms my issue is that the game is trying to add a root/in world form item inside a storage and I want to tell it to add the iconic form instead, but I don’t know how

Let me know if I can help with more information and as usual - thank you very much for any support :slight_smile:

try with:

1 Like

Hmmm, thought it worked at first but didn’t manage to, will try again tomorrow when my head is clearer, I guess :smiley:

One of the options when creating an entity would be { force_iconic = true }, see if that helps you.
I recommend this order to force create an item into an storage, so that it also gets added to the corresponding inventory, you can try it if the above causes issues:

local inventory = stonehearth.inventory:get_inventory(player_id)
local sc = my_storage_entity:get_component('stonehearth:storage')
local jerky = radiant.entities.create_entity('stonehearth:red_fox_jerky')
inventory:add_item(jerky, my_storage_entity, true)
sc:add_item(jerky, true, player_id) 

Thanks @BrunoSupremo and @Relyss for your kind replies :slight_smile:
I feel like I am missing something obvious, however :sob:

I tried these solutions but somehow I keep getting the error “Cannot add entity to storage because it is the root form!” even though I tried forcing iconic option like Relyss suggested on both the create_entity and the add_item functions. It’s like the option was not recognized, I guess? Or maybe I added it in the wrong place?

Here’s the function that transforms one item into another; This works perfectly when dealing with items that only have a single form (like, for example, refined items or resources); the issue just appears when the item being transformed to (referred here as “cool_alias”/“cool_entity”) is an item with multiple forms and the transformation occurs inside storage.

You’ll notice that it is pretty much the same function as present in Food_Decay service, since my service was based on that one.

function CoolingService:_convert_to_cool_form(entity, cool_alias)
   local inventory = nil
   local storage_component = nil
   local location = nil
   local cool_entity
   if cool_alias then
      local player_id = radiant.entities.get_player_id(entity)
      if player_id then
         inventory = stonehearth.inventory:get_inventory(player_id)
         location = radiant.entities.get_world_grid_location(entity)
         cool_entity = radiant.entities.create_entity(cool_alias, { owner = player_id })
         if not location then
            local storage = inventory:container_for(entity)
            if storage then
               storage_component = storage:get_component('stonehearth:storage')
   if inventory then
   if cool_entity then
      if location then
         radiant.terrain.place_entity(cool_entity, location)
      elseif not storage_component or not storage_component:add_item(cool_entity, true) then
         if inventory then

I tried adding the { force_iconic = true } as options both in the create_entity function and later on the add_item function; neither worked.

Any ideas?
Thank you so much and forgive me for taking your time :slight_smile:


Sorry for the double post but…
I DID IT! Finally! :smiley: :smiley: thank you two so much for the tips!
At first neither of the suggestions were working :sob: and I felt kinda lost, to be honest… but then I started looking around for similar code/situations in stonehearth itself and remembered the Entity Creator from DebugTools mod - it can create iconic entities, right?

So turns out that Bruno’s suggestion was functional but I didn’t know how to apply it properly - however by looking around DebugTools code I found out how to properly use that and saved my mod with this little bit of code I wrote after the entity is created:

cool_entity = radiant.entities.create_entity(cool_alias, { owner = player_id })
		 local entity_forms = cool_entity:get_component('stonehearth:entity_forms')
		 if entity_forms ~= nil then
			cool_entity = entity_forms:get_iconic_entity()

I’ll finally be able to finish this mod now :3

1 Like