Jump to content


Photo

Do...Until Not?


  • Please log in to reply
9 replies to this topic

#1 andykuo1

andykuo1

    GMC Member

  • GMC Member
  • 29 posts

Posted 06 April 2012 - 12:38 AM

I have a question. I tried using do{....} until(....) with a twist, which is adding not in until.
it kind of looks like this:

ini_open("gxy_setup.ini");
a = 0;
do
{star = instance_create(x,y,obj_star);
star.star_adr = ini_read_string("star_adr","star"+string(a),"error");
if star.star_adr == "error"
{a+=1;
do
{star.star_adr = ini_read_string("star_adr","star"+string(a),"error");
a+=1;}
until(star.star_adr!="error");
}
a+=1;}
until(a == star_count);

Everything works fine until I added the 2nd do...until in there. My question is how do you use do{something} until(something does not = something)? Did I place the exclamation point in the wrong area? It didn't give me an error. Instead, it went into a never-ending loop, freezing the application. I hope to have an answer soon. Thanks in advance.
  • 0

#2 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 06 April 2012 - 12:53 AM

No this check is fine, your inside do loop is probably messed up. E.g., say "a" is like 9551 and your star has "error". Do you really have an infinite number pf starX in keys in your ini? Cause it'll keep going up and up and up until it finds something, and if your thing isn't infinite it'll keep going up and up and up...
  • 0

#3 Blake

Blake

    GMC Member

  • GMC Member
  • 443 posts
  • Version:GM:Studio

Posted 06 April 2012 - 12:53 AM

The way you have done it is correct, as far as I know. Infinite loops in this case are most likely caused by the until statement never returning true. So, if the second do loop is indeed the problem, this probably means that star.star_adr is always equal to "error".
  • 0

#4 andykuo1

andykuo1

    GMC Member

  • GMC Member
  • 29 posts

Posted 06 April 2012 - 01:03 AM

Thanks for the quick replies. Yeah, the ini has to be starX, since I have a changing number of stars. I'll check in the ini file, if there are I left it open. And also, I've added a star_number to keep track how many stars there are, to limit the number of times the loop checks. Supposedly, if a star is destroyed, perhaps star2, it skips star2 and heads for star3, if such exists. Again, I will check if I left it open. At first I thought untils cant use nots, so thank you for clearing that up. Thank you once again for helping. Ill post back if I succeed or encounter another problem.
  • 0

#5 andykuo1

andykuo1

    GMC Member

  • GMC Member
  • 29 posts

Posted 06 April 2012 - 01:18 AM

I am confused. This is the entire setup:
Create Event:
__________________________________________________________________
ini_open("gxy_setup.ini");
star_count = ini_read_real("main","star_count",1);
i = 0;
do
{star = instance_create(x,y,obj_star);
star.star_adr = ini_read_string("star_adr","star"+string(i),"error");
if star.star_adr == "error"
{i+=1;
do
{star.star_adr = ini_read_string("star_adr","star"+string(i),"error");
i+=1;}
until(star.star_adr != "error");}
i+=1;}
until(i == star_count);
__________________________________________________________________
"gxy_setup.ini":
__________________________________________________________________
[main]
star_count=8

[star_adr]
star0="save/Andromeda Galaxy/star0/star0.ini"
star1="save/Andromeda Galaxy/star1/star1.ini"
star2="save/Andromeda Galaxy/star2/star2.ini"
star3="save/Andromeda Galaxy/star3/star3.ini"
star4="save/Andromeda Galaxy/star4/star4.ini"
star9="save/Andromeda Galaxy/star9/star9.ini"
star10="save/Andromeda Galaxy/star10/star10.ini"
star11="save/Andromeda Galaxy/star11/star11.ini"
__________________________________________________________________

I read them through. Everything should work. No variables end up with "error" unless the starX does not exists and defaults to "error". In that case, the 2nd do/until should find the next existing starX. As each star is found i is added up. Once equal to star_count, it should stop. Now I once again pulled out the 2nd do/until, and it works. Proving the star_count function works. I don't get how the 2nd part shouldn't work. I would appreciate your reply. Thanks in advance.

Edited by andykuo1, 06 April 2012 - 01:19 AM.

  • 0

#6 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 06 April 2012 - 01:25 AM

The problem is your INSIDE do loop does not have the star_count condition to stop! So add an || i == star count to your != "error"
  • 0

#7 andykuo1

andykuo1

    GMC Member

  • GMC Member
  • 29 posts

Posted 06 April 2012 - 01:33 AM

The problem is your INSIDE do loop does not have the star_count condition to stop! So add an || i == star count to your != "error"


It still goes into a continual loop....I still don't get what's causing this. Thanks for helping though.
  • 0

#8 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9260 posts
  • Version:Unknown

Posted 06 April 2012 - 01:35 AM

Another potential issue I noticed is that you're using "i" as the variable in the indexes (i.e. you have i=0 for star0, i=11 for star11, etc.), but then you're checking to see if it's equal to star_count before ending your outer loop. Think about what that would do in this case. Your code finds star4 and everything's fine. It then checks for star5, which doesn't exist, so it triggers the inner loop. The next star it finds is star9. But wait! It's skipped star8! So if i never equals 8, the loop will continue on forever if there's no star8 in your file.

You should instead use one variable for the key indexes and a separate one for the number of created stars, since clearly they don't always match up.

ini_open("gxy_setup.ini");
star_count = ini_read_real("main", "star_count", 1);
i = 0;
count = 0;
do {
  star = instance_create(x,y,obj_star);
  star.star_adr = ini_read_string("star_adr", "star" + string(i), "error");
  if (star.star_adr == "error") {
    i += 1;
    do {
      star.star_adr = ini_read_string("star_adr", "star" + string(i), "error");
      i += 1;
    }
    until(star.star_adr != "error");
  }
  i += 1;
  count += 1;
}
until(count >= star_count);

Here, "i" is still the key index, but "count" is a measure of the actual number of entries you've read.

-IMP
  • 1

#9 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 06 April 2012 - 01:38 AM

Hrm... oh I see. Change the == to >= in both cases. I didn't know there were gaps in the middle.

Edit: Bah, or use IMP's method

Edited by greep, 06 April 2012 - 01:39 AM.

  • 0

#10 andykuo1

andykuo1

    GMC Member

  • GMC Member
  • 29 posts

Posted 06 April 2012 - 01:47 AM

THANK YOU IMP! That solved it. Thank you everybody that helped too. Now I can progress! Thank you for all the fast replies.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users