Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't build a bot with a program passed as a string #938

Closed
Anton-V-K opened this issue Mar 15, 2017 · 6 comments
Closed

Can't build a bot with a program passed as a string #938

Anton-V-K opened this issue Mar 15, 2017 · 6 comments
Assignees

Comments

@Anton-V-K
Copy link

@Anton-V-K Anton-V-K commented Mar 15, 2017

When a bot built with a firmware loaded from external file its program doesn't start.

Script sample:

void object::buildBot(int type, string firmware_file)
{
	int nLines = 0;
	file hFile();
	hFile.open(firmware_file, "r");
	string firmware = "";
	while(not hFile.eof())
	{
		firmware += hFile.readln() + "\n";
		++nLines;
	}
	hFile.close();
	message(nLines + " lines were loaded from " + firmware_file);
	
	object it = radar(BotFactory);
	goto(it.position);
	drop();
	move(-2.5);
	it.factory(type, firmware);
}

I tried the sample Shooter script from the very basic solution: the program runs fine if it is included in the script of the building grabber (so the code is like it.factory(type, "Shooter"), and same program doesn't start if it is loaded into the new bot from an external file.
Code battle mode with version 0.1.9 (Windows 7 64-bit).

@melex750
Copy link
Contributor

@melex750 melex750 commented Mar 15, 2017

Be sure your program has an 'extern' function.

.factory(WingedShooter, "/files/Shooter.txt"); // files folder

 // public program folder
.factory(WingedShooter, "/program/Shooter.txt");

 // private program folder
.factory(WingedShooter, "/savegame/<PLAYER_NAME>/program/Shooter.txt");

It would be nice if there were shortcuts like:
"%priv%/Shooter.txt"
"%publ%/Shooter.txt"

It would good if %lvl%, %cat%, and %chap% work with .factory() too.

@Anton-V-K
Copy link
Author

@Anton-V-K Anton-V-K commented Mar 15, 2017

@melex750 , thanks for the help.
This way the bot's program gets started fine.
I wonder why the program doesn't run when it is passed directly. This bug makes it impossible to run auto-generated (or auto-modified) programs on created bots.

@Anton-V-K Anton-V-K changed the title Can't build a bot with a program from external file Can't build a bot with a program passed as a string Mar 15, 2017
@melex750
Copy link
Contributor

@melex750 melex750 commented Mar 15, 2017

I can only think it might be a compile error or an error that stops the program.
This example works.

extern void object::Test()
{
	string prog = "extern void object::ProgramString()\n";
	prog += "{\n";
	prog += "    while(this.energyCell == null) wait(1);\n"; // wait for a battery
	prog += "    message(\"Hello World!\");\n";
	prog += "}\n";
	
	object botFactory = radar(BotFactory);
        botFactory.factory(WheeledGrabber, prog);
}

You can open the debugger during a code battle by:
Selecting the bot, click the camera mode button then the edit program button.
I usually pause the game first though.

@Anton-V-K
Copy link
Author

@Anton-V-K Anton-V-K commented Mar 16, 2017

@melex750 , thanks for looking into it.
You code works fine. And I also tried loading a smaller/simpler program from a file - it also works as expected.
So I guess something is wrong with my particular program, which is loaded into the bot ( I've attached it as
HuntFromAir.txt.zip ).
If I pass the path with the program directly into factory, it is loaded and works fine. However if I read the file into a string and then pass into factory, the program isn't loaded into the new bot (its programs list will be empty, except for a dummy New slot).
In the second case I also dump the program (after reading from file) back to disk to check its content - it is identical to the original program (except for an extra New Line character at the end).

@melex750
Copy link
Contributor

@melex750 melex750 commented Mar 17, 2017

I was able to reproduce this, it is basically a bug in Bot Factory code.
It's caused by line 29 of your HuntFromAir program.

If the string passed to.factory() contains .txt at the end of a line,
factory will try to use the entire string as a file path.
See line 413 autofactory.cpp.

@krzys-h
Copy link
Member

@krzys-h krzys-h commented Jun 3, 2017

@melex750 good catch, I didn't really realize that the string argument may contain more than one line :P

@krzys-h krzys-h self-assigned this Jun 3, 2017
krzys-h added a commit that referenced this issue Jun 3, 2017
@krzys-h krzys-h closed this Jun 3, 2017
B-CE added a commit to B-CE/colobot that referenced this issue Mar 6, 2018
B-CE added a commit to B-CE/colobot that referenced this issue Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants