Is Counter an alternative to do_while for looping

The place to discuss scripting and game modifications for X4: Foundations.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

thomasbkdk
Posts: 179
Joined: Wed, 8. Jan 20, 07:34
x4

Is Counter an alternative to do_while for looping

Post by thomasbkdk »

I have put this together that works

Code: Select all

	<set_value name="$CountSships" exact="4" />	
		<do_while value="$CountSships gt 0 ">

			xxxxx	
			xxxxx

			<set_value name="$CountSships" exact="$CountSships - 1"/>
		<do_while>
But would this below be an alternative that would work?

Code: Select all

	<set_value name="$CountSships" exact="4"  />	
		   <do_all exact="$CountSships" counter="$i">

			xxxxxx
			xxxxxx

	           </do_all>
Or can Counter perhaps be used even without the variable?

Code: Select all

		   <do_all counter="4">

			xxxxxx
			xxxxxx

	           </do_all>
AMD 3700X|GSKILL 2x32GB 3600C17|MSI X570 UNIFY|MSI RX 6800|SAMSUNG NVME 990 PRO|RAZER TARTARUS PRO|AOC AGON AG273QX 2560x1440@165|STREAM DECK XL+PEDAL|CORSAIR K95 PLATINUM RGB
j.harshaw
EGOSOFT
EGOSOFT
Posts: 2148
Joined: Mon, 23. Nov 15, 18:02

Re: Is Counter an alternative to do_while for looping

Post by j.harshaw »

with do_all, exact tells the engine how many times you want it to do everything within the do_all node. counter just gives you the index of any particular iteration. helps to tell specific iterations apart or compare values between them.

so:

Code: Select all

<set_value name="$somelist" exact="[0.1, 3000, 20]"/>
<do_all exact="$somelist.count" counter="$counter">
  <do_if value="$somelist.{$counter} gt @$highestvalue">
    <set_value name="$highestvalue" exact="$somelist.{$counter}"/>
    <set_value name="$highestvalueindex" exact="$counter"/>
  </do_if>
</do_all>
<do_if value="$highestvalue?">
  <debug_text text="'highest value is: %s at index: %s'.[$highestvalue, $highestvalueindex]"/>
</do_if>
should print "highest value is: 3000 at index: 2"

it'll first record 0.1 as $highestvalue with index 1,
then overwrite that value and record 3000 as $highestvalue with index 2,
then ignore the third value since it'll fail the condition within the loop.

do note that do_all doesn't necessarily have to be used in loops. it can simply be an instruction to the engine to do everything within the node. limited usage since that's implicitly true in most cases (all but one, i think).
also note that you could also use the tidier do_for_each in most cases where you need to iterate over a number of entries in a list or table.
j.harshaw
EGOSOFT
EGOSOFT
Posts: 2148
Joined: Mon, 23. Nov 15, 18:02

Re: Is Counter an alternative to do_while for looping

Post by j.harshaw »

heh. just realized i didn't answer your question. given the example that you gave, you can absolutely use do_all in place of do_while, but you'll need exact, not counter.

Code: Select all

<do_all exact="4">
  stuff
  more stuff
  clean stuff up
</do_all>
which will do stuff, more stuff, and clean stuff up in that exact order exactly four times unless you stick a blocking action in there and it's interrupted.

Return to “X4: Foundations - Scripts and Modding”