Manipulating Text Slices

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

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

Manipulating Text Slices

Post by kylekrack »

I couldn't find another thread for this, so I figured I'd make one.

I'm currently scripting a battle system for a game and I'm having trouble displaying the hp in the corner and the name of the enemy. Right now I have the blank text slices loaded up in a slice collection along with everything else in the battle scene. I figured this would be the easiest way to do it, rather than having to use show string at().

Here's what the script says now:

Code: Select all

	col := load slice collection(0)
	append number(lookup slice(sli:hpcurrent,col),get hero stat(0,stat:hp,current stat))
I assume I'm not supposed to treat it as a string since it's a slice, but I don't know how else to add things to it. Do I not use append number()? Also how would I do the same with an enemy's name?
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 »

That's right, text slices and strings are completely different objects. To set the text of a textslice, you use the set slice text command, after filing a string with the content you want. For example:

Code: Select all

$0 = "Uglar"
set slice text(lookup slice(sli:enemy1,col), 0)  # string ID 0

$0 = ""
append number(0, get hero stat(0,stat:hp,current stat))
set slice text(lookup slice(sli:hpcurrent,col), 0)
Also, if you're using nightlies and you want to piece together long strings, you might find string sprintf useful. Example:

Code: Select all

string sprintf(0, $0="HP: %d/%d", get hero stat(0,stat:hp,current stat), get hero stat(0,stat:hp,maximum stat))
set slice text(lookup slice(sli:hpcurrent,col), 0)
Last edited by TMC on Thu Oct 02, 2014 6:57 am, edited 1 time in total.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1243
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

I didn't even see that command. Thank you very much.
My pronouns are they/them
Ps. I love my wife
User avatar
kylekrack
Liquid Metal Slime
Posts: 1243
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

I added more stuff to Stand. I figured out text slices, but making them children of each other doesn't work right, which is obvious if you go into a battle. They move and I'm not sure why.
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 »

I had a brief look. The battle system is a cool project.

Why are you triyng to parent the text slices to one another? Is it get them to appear end-to-end? (Assume you just haven't set the anchor positions yet in the slice editor.) That will work, If you want to do that, just set up the parenting in the slice editor directly rather than with a script. The reason that the slcies move when you change the parent with setparent is that using setparent does not behave the same as setting a slice's parent in the slcie editor (with the shift key). A slice's x/y position is relative to its parent (or whichever edge or corner that might be). When you setparent a slice its position goes from being relative to the top left of th screen to relative to the new parent. On the other hand in the slcie editor when you reparent the offset is recalculated to keep the slice in the same position relative to the screen. Hint, you can press ctrl+F4 in game to investigate the slice tree.

I think I mentioned the alternative in another thread, "string sprintf"
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7660
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

Here is a handy little helper script if you want to reparent a slice without changing its current position.

Code: Select all

script, set parent keep pos, sl, new parent, begin
  variable(oldx, oldy)
  oldx := slice screen x(sl)
  oldy := slice screen y(sl)
  set parent(sl, new parent)
  set slice screen x(sl, oldx)
  set slice screen y(sl, oldy)
end
You can use this command like this

Code: Select all

set parent keep pos(myslice, new parent)
And it will be reparented pretty much the same way reparenting works in the slice editor.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

To the Script dump!
User avatar
kylekrack
Liquid Metal Slime
Posts: 1243
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

I could never figure out how to set parents in the slice editor. Now I know! That will be incredibly helpful in the future. Thank you, I will fix this.

EDIT: I want to set the numbers as parents because the number of digits will potentially change as the players HP increases or decreases.
Last edited by kylekrack on Wed Oct 08, 2014 10:36 pm, edited 1 time in total.
My pronouns are they/them
Ps. I love my wife
User avatar
Mystic
Metal Slime
Posts: 322
Joined: Wed Jul 23, 2008 4:32 am

Post by Mystic »

I feel like we could make an actual hss or whatever file with a bunch of things, maybe organized by type... I know I have a bunch of slice functions I'm using in Silhouette that could be made more generic...
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

kylekrack wrote:I could never figure out how to set parents in the slice editor. Now I know! That will be incredibly helpful in the future. Thank you, I will fix this.

EDIT: I want to set the numbers as parents because the number of digits will potentially change as the players HP increases or decreases.
I assumed that you did it like that because you wanted the ability to make the numbers different colours (in which case, that's the best way to do it). If you don't want different colours, you can just use a single slice and "string sprintf", as I showed further up this thread.
Mystic wrote:I feel like we could make an actual hss or whatever file with a bunch of things, maybe organized by type... I know I have a bunch of slice functions I'm using in Silhouette that could be made more generic...
Maybe! The 3rd party HSI did this long ago but is now fairly obsolete. If you just have some possibly useful but not generalised scripts, it's still nice to put them in the script dump or link to them from there (i.e. make the scripts freely available for use, so that someone else can clean them up and put them on the wiki).
Post Reply