Coding: Difference between revisions

Jump to navigation Jump to search
1,804 bytes added ,  00:11, 15 November 2022
Added a section on how to import external scripts
(Added a section on how to import external scripts)
Line 77: Line 77:
|-
|-
|}
|}
[[#top|Back to Top]]
==⚙ Importing External Scripts==
The RoboCo python interpreter searches a few places for libraries by default. The main location is <your steam directory>\steamapps\common\RoboCo\RoboCo_Data\StreamingAssets\Robocode\python\Lib. The game's code libraries are stored here.
Adding a file to the Lib directory will allow you to import it from any script in your robots, however the Lib folder could get reset in the future, and it's inconvenient to set up libraries in the folder over the Scripts folder where the scripts for all of your robots (including ones from the workshop) are stored.
The Scripts folder is located far within the user's Documents folder, and is not searched for libraries by default. The following code segment adds the Scripts folder to the list of folders to look for libraries in.
<pre>
import os, sys
path = str(os.path.expanduser('~'))+'\\Documents\\My Games\\Roboco'
if not os.path.isdir(path): path = str(os.path.expanduser('~'))+'\\My Documents\\My Games\\Roboco'
if os.path.isdir(path):
    for file_path in os.listdir(path):
        for folder in os.listdir(os.path.join(path, file_path)):
            f_path = os.path.join(path, file_path, folder)
            if(os.path.isdir(f_path) and folder == 'Scripts'):
                if not (f_path in sys.path): sys.path.append(f_path)
else:
    print('*Error finding Roboco folder*')
</pre>
After the path is added, the files within the Scripts folder can be imported like any other file would be.
Importing a script from another known directory is simple, too.
For example, on my computer (your steam directory might be different), this code will allow me import a file from the RoboCo game folder.
<pre>
import sys
sys.path.append('C:\Program Files (x86)\Steam\steamapps\common\RoboCo')
</pre>


[[#top|Back to Top]]
[[#top|Back to Top]]
1

edit

Navigation menu