Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Public files, factory and code battle
I have 2 problems. 
1 : I can't make "factory" works properly if I don't load and exec 1 public file. If I use a private file, I can't reach my class definition, used by both my builder and my new robot. 

2 : I would like to play against a friend without having to check for functions name. I can't make a simulation with my IA against itself because it (obviously) try to declare function already declared in the public space.

So, how does work public programs and how does factory select the function ?

I also can't exactly know which function factory is using. It not seems to read it in the code saved in the editor, but a file in my "public" folder, but... which one !? Because I have a lot of backups with the same functions / class defined inside :p

To play a simulation of my IA against itself, I could do 2 files with a different extern function, but I have to know how works the "public space", and how to avoid name collisions in code battle.

Thank you for your help,
Love from la France
All public names in CBot (both for functions and classes) are shared between teams. We know this can cause name collisions, especially if you try to load the same programs for both teams, and it will be fixed in the future. See There is also for when you just want to use classes to organize things in the code and not for sharing data between robots.

Using factory() with function name actually just makes it run a program that looks something like that:
extern void object::Autogenerated()
so it follows normal rules of calling public functions. An alternative is to provide a filename to load (I actually don't even remember where does it load the file from myself, but I think it's the "files" directory, the same one that is used when reading/writing files with CBOT) or full program code.
A public function is a function declared with keyword "public". It has to be loaded into one bot so other bots can use it. You probably saved a program as public, but this is different. This makes program available to all player profiles in Colobot, but this program is not loaded by default in any bot, which is why you can't use functions defined inside. An example of public function defined inside empty program:

extern void object::RechargeLib()

public void object::Recharge()
   object o = radar(PowerStation);
   while(energyCell.energyLevel < 1)

You can define as many public functions as you want in one program source, thus creating a kind of library. After you load the program inside any bot, you can use those functions in other programs.

factory() lets you use one of three ways of specifying program to run:
1. Name of a public function to run
2. Name of a file containing source code of the program to run
3. Complete source code of the program to run

You can check in-game CBot manual or online manual for more information.
"After three days without programming, life becomes meaningless."
~The Tao of Programming
Thank you for your help. I will try this Smile

I just have to rename my class to a no generic name.

BOT1 :
robotClass ==> class robot
Builder1 ==> new robot()

BOT2 :
Builder2 ==> new robot()
Love from la France

Forum Jump:

Users browsing this thread: 1 Guest(s)