Jump to content

Problems with the components.writeable. Pupup input.


Recommended Posts

I'm trying to create a popup with an input field in my mod. As an example, I took the name of beefalo, which uses the “writeable” component.

    inst:AddComponent("writeable")
    inst.components.writeable:SetDefaultWriteable(false)
    inst.components.writeable:SetAutomaticDescriptionEnabled(false)
    inst.components.writeable:SetWriteableDistance(TUNING.BEEFALO_NAMING_DIST)
    inst.components.writeable:SetOnWrittenFn(OnNamedByWriteable)
    inst.components.writeable:SetOnWritingEndedFn(OnWritingEnded)

When an action is triggered on an item, calling the popup window triggers an error.

local SIGN = _G.Action()
SIGN.id = "SIGN"
SIGN.str = "Add label"
SIGN.fn = function(act)
    if act.invobject then
        if act.target.components.writeable ~= nil then
            act.target.components.writeable:BeginWriting(act.doer)
        end
    end
end
AddAction(SIGN)

Error info:

[string "scripts/widgets/writeablewidget.lua"]:134: attempt to index local 'config' (a nil value)
LUA ERROR stack traceback:
    scripts/widgets/writeablewidget.lua:134 in (field) _ctor (Lua) <69-218>
    scripts/class.lua:191 in (upvalue) WriteableWidget (Lua) <181-194>
    scripts/screens/playerhud.lua:703 in () ? (Lua) <699-711>
    =(tail call):-1 in ()  (tail) <-1--1>
    scripts/components/writeable.lua:159 in (method) BeginWriting (Lua) <150-162>
    ../mods/hello-world/modmain.lua:79 in (field) fn (Lua) <63-87>
      
..]

I have no idea why an empty configuration parameter is passed to the class.

Edited by BK-201
New problem with components.writeable
Link to comment
Share on other sites

The writeable screen needs config to know how to set up the image, buttons, strings and such that appears when you're writing the text. Refer to scripts/writeables.lua to see examples of such config.

Here's the basics of what you need to add your own config, make sure the name of your prefab that has the writeable component matches with the layout name you're adding. Also, this code goes in modmain:

require("writeables").AddLayout("yourprefabnamehere", -- Make sure to change this
  prompt = "Write on the sign", -- I believe this is unused, but have something here anyway
  animbank = "ui_board_5x3",
  animbuild = "ui_board_5x3",
  menuoffset = GLOBAL.Vector3(6, -70, 0),
  
  -- Cancel writing button
  cancelbtn = {
    text = "Cancel", -- Text on the button, change as you please
    cb = nil, -- Function to run when the button is pressed, not usually need for cancel/accept
    control = GLOBAL.CONTROL_CANCEL, -- Key to press for controllers
  },
  
  -- Accept text button
  acceptbtn = {
    text = "Write it!",
    cb = nil,
    control = GLOBAL.CONTROL_CANCEL,
  },
})

A few other things are supported, like "middlebtn", "maxcharacters" and "defaulttext". You can see examples in writeables.lua, in the "beefalo" layout.

If you have more questions, feel free to ask!

Link to comment
Share on other sites

Perhaps I should have created a new topic, but I'll ask the question here. I received an error related to the writeable component when entering a world where there are already more than 400 days.

[string "scripts/components/writeable.lua"]:40: attempt to index field 'writeable' (a nil value)
LUA ERROR stack traceback:
        scripts/components/writeable.lua(40,1) in function '?'
        scripts/class.lua(43,1)
        scripts/components/writeable.lua(57,1) in function '_ctor'
        scripts/class.lua(191,1) in function 'cmp'
        scripts/entityscript.lua(604,1) in function 'AddComponent'
...]

To identify the cause, I disabled all modifications and simplified the only mod, leaving only the line for adding components:

AddPrefabPostInit("pocketwatch_recall", function(inst)
    inst:AddComponent("writeable")
end)

I tried to create a clean world, craft a few pocket watches, set points, etc. Further activation of the mod for such a one-day server did not trigger the error.

If I understand correctly, then this error is a client error, I see it in client_log.txt. The server itself continues to work until I press the exit button on the in-game error screen.

Link to comment
Share on other sites

38 minutes ago, _zwb said:

I think it's because you were trying to access components on client side

Thanks for the direction. I did a client check before adding the component and it helped.

    if not _G.TheWorld.ismastersim then
        return inst
    end

But I have a few other general questions. Firstly, why does adding components.writeable drive the client crazy in one situation, but in another situation it works without a crash? Secondly, how can you differentiate between necessary and unnecessary components on the client side? In some standard prefabs you can see that some components are added before the check, and others after. This ambiguity confuses me.

Or maybe I'm approaching this issue incorrectly.

Link to comment
Share on other sites

10 minutes ago, BK-201 said:

But I have a few other general questions. Firstly, why does adding components.writeable drive the client crazy in one situation, but in another situation it works without a crash? Secondly, how can you differentiate between necessary and unnecessary components on the client side? In some standard prefabs you can see that some components are added before the check, and others after. This ambiguity confuses me.

If you're hosting without Caves, you're both the client and the server, so even as a client you still have access to all the information that the server does. If you're hosting with Caves or you aren't the host, you only have access to the client information.

Components are almost always server-only, there's some exceptions but that's a pretty good rule of thumb. In your case it looks like the game crashed because the writeable replica was missing. If you don't know what replicas are, to put it very simply, they're simplified components that also exist on the client, the server-side component communicates through them. I assume because your component was also client-side, then the game didn't make a replica.

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