[SOLVED!][SCRIPT] Infinite loop hell

The place to discuss scripting and game modifications for X³: Terran Conflict and X³: Albion Prelude.

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

Post Reply
lostProfitssssArrgh
Posts: 161
Joined: Tue, 22. Aug 17, 13:14

[SOLVED!][SCRIPT] Infinite loop hell

Post by lostProfitssssArrgh » Mon, 25. Sep 17, 06:50

Hi!

I've been :headbang: from infinite loop errors while trying to add a check to prevent multiple ships from going to the same station. Different methods were tried, the latest (and possibly cleanest) being this:

Code: Select all

...
* number of scrapers
$scrapers = array alloc: size=0
if not $home -> get local variable: name=$whLocVar
|   append [THIS] to array $scrapers
|   $home -> set local variable: name=$whLocVar value=$scrapers
else
|   $scrapers = $home -> get local variable: name=$whLocVar
|   append [THIS] to array $scrapers
|   $home -> set local variable: name=$whLocVar value=$scrapers
end

...

* Run through station list, get ware info for next station in loop
while $count < $numStat
|   $station = $stations[$count]

|   * Skip current station if homebase, duh!
|   do if $station == $home
|       continue

|   * Check if another scraper is servicing the current station
|   $serviced = 0
|   $scrapers = $home -> get local variable: name=$whLocVar
|   for each $scraper in array $scrapers

|       do if [THIS] == $scraper
|           continue

|       $orders = $scraper -> get orders string
|       $stationStr = $station -> get name
|       $stationFound = find position of pattern $stationStr in $orders

|       if $stationFound > -1
|           $serviced = 1
|           break
|       end
|   end

|   do if $serviced
|       continue
The infinite loop check systematically returns a -non-consistent- error during:

Code: Select all

for each $scraper in array $scrapers
...
What am I *not* seeing here..?

Thank you,
-lpa

EDIT : The first ship runs fine, the infinite loop check triggers when starting the second ship.
Last edited by lostProfitssssArrgh on Mon, 25. Sep 17, 08:33, edited 1 time in total.

User avatar
Litcube
Posts: 4254
Joined: Fri, 20. Oct 06, 19:02
xr

Post by Litcube » Mon, 25. Sep 17, 08:00

Code: Select all

while $count < $numStat 
|   $station = $stations[$count] 

|   * Skip current station if homebase, duh! 
|   do if $station == $home 
|       continue 
If $station == $home even once, you will infinite loop. There's no change to count between continue and while.

lostProfitssssArrgh
Posts: 161
Joined: Tue, 22. Aug 17, 13:14

Post by lostProfitssssArrgh » Mon, 25. Sep 17, 08:32

Litcube wrote:If $station == $home even once, you will infinite loop. There's no change to count between continue and while.
I knew it had to be something along those lines... :doh:

Removing the home-base from the station list before entering the loop will do the trick, and actually be cheaper too. Marking as solved.

Thanks a million!
-lpa

Cycrow
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 22227
Joined: Sun, 14. Nov 04, 23:26
x4

Post by Cycrow » Mon, 25. Sep 17, 10:41

Its generally better to do a reverse loop.

so for an array, something like

Code: Select all

$i = size of array $array
while $i
  dec $i
  $value = $array[$i]
end
this way, if you have any continues, you will have already adjusted the counter.

it also helps if you ever what to remove anything from the array in the loop.

For loops that you want to do in first to last order, you could simply use the reverse array command, then do a reverse loop

Post Reply

Return to “X³: Terran Conflict / Albion Prelude - Scripts and Modding”