Page 1 of 2

Replace a text in a specific position in a user window

Posted: Fri Aug 26, 2022 3:29 am
by Puckster
I have the following code that generates tabular output in a user window.

Code: Select all

function WindowUpdate()

  roomWindow:clear()
  echo("roomWindow", "1234567890123456789012345678901234\n")
  echo("roomWindow", "Fruit     Color     Size     Price\n")
  echo("roomWindow", "Apple     Red       Large    100  \n") 
  echo("roomWindow", "Orange    Orange    Small    123  \n")

end
I want to replace a specific element in that tabular output. For example, replace the price of an orange with 234 or change the size of an apple from large to small.

I have been fighting with various methods of using moveCursor(), insertTest(), selectSection(), replace(), etc.. with zero luck.

Any thought on how to do this? Seems like it should be straightforward, but I can't make it work.

Thanks!

Re: Replace a text in a specific position in a user window

Posted: Fri Aug 26, 2022 5:25 am
by Vadi
You could just change the echo you are outputting, is that what you mean?

Re: Replace a text in a specific position in a user window

Posted: Fri Aug 26, 2022 12:46 pm
by demonnic
Yeah, you're already clearing the window then echoing to it in that example, and that's honestly the easiest way to deal with changing displays like that. You should be able to use roomWindow:echo instead of echo("roomWindow"

You might want to look into the MDK, and specifically the TableMaker: https://github.com/demonnic/mdk/wiki/fT ... A-Examples

Re: Replace a text in a specific position in a user window

Posted: Fri Aug 26, 2022 12:51 pm
by Puckster
Yeah, I realized later last night after I posted this that the example is a much simplified one from what I actually need. I created it to reduce the variables while I figured out the solution.

What this is for is for a list of mobs in a room. Sometimes the whole table will be replaced (i.e. entering a room). The table could have any length and would actually be sparse. I am not having trouble when I need to draw the whole table, but there are times when only one thing changes (say, a character changes position). I don't want to re-build and re-draw the entire table, especially if there are a dozen mobs in the room.

Re: Replace a text in a specific position in a user window

Posted: Fri Aug 26, 2022 1:02 pm
by Vadi
Capture all the mobs, and then redraw the table once. Try it out - you'll find the performance of that quite satisfying.

Re: Replace a text in a specific position in a user window

Posted: Fri Aug 26, 2022 1:04 pm
by Puckster
The TableMaker in MDK looks quite interesting. I would need to change my strategy a bit, but it looks like there is a good bit of flexibility. I'll tinker with that a bit.

Re: Replace a text in a specific position in a user window

Posted: Fri Aug 26, 2022 1:07 pm
by Puckster
@Vadi, that was my fallback plan. I am using GMCP, and I get a GMCP.Room.Char.Upate event that only contains the updated information, so I was hoping to be surgical, and just replace those few (often one) values, but if I have to redraw the whole table, so be it. I didn't really think there would be a performance hit. I just thought it would be more elegant (and easy with how I read selectSection and replace work). :-)

Re: Replace a text in a specific position in a user window

Posted: Fri Aug 26, 2022 1:13 pm
by Puckster
@Dominic two questions that I don't see in the MDK wiki

1. can you remove rows from a table or would you just create a new table without that row and redraw the whole thing.
2. can a single element in the table be independently formatted or is it only by column? I need to format certain fields based on content.

Re: Replace a text in a specific position in a user window

Posted: Fri Aug 26, 2022 7:21 pm
by demonnic
There is an option to delete a row, add a row, or even replace a row. Full API docs are at https://demonnic.github.io/mdk/current/ ... ftext.html

The formatting for the column in terms of width and alignment is all set, but if you just mean you want to change the color of the items in there, you can put your own formatting inside the text. So if you need to make it red instead of the usual green, you can pass "<red>Entry" instead of "Entry". The tablemaker will just continue on like you had not done so.

Or "<128,0,0>" or "#aa0000" or whatever the proper formatting for the c/d/hecho variant you chose is. But iirc it defaults to cecho.

Re: Replace a text in a specific position in a user window

Posted: Sat Aug 27, 2022 6:08 pm
by Puckster
@demonnic aha, thanks! Similar to how I would do it if I were just rendering strings or such. Slick (and obvious now that you point it out ... ).

Thanks for all the help!