Notifications
Clear all

[Closed] %n meaning for format

I’m kind of lost with format command. I can’t seem to concatenate strings or change arguments. I’ll explain better.

Maxscript reference says very little about it. What do %n mean, exactly?

It says you should print something to the listener with

format "name is %
" obj.name 

But what if I want to write the obj.name before the “name is”. For example obj.name “has this property”?

I’ve tried

format obj.name "has this property"

but that doesn’t work

4 Replies

% is the value of the array given after the quotation marks
is like hitting enter key in notepad.

here is an example script.

a="12"
b=#("lemonaide stand","delicious")
clearlistener()

format "I would like to buy % lemons for my %
They are %
" a b[1] b[2]

Example below, if you want to put numbers in you need to convert them to string values.

format "%, is the way
 to
 %, set this %
" "This" "That" (5 as string)

DaveWortley: actually with format, you don’t have to:

format "int: %, float: %, point3: %, matrix: %, function: %
" 1 1.0 x_axis (matrix3 1) mod

As mentioned above,
is a newline character with \ being the escape character (thus when you want to use single \ in string, you have to escape it as well). The complete list of escape sequences is in the maxscript reference under String Literals.

The % in the format() function is a sort of “dummy” or “placeholder” and it indicates to the format() function that that place will be replaced with an argument that must be passed after the string.

The arguments will be passed in order, so the first argument will replace the first %, the second argument will replace the second % and so on.

 Basically the format() function using % will be constructed like:  
format <string> <arguments>

If you use 1 % symbol then the function expects 1 argument, if you use 2 % symbols then the function expects 2 arguments.

 One of the advantages of this is that you actually do not need to  convert the arguments to strings in order to build the final string.
 
 For example, the following code expects to have 2 arguments after the  <string>. The "

” at the end is used to put an EOL mark at the end of the string, it is not needed in all cases but in this examples so you see each format() in a different line in the Listener.

format  "% %
" 1 2
     format "% %
" "A" "B"
     format "% %
" objects.count lights.count

If you don’t pass as many arguments as % symbols are in the then the function will throw an exception.

 As each % symbol will be replaced with the corresponding argument, you  can build more interesting strings, for example: 
format "Objects:%  Lights:%
" objects.count lights.count
 Objects:35  Lights:12

This combined with other Escape Sequences allows you to build very complex formatted strings.