Returning Next Textbox ID

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Returning Next Textbox ID

Post by kylekrack »

After looking through the documentation, I'm not finding any way to access textbox data, other than using textbox line() to append the lines to a string. I'm using this for custom textbox scripts. I'm planning on narrowing the width of the game screen, so default textboxes aren't working out.

First of all, is there any way to resize the textbox slices while maintaining text wrap and not screwing everything up? The script I'm currently sporting uses textbox line() to copy lines from textboxes defined in custom into a text slice. I'm not sure if that's the bet way to do it. The wrap is kinda messed up. Sometimes words go off the box a bit and I'm not sure how to prevent that.

The code is below. The keypress handler checks if a global, displayed text, is greater than -1, then calls "advance formatted text(id)" if it is.

Code: Select all

plotscript, show formatted text, id=0, begin
    variable(root,box,txt,line,height)
    
    clear string(str:set)
    clear string(str:temp)
    
    # sli:textroot is a container slice 
    # on the HUD slice collection
    root := lookup slice(sli:textroot,HUD)
    
    for(line,0,7) do(
        textbox line(str:temp,id,line)
        concatenate strings(str:set, str:temp)
        $str:set+" "
        if(string length(str:temp) > 0) then(
            height += 1)
    )
    
    height := (string length(str:set) / 29) * 10 + 18
    box := create rect(240,height)
    set padding(box,4)
    set parent(box, root)
    
    txt := create text
    set wrap(txt,true)
    set parent(txt,box)
    set slice text(txt,str:set)
    
    clear string(str:set)
    clear string(str:temp)
    
    # This makes the keypress not allow 
    # movement, NPC interaction, etc.
    displayed text := id
end

script, advance formatted text, id, begin
    variable(root)
    root := lookup slice(sli:textroot,HUD)
    
    displayed text := -1
    
    # the first child of root should be 
    # "box" from the above script
    free slice(slice child(root,0))
end
But anyway, my main question is how I might go about getting a reference to the "after" field of a given textbox. I can't find anything for this in the documentation. I'm assuming there's no direct way to do it, if there's a way at all. In addition, if there's anything I've done suboptimally I'd like to know; I was mostly winging it with this script.
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

There's no current way to get the next textbox field (textbox line is the only command to get textbox data), and I'm a little uneasy about it due to the complications of the tag conditions and the ability to trigger a script after instead, the details of which might change after the textbox rewrite. However, we could add a command that returns just the next textbox number, and returns 0 if none or if the box is set to trigger a script instead, and which ignores any tag conditions. That would be much less of a hazard to add.
First of all, is there any way to resize the textbox slices while maintaining text wrap and not screwing everything up?
You mean the text slice belonging to a builtin textbox? The lines are separated by newline characters; if you want the text to reflow when changed in size then you need to replace those newlines with spaces (just like you're already doing, but use "set slice text" on the existing slice instead of a new one).


Instead of trying to calculate the height of the text slice yourself, you should let the engine do it. After each command such as "set wrap" or "set slice text" the text slice height is recalculated, as is the width if it's not set to wrap. So set the rect slice height afterwards:

Code: Select all

    for(line,0,7) do(
        textbox line(str:temp,id,line)
        concatenate strings(str:set, str:temp)
        $str:set+" "
    )
   
    box := create rect(240,0)  # only need to specify width
    set padding(box,4)
    set parent(box, root)
   
    txt := create text
    set wrap(txt,true)
    set parent(txt,box)   # This sets the width of the txt slice
    set slice text(txt,str:set)
    
    set slice height(box, slice height(txt) + 18)
You might also want to add newlines between lines of text under certain conditions (by writing $str:set+"\n"), such as if a line is blank or short or has several trailing spaces, otherwise it always gets smushed up.


Also, you can write "free slice children(root)" instead of "free slice(slice child(root,0))", which avoids a potential error
Last edited by TMC on Sat Oct 29, 2016 4:16 am, edited 1 time in total.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

Alright I put that height fix into place. Works much better this way, thank you! Not being able to check for a next box is pretty limiting. The way I dealt with it in Overgrowth was by just creating a long switch statement that spit out strings and variables like "cont := boolean" and "next := num". It worked, but it was a lot of code, and insteads were pretty messy. I think I'll probably try to alter the default textbox slices. I'm gonna go try some of that stuff and get back.
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Ah, so in that case a command just to get the next textbox in the chain won't be enough.
Rather than reimplement all the textbox conditionals yourself, why not let the engine do it? You can just hide the whole textbox with "set slice visible", and then draw the textbox in a custom way. You can use suspendboxadvance and advancetextbox if you want to take over control of advancing the textbox. I don't think any set of script commands for querying textbox data could ever get close to the convenience of doing it that way. And I can't think of any reason that you would want to read any of that data unless you were actually advancing through the textboxes normally... or maybe scripting your own textbox editor/browsing tool, I guess.
Last edited by TMC on Sun Oct 30, 2016 1:08 am, edited 1 time in total.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

I'll do what you say and use the default textbox slices. It's definitely not worth scripting all that functionality, especially considering this is a small project. Thanks again.
My pronouns are they/them
Ps. I love my wife
Post Reply