recurring code query

Freeform discussion about anything related to modding Transcendence.
Post Reply
relanat
Militia Captain
Militia Captain
Posts: 600
Joined: Tue Nov 05, 2013 9:56 am

Tue Jun 12, 2018 1:53 am

Currently using hacky code to show the varying number of lines of text in a screen description in the Taipan GodShip mod.

Currency type names from (typFind '$) are listed in a custompicker screen.
The screen description shows the current player balance for every available currency and updates in OnPaneInit.
Actions are used to change the value of the currency the cursor is on.

But the number of currencies varies; 2 in SOTP, 4 in EP, etc, so the number of lines in the screen description can also vary.
This <OnPaneInit> code works:

Code: Select all

(block Nil
	(scrSetData gScreen 'currency (scrGetListEntry gScreen))
	(setq currList (typFind '$))
	(setq listLength (count currList))

	(scrSetDesc gScreen
		(cat "Enter amount of " (scrGetData gScreen 'currency) " to add or subtract.\n"
			(if (@ currList 0)
				(cat (typGetProperty (@ currList 0) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 0))) "\n")
			)
			(if (@ currList 1)
				(cat (typGetProperty (@ currList 1) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 1))) "\n")
			)
			(if (@ currList 2)
				(cat (typGetProperty (@ currList 2) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 2))) "\n")
			)
			(if (@ currList 3)
				(cat (typGetProperty (@ currList 3) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 3))) "\n")
			)
			(if (@ currList 4)
				(cat (typGetProperty (@ currList 4) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 4))) "\n")
			)
		)
	)
)
but there is probably a neater way of doing it.
Possibly 'enumWhile' until the number of display list entries (listLength) is reached? Or 'for' doing the same thing? Another possibility might be 'loop'?

Anyone know the easy way to do this? The number of currencies displayed in the screen description needs to match the number of currencies displayed in the list which is set by typFind '$.

Whole dockscreen code:

Code: Select all

	<Dockscreen UNID="&dsD789GodCurrencyDockscreen;"
			name=		"Currency"
			type=			"customPicker"
			nestedScreen=	"true"
			>

		<List
			rowHeight="44"
			>
			(map (typFind '$) theCurrency (typGetProperty theCurrency 'name))
		</List>

		<Panes>
			<Default
				showCounter="true"
				>
				<OnPaneInit>
					(block Nil
						(scrSetData gScreen 'currency (scrGetListEntry gScreen))
						(setq currList (typFind '$))
						(setq listLength (count currList))

						(scrSetDesc gScreen
							(cat "Enter amount of " (scrGetData gScreen 'currency) " to add or subtract.\n"
								(if (@ currList 0)
									(cat (typGetProperty (@ currList 0) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 0))) "\n")
								)
								(if (@ currList 1)
									(cat (typGetProperty (@ currList 1) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 1))) "\n")
								)
								(if (@ currList 2)
									(cat (typGetProperty (@ currList 2) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 2))) "\n")
								)
								(if (@ currList 3)
									(cat (typGetProperty (@ currList 3) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 3))) "\n")
								)
								(if (@ currList 4)
									(cat (typGetProperty (@ currList 4) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList 4))) "\n")
								)
							)
						)
					)
				</OnPaneInit>

				<Actions>
					<Action id="actionMillionCurrency">
						(block Nil
							(plyCredit gPlayer (scrGetData gScreen 'currency) 1000000)
							(scrShowPane gScreen "Default")
						)
					</Action>

					<Action id="actionZeroCurrency">
						(block Nil
							(plyCharge gPlayer (scrGetData gScreen 'currency) (plyGetCredits gPlayer (scrGetData gScreen 'currency)))
							(scrShowPane gScreen "Default")
						)
					</Action>

					<Action id="actionAddCurrency">
						(block Nil
							(plyCredit gPlayer (scrGetData gScreen 'currency) (scrGetCounter gScreen))
							(scrShowPane gScreen "Default")
						)
					</Action>

					<Action id="actionSubtractCurrency">
						(block Nil
							(plyCharge gPlayer (scrGetData gScreen 'currency) (scrGetCounter gScreen))
							(scrShowPane gScreen "Default")
						)
					</Action>

					<Action id="actionBack" cancel="1">
						(scrExitScreen gScreen)
					</Action>
				</Actions>
			</Default>
		</Panes>

		<Language>
			<Text id="descDefault">
			</Text>

			<Text id="actionMillionCurrency">Add 1 [M]illion</Text>
			<Text id="actionZeroCurrency">Set To [Z]ero</Text>
			<Text id="actionAddCurrency">[A]dd Currency Amount</Text>
			<Text id="actionSubtractCurrency">[S]ubtract Currency Amount</Text>
			<Text id="actionBack">[B]ack</Text>
		</Language>
	</Dockscreen>
Attachments
currency use.jpg
currency use.jpg (93.13 KiB) Viewed 342 times

NMS
Militia Commander
Militia Commander
Posts: 472
Joined: Tue Mar 05, 2013 8:26 am

Tue Jun 12, 2018 4:14 am

You're on the right track. You could do something like this:

Code: Select all

(block (desc (currList (typFind '$)))
	(for i 0 (- (count currList) 1)
		(setq desc (cat desc (typGetProperty (@ currList i) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList i))) "\n"))
	)
	(scrSetDesc gScreen desc)
)
But the more elegant lisp style is:

Code: Select all

(scrSetDesc gScreen (apply cat
	(map (typFind '$) currency
		(cat (typGetProperty currency 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer currency)) "\n")
	)
))

relanat
Militia Captain
Militia Captain
Posts: 600
Joined: Tue Nov 05, 2013 9:56 am

Mon Jun 18, 2018 3:43 am

Thanks. It works beautifully. I've used the 'for' example as I'm still working through how the 'apply' example works.

The 'for' function runs the code and then runs it again for every value up to the 'to' value. And 'i' increases value by one on every cycle.
Every cycle adds another line to 'desc' using the next currency in the list. This builds the screen description to the full number of currencies.

Other 'for' examples use 1 to n as the values but here you have used 0 to n so that the value of 'i' can be used to refer to the position of the list entries. Very nice.

Couple of questions:
When the 'for' function is cycling through the values is each of these cycles what is called an 'iteration'?
And I assume that 'i' can only be a numerical value. Not alpha-numeric? It that correct?

Final code:

Code: Select all

(block (currAmounts desc
	(currList (typFind '$))
	)

	(scrSetData gScreen 'currency (scrGetListEntry gScreen))

	(setq currAmounts 
		(for i 0 (- (count currList) 1)
			(setq desc (cat desc (typGetProperty (@ currList i) 'name) ": " (fmtNumber 'integer (plyGetCredits gPlayer (@ currList i))) "\n"))
		)
	)

	(scrSetDesc gScreen
		(scrTranslate gScreen 'descDefault {
			currName: (fmtCurrency (scrGetData gScreen 'currency) True)
			currAmounts: currAmounts
			})
	)
)

<Text id="descDefault">
	Enter amount of %currName% to add or subtract.\n
	%currAmounts%
</Text>

NMS
Militia Commander
Militia Commander
Posts: 472
Joined: Tue Mar 05, 2013 8:26 am

Sat Jun 23, 2018 9:57 am

You're welcome.
relanat wrote:
Mon Jun 18, 2018 3:43 am
Couple of questions:
When the 'for' function is cycling through the values is each of these cycles what is called an 'iteration'?
And I assume that 'i' can only be a numerical value. Not alpha-numeric? It that correct?
Yes to both.

relanat
Militia Captain
Militia Captain
Posts: 600
Joined: Tue Nov 05, 2013 9:56 am

Sat Jul 07, 2018 1:18 am

Ahh. Got it. I was trying to figure out how 'apply cat' was affecting the 'map' function. But it doesn't. Not in the least. 'map' generates a list of four entries (in this example) and THEN 'apply' tells the 'cat' function to go to work on those four list entries.
So all four list entries get con'cat'enated no matter what their format is. By using '\n' in the list entries the output is stacked into a vertical list.

Many thanks for this and also for your and OxABCDEF's posts in the "list forming query 2" topic which helped to clarify this for me.
Attachments
recurring code use.jpg
recurring code use.jpg (76.28 KiB) Viewed 260 times

Post Reply