Difference between revisions of "Talk:StringUtil Script"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>Jbezorg
(Created page with "<source lang="papyrus"> ; Simple C++ like sprintf ; ; ; %% a percent sign ; %s a string ; %d a signed integer ; %f a floating-point number ; ; @asSource String - ...")
 
imported>DAb
 
Line 50: Line 50:
</source>
</source>
~ [[jbezorg]] Mar 17 2013
~ [[jbezorg]] Mar 17 2013
== How to display the actor IDs in game ==
<source lang="papyrus">
; Simple way to display the actor IDs properly by removing the not-printable < and > characters.
Debug.Notification("Magic effect started on actor " + StringUtil.Substring (akTarget, StringUtil.GetLength (akTarget) - 11, 8) )
</source>

Latest revision as of 14:25, 28 August 2018

; Simple C++ like sprintf
;
; 
; %%    a percent sign
; %s    a string
; %d    a signed integer
; %f    a floating-point number
;
; @asSource String - The format string containing the % tokens
; @asReplacements String Array - The stack of replacements for the % tokens
String Function sprintf(String asSource, String[] asReplacements )
	Int iStart = 0
	Int iEnd = 0
	Int iIndex  = 0
	String sReturn = ""
	String sOperator = ""
	
	iEnd = StringUtil.Find(asSource, "%", iStart)
	If ( iEnd == -1 )
		Debug.Trace( "You are passing an unformatted string into sprintf: " + asSource )
		Return asSource
	Else
		While ( iEnd != -1 && iIndex < asReplacements.Length )
			sOperator = getNthChar(asSource, iEnd + 1)

			If ( sOperator == "%" )
				sReturn += Substring(asSource, iStart, iEnd) + "%"
			ElseIf ( sOperator == "s" )
				sReturn += Substring(asSource, iStart, iEnd) + asReplacements[ iIndex ] as String
			ElseIf ( sOperator == "d" )
				sReturn += Substring(asSource, iStart, iEnd) + asReplacements[ iIndex ] as Int
			ElseIf ( sOperator == "f" )
				sReturn += Substring(asSource, iStart, iEnd) + asReplacements[ iIndex ] as Float
			Else
				Debug.Trace( "Improper format for sprintf: " + asSource )
				Return asSource
			EndIf

			iIndex += 1
			iStart = iEnd + 2
			iEnd = StringUtil.Find(asSource, "%", iStart)
		EndWhile

		sReturn += Substring(asSource, iStart)
	EndIf
	
	Return sReturn
EndFunction

~ jbezorg Mar 17 2013

How to display the actor IDs in game[edit source]

; Simple way to display the actor IDs properly by removing the not-printable < and > characters.

Debug.Notification("Magic effect started on actor " + StringUtil.Substring (akTarget, StringUtil.GetLength (akTarget) - 11, 8) )