Colobot Forum - International Colobot Community
I'm working on a code for clearing the waypoint course - Printable Version

+- Colobot Forum - International Colobot Community (https://colobot.info/forum)
+-- Forum: [Archive] New forum (2015-2019) (https://colobot.info/forum/forumdisplay.php?fid=76)
+--- Forum: Colobot: Gold Edition Basics (https://colobot.info/forum/forumdisplay.php?fid=58)
+---- Forum: General (https://colobot.info/forum/forumdisplay.php?fid=59)
+---- Thread: I'm working on a code for clearing the waypoint course (/showthread.php?tid=1044)



I'm working on a code for clearing the waypoint course - {No}Mad - 07-19-2018

Hey. This is {No}Mad here. I recently started playing COLOBOT: Gold Edition because I remember fooling around with it as a kid but not really understanding it. Now I've returned to it to teach myself a bit of coding and for the fun challenge of it.

I created a code for the first challenge of getting the robot to follow the waypoints, instead of just moving a set number of meters, but I ran into an issue at the end where there were no more waypoints, and I'm trying to get it to move the appropriate meters to the left of it to cross the finish line. I just haven't figured it out yet.

The code is as follows:


Quote:extern void object::Move()
{
 
 object item;
 
 while (true)
 
 {
  item = radar(WayPoint);
  goto(item.position);
  
  if (false)
  {
   break;
  }
 }
 
 turn(90);
 move(20);
 
}

The only issue is that, when I use it, it doesn't work. As soon as the bot runs out of waypoints, it says "Unknown Object", and stops dead in it's tracks.

I'm going to continue working on it, but suggestions would be, you know, nice. Though I do plan to figure these out on my own as I get more experienced.


RE: I'm working on a code for clearing the waypoint course - krzys_h - 07-19-2018

First of all, the if (false) condition is not doing anything - you are saying that the code in the following block should execute if the expression false evaluates to true which will never happen. What you probably intended to write is if(item == null), which means execute if the item was not found. You will also have to move the whole if instruction between the radar and goto lines (because you can't go to somehing that doesn't exist).


RE: I'm working on a code for clearing the waypoint course - {No}Mad - 07-20-2018

Hey, I tried exactly what you said, and it worked perfectly! I don't entirely understand why the if block has to go between the radar and goto lines, but I guess it's just because the flow of the programming language doesn't work like regular old English. Or maybe I just need to practice more and reimagine it in my head.

I mean, it kind of makes sense. When I wrote it, I was trying to say "You're objective is an item. While it rings true there is an item, look (radar) for the item WayPoint and goto it. If it is no longer true that there is a WayPoint, stop (break) what you're doing and turn 90 degrees to the left, and move 20 meters out."

Instead, it should have been, "You're objective is an item. While it rings true that you are looking for an item, look (radar) for the item WayPoint. If you do not see any more WayPoints (item == null), stop (break) looking for it, turn 90 degrees to the left, and move 20 meters out. Now, goto the first WayPoint that you see."


RE: I'm working on a code for clearing the waypoint course - krzys_h - 07-21-2018

Programs always execute in order from top to bottom (with the exception of loops etc.). It goes more like this: "Look for a WayPoint and store it under the name item. If the item was not found, stop looking. Otherwise, go to the item and then return to the beginning of the program. After you're done with the WayPoints, turn 90 degrees left and move 20 meters forward."

With the if after goto, it goes like this:
"Look for a WayPoint and store it under the name item. Go to the item. (You didn't check if the 'item' is found yet, it may not even exist! Trying to go to something that does not exist would cause an error) If the item was not found, stop looking, otherwise, return to the beginning of the program. After you're done with the WayPoints, turn 90 degrees left and move 20 meters forward."

If you translate exactly what you said into code, it looks something like this (note that I used some weird construct in while() to keep it looking exactly like you said):
Code:
object item;
while((item = radar(WayPoint)) != null) {
    goto(item.position);
    // no need for any if(false) - while statement ends automatically if the condition is not true at the end of the loop
}
turn(90);
move(20);
and this should work as well