EnglishGermanFrenchRussianItalianSpanish
Log inRegister
 
[SOLVED!][SCRIPT] Infinite loop hell
Post new topic Reply to topic
View previous topic :: View next topic
Author Message
lostProfitssssArrgh





Joined: 22 Aug 2017



modified
PostPosted: Mon, 25. Sep 17, 06:50    Post subject: [SOLVED!][SCRIPT] Infinite loop hell Reply with quote Print

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:
...
* 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:
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
Back to top
View user's profile Send private message
Litcube





Joined: 20 Oct 2006
Posts: 4235 on topic
Location: Canada
Thank you for registering your game
PostPosted: Mon, 25. Sep 17, 08:00    Post subject: Reply with quote Print

Code:
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.


_________________


Litcube's Universe Wiki
Back to top
View user's profile Send private message
lostProfitssssArrgh





Joined: 22 Aug 2017



PostPosted: Mon, 25. Sep 17, 08:32    Post subject: Reply with quote Print

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... D'oh

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

Back to top
View user's profile Send private message
Cycrow
Moderator (Script&Mod)
Moderator (Script&Mod)



Joined: 15 Nov 2004
Posts: 20512 on topic
Location: London
Thank you for registering your game
PostPosted: Mon, 25. Sep 17, 10:41    Post subject: Reply with quote Print

Its generally better to do a reverse loop.

so for an array, something like

Code:

$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


_________________
My Scripts | MY X3TC Scripts | X3 Plugin Manager | Custom Gui
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic Reply to topic
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum
Control Panel
Login Data
The time now is Thu, 19. Jul 18, 21:19

All times are GMT + 2 Hours

[ Disclaimer / Impressum ] | [ Privacy Policy / Datenschutz ]

Board Security

Copyright © EGOSOFT 1989-2018
Powered by phpBB © 2001, 2005 phpBB Group
Template created by Avatar & BurnIt!
Debug: page generation = 0.07503 seconds, sql queries = 18