Code4Life CONNECT function

Hi there. I have a little issue with the CONNECT function while on the DIAGNOSIS module.
I search for a sample with the highest Health value to retrieve it from the module.

On the first round it works perfectly well, the robot collects the molecules and it brings these to the LABORATORY.

Here is the output from the Std and Err

Standard Error Stream:

ID: 1
CONNECT 1 generated by code cerr << "CONNECT " << bestSampleId << endl; //bestSampleId = 1

Standard Output Stream:

CONNECT 1 generated by code cout << "CONNECT " << bestSampleId << endl; //bestSampleId = 1

On the second round the best sample is 5.

Standard Error Stream:

ID: 5
CONNECT 5 generated by code cerr << "CONNECT " << bestSampleId << endl; //bestSampleId = 5

Standard Output Stream:

CONNECT 1 generated by code cout << "CONNECT " << bestSampleId << endl; //bestSampleId = 5

Why does it try to connect to sample 1? Variable bestSampleId = 5 which can be seen from the cerr


You must output only one command per turn (iteration of the infinite while loop), else your commands will be desynchronised from the inputs you’re receiving and viewing in the viewer/console.

1 Like

Hi Thibaud

Actually I am doing a GOTO DIAGNOSIS, CONNECT 1, GOTO MOLECULES, CONNECT B (4x), GOTO LABORATORY in one while loop. After that it goes to the beggining of while(1) goes though the for () functions and I try to do the same process as before but this time with 5 as ID, but instead of doing CONNECT 5 it does CONNECT 1.
Do you suggest of doing the following:

  • while(1) loop does GOTO DIAGNOSIS… end of while(1)
  • while(1) loop does CONNECT 1… end of while(1)
  • while(1) loop does GOTO MOLECULES … end of while(1)
  • and so on?



To make a somewhat weird parallel, imagine you were driving but instead of seeing with your eyes continuously, I was sending you screenshots of what you see every second, would you take your driving decisions for the next kilometre from the first screenshot only?
You wouldn’t. Instead you would check the first screenshot, take a decision (keep on, slow down, turn, etc), see next screenshot, another decision etc…

It’s the same here. Does it make sense?

1 Like

Yes, that makes sense. I will switch the mindset to this structure for the coding.

Thank you very much!

I have a similar problem but I’m quite sure that I only print one action per iteration in the infinite while loop.
Still the output I get from my debugging statements seem to be wrong. My bot is still standing at DIAGNOSIS but the target variable has value MOLECULES. Can there be another reason in my case?

Ok I got my bug. I didn’t realize that you can also get the target value of the other bot in the game.