This site requires JavaScript, please enable it in your browser!
Greenfoot back
Psychpsyo
Psychpsyo wrote ...

2016/8/4

getting the scenarios directory

Psychpsyo Psychpsyo

2016/8/4

#
I'd like to know how I can get the directory where the scenario (or .jar file) itself is saved on my computer so I can save files there. An no, it doesn't have to run on the greenfoot site or the browser.
Game/maniac Game/maniac

2016/8/4

#
Search for your projects name using your computers file browser
danpost danpost

2016/8/4

#
Usually, if you get the target path of your greenfoot application shortcut (on your desktop, it will continue '/Greenfoot/scenarios'. You should be able to right click on the shortcut and select 'Properties' to find the target path.
Psychpsyo Psychpsyo

2016/8/5

#
That's not what I meant. I know how to find files on my computer. But I want to know if there is a way that the scenario itself can detect where it's saved (even if I move it somewhere else) so it can save stuff in that directory.
danpost danpost

2016/8/5

#
Psychpsyo wrote...
That's not what I meant. I know how to find files on my computer. But I want to know if there is a way that the scenario itself can detect where it's saved (even if I move it somewhere else) so it can save stuff in that directory.
You do not have to use complete paths. That is, you can use a local path which starts at the directory your scenario is located. To clarify, for images and sounds in your scenario, you are only putting the name of the file because the method called ('setImage', 'setBackground', and 'playSound') adds the additional folder name to the name given it. For example, when using 'setImage("player1.png")', the 'setImage method uses the path 'images/player1.png'. At any rate, there is really no need to use a complete path. Besides, if you do, you cannot move the scenario without breaking it (you cannot share it in any way or relocate it within your file directory).
Psychpsyo Psychpsyo

2016/8/5

#
Then: How do I use a "local path"?
danpost danpost

2016/8/5

#
Psychpsyo wrote...
Then: How do I use a "local path"?
Just use the name of the file itself and make sure the file is located in the scenario folder. That way, if you move or copy the scenario, the file moves with it and the local path does not change.
Psychpsyo Psychpsyo

2016/8/5

#
danpost wrote...
Psychpsyo wrote...
Then: How do I use a "local path"?
Just use the name of the file itself and make sure the file is located in the scenario folder. That way, if you move or copy the scenario, the file moves with it and the local path does not change.
But I don't want the file to be located in the scenario folder. I want the file to be located in the folder where the scenario folder is located.
danpost danpost

2016/8/5

#
Psychpsyo wrote...
But I don't want the file to be located in the scenario folder. I want the file to be located in the folder where the scenario folder is located.
I believe that you can access the parent folder by using the double dot symbol -- "../filename.txt" (an initial backslash may be needed). However, I remind you again that you must then move both the file and the scenario folder together and that sharing would be out of the question without appropriate changes to the code.
Psychpsyo Psychpsyo

2016/8/5

#
Thanks! That's what I wanted to know.
Psychpsyo Psychpsyo

2016/8/8

#
danpost wrote...
Psychpsyo wrote...
But I don't want the file to be located in the scenario folder. I want the file to be located in the folder where the scenario folder is located.
I believe that you can access the parent folder by using the double dot symbol -- "../filename.txt" (an initial backslash may be needed). However, I remind you again that you must then move both the file and the scenario folder together and that sharing would be out of the question without appropriate changes to the code.
How would I have to change the code so it runs a .jar file too?
danpost danpost

2016/8/8

#
Psychpsyo wrote...
How would I have to change the code so it runs a .jar file too?
I guess you would have to check both local paths (the one with "../" and the one without). Something like this:
1
2
java.io.File file = new java.io.File("filename.txt"); // referenced as in 'jar' file
if (!file.exists()) file = new java.io.File("../filename.txt"); // referenced as in greenfoot application
Of course, you can have:
1
import java.io.File;
and then have
1
2
File file = new File("filename.txt");
if (!file.exists()) file = new File("../filename.txt");
You need to login to post a reply.