Jump to content

Recommended Posts

I'm working on a mod that involves two items: painmagnet and painmagnet_item.

Their relationship is similar to eyeturret and eyeturret_item from Houndius Shootius.

The issue I'm facing is that when I move painmagnet_item from my inventory with the mouse to build, the displayed text says Plant instead of Build.

Additionally, the constructed object is much smaller than expected.

Snipaste_2025-03-16_21-46-11.png.4d358bcdf06b435d2faff1bf73bcfb6d.png

 

The code repository for the mod is here

https://github.com/yanghao5/painmagnet

Here is the key code snippet .

modmain.lua

AddRecipe2("painmagnet_item",
    {
        Ingredient("nightmarefuel", 5)
    },
    TECH.SCIENCE_ONE,
    {
        -- placer = "painmagnet_placer",
        atlas = "images/inventoryimages/inventoryicon.xml",
        image = "inventoryicon.tex" 
    },
    {"MAGIC"}
)

scripts\prefabs.lua

-- painmagnet_item ondeploy
local function ondeploy(inst, pt, deployer)
    local structure = SpawnPrefab("painmagnet")
    if structure then
        structure.Transform:SetPosition(pt:Get())
        inst:Remove() -- Remove the item after deployment
    end
end 

-- painmagnet_item
local function itemfn()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)
    MakeInventoryFloatable(inst)

    -- inst:AddTag("structure")
    inst.AnimState:SetBank("painmagnet_item")
    inst.AnimState:SetBuild("painmagnet_item")
    inst.AnimState:PlayAnimation("idle")

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "inventoryicon"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/inventoryicon.xml"

    inst:AddComponent("deployable")
    inst.components.deployable.ondeploy = ondeploy
    inst.components.deployable:SetDeployMode(DEPLOYMODE.ANYWHERE)

    return inst
end

-- painmagnet
local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeObstaclePhysics(inst, 1)
    
    inst:AddTag("structure")

    inst.AnimState:SetBank("painmagnet")
    inst.AnimState:SetBuild("painmagnet")
    inst.AnimState:PlayAnimation("idle")

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(4) -- Takes 4 hammer hits to destroy
    inst.components.workable.onfinish = function(inst, worker)
        inst:Remove()                       -- Simply remove for now; add loot later if desired
    end

    return inst
end

return Prefab("painmagnet_item", itemfn, itemassets),
    Prefab("painmagnet", fn, assets),
    MakePlacer("painmagnet_placer", "painmagnet", "painmagnet", "idle")

 

I think the key is the ondeploy function.

local function ondeploy(inst, pt, deployer)
    local structure = SpawnPrefab("painmagnet")
    if structure then
        structure.Transform:SetPosition(pt:Get())
        inst:Remove() 
    end
end 

I searched the Portable mod and didn't find any useful examples.

Does anyone know how to fix it?
Or are there any related examples I can refer to?

You can make your Recipe structures as well

{"MAGIC", "STRUCTURES"}

Any object with the deployable component has "Plant" as its Action string by default.

in STRINGS.ACTIONS

Spoiler
TOGGLE_DEPLOY_MODE =
{
  GENERIC = "Plant",
  GROUNDTILE = "Place Ground",
  WALL = "Build Wall",
  FENCE = "Build Fence",
  GATE = "Build Gate",
  TURRET = "Place",
  WATER = "Launch",
  PORTABLE = "Place",
  DEPLOY = "Deploy",
  DEPLOY_TOSS = "Toss",
  FERTILIZE_GROUND = "Fertilize Plot",
},

If these magnets are similar to the turrets I highly recommend using "Place" instead because of multi-language and consistency. The way you do this is inst:AddTag"deploykititem".

If you want to make it "Build" still, you can do something like (untested):

Spoiler
GLOBAL.STRINGS.ACTIONS.TOGGLE_DEPLOY_MODE.CONSTRUCTED_TURRET = "Build"
DEPLOY_strfn_old = GLOBAL.ACTIONS.DEPLOY.strfn
GLOBAL.ACTIONS.DEPLOY.strfn = function(act, ...)
	if act.invobject and act.invobject:HasTag"builtturret" then -- "builtturret" is a new tag
		return "CONSTRUCTED_TURRET"
	end
	return DEPLOY_strfn_old(act, ...)
end

To change size of an object, you can do something like:

Spoiler
local SCALE = x -- in units. for reference 4 = 1 pitchfork tile
inst.AnimState:SetScale(SCALE, SCALE)

But if I had to guess, generally the better practice is to upscale it in whatever program you're using to create the texture instead. Be sure to tweak the obstacle physics as well:

Spoiler
MakeObstaclePhysics(inst, radius, height) -- in units. for reference 4 = 1 pitchfork tile
Edited by oregu
  • Thanks 1

@oregu

Thank you very much! I’ve fixed these issues.

This code is useful:

{"MAGIC", "STRUCTURES"}
inst:AddTag("deploykititem")
But this code doesn’t work:
MakeObstaclePhysics(inst, 1, 3)
Then I realized the texture size might be too small.
I reviewed other mods.
I changed the texture size from 128x128 pixels to 512x512 pixels.
 
Everything works fine now.
8 hours ago, hall123 said:

@oregu

Thank you very much! I’ve fixed these issues.

This code is useful:

{"MAGIC", "STRUCTURES"}
inst:AddTag("deploykititem")
But this code doesn’t work:
MakeObstaclePhysics(inst, 1, 3)
Then I realized the texture size might be too small.
I reviewed other mods.
I changed the texture size from 128x128 pixels to 512x512 pixels.
 
Everything works fine now.

image.png.ddbdb4d10c5c8676fd047f012eecd6e2.png

MakeObstaclePhysics already exists in your code. I think you can only use MakeObstaclePhysics once, and then you have to modify the physics using other means.

Edited by oregu
  • Like 1

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