Author Topic: [Tutorial] Understanding paths and directories  (Read 12837 times)

Helepolis

  • Charisma!
  • *
  • O-ojousama!?
[Tutorial] Understanding paths and directories
« on: January 24, 2010, 10:24:08 AM »
Important note:
Ph3 and 0.12m seem to have discrepancy regarding uses of slashes in path names. Engine 0.12m holds traditional to backward slashes like Windows OS paths where ph3 allows forward slashes. Keep this in mind as it is impossible for me to take all examples as it will become confusing




Relative pathing
Most people might have encountered these already. Images or files being loaded from a certain directory using GetCurrentScriptDirectory or ./ and ../. Let us quickly look at an example:

Code: [Select]
let bossimg = GetCurrentScriptDirectory ~ "boss.png";
let bossimg = "./boss.png";

We're loading a sprite or image for our boss. These two lines of code do essentially the same thing. The ./ tells Danmakufu to look for a file named boss.png inside the current directory. GetCurrentScriptDirectory is basically the same thing.

Sometimes you may see people using let CSD = GetCurrentScriptDirectory; followed by let bossimg = CSD ~ "boss.png". It is called being 'a lazy programmer'. In order to avoid retyping the annoying long function, which has high risk to misstype as Danmakufu is case sensitive, they first create a variable called CSD and link it to GetCurrentScriptDirectory.

Some people might complain 'CSD' is vague and GetCurrentScriptDirectory is proper programming as the code speaks for itself. Regardless whether you're working in a team or not, comprehensive self-explaintory code would make it easier to read and maintain. So your call.

Now about that double-dot-backslash one. ../ tells Danmakufu to go back exactly one folder. Honestly, unless you're a sadist who likes to make his own life difficult, I don't see the use for fooling around with ../. But this is a tutorial right, so I need to explain you what it does. Fair enough.
Assume your current spell card is run from script/stage1/. Suddenly you decided you need another image as an effect, but it is inside another folder: script/effects/. You have decided to use relative pathing.

Code: [Select]
let bossimg = "./boss.png";
let effectimg = "../effects/particle.png"

This will load the image for our boss from the current 'stage1' directory and the particle image from the 'effects' folder. To show how the loading and change occurs:
Code: [Select]
- script/stage1/    ← Starts here
- script/           ← ../ Makes it go here.
- script/effects/   ← Finally enters the effects folder.
 


Bad examples:
Code: [Select]
let bossimg = GetCurrentScriptDirectory/sprites/boss.png       ← Not going to work because path is not between "" and missing ~ in between.
let particleMissile = ../../boss.png                           ← Unless your boss.png is somewhere 2 folders before, this is never going to load proper.




Absolute pathing
The most important thing to keep in mind when using absolute pathing is the naming of your folders. Renaming any folder will cause your absolute pathing to break. The name kind of tells you the path is 'absolute'. How about an example:

Code: [Select]
#include "script/library/functions.txt";
let titleScreen = "script/title.png";
let smokeParticle = "script/system/effects/smoke.png";

Simple as it is. And if you want to load things from another directory, you just type out the full path to that directory. Just keep in mind that the running directory for Danmakufu is always 'script' which we often call root script folder. This is why often you drop games from other authors into the script folder. Some people prefer to release the entire danmakufu folder so it becomes a 'extract and play' script.


Bad examples:
Code: [Select]
#include "script\library\functions.txt";                               ← Backslash is no longer valid in ph3. Use forward slash.
let faceMarisaSmile = "system/dialogue/marisaSmile.png;"               ← Root script folder is missing.
let fontSet = "C:/users/Helepolis/danmakufu/script/fonts/font.png";    ← Personal folders should be avoided at all costs. Use root script folder instead.




Which one to use?
Relative paths are more flexible to maintain when renaming folders. Absolute paths are more comprehensive to avoid confusion or mystery paths. Up to you to decide which one you feel comfortable with. Just try not to mix them together.


Tips and tricks:
- Avoid long pathnames. If possible place your danmakufu game in root of C: or other drive letter root. (So the root script folder becomes C:/danmakufu/script/
- Never use personal folders or such named folders. (Windows Users etc)
- Avoid overdoing subfolders, especially if they are not required. 'Keep it simple stupid'
- Plan early if your project might become large(r)


« Last Edit: May 16, 2016, 04:34:04 PM by Helepolis »

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: [Tutorial] Understanding paths and directories
« Reply #1 on: January 24, 2010, 10:49:15 AM »
Another rule.

With variables storing path names, use GetCurrentScriptDirectory ~ "stuff.txt"
With #include_function, use ".\stuff.txt"

For some reason, #include_function doesn't understand GetCurrentScriptDirectory and variables don't understand .\
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

Helepolis

  • Charisma!
  • *
  • O-ojousama!?
Re: [Tutorial] Understanding paths and directories
« Reply #2 on: January 24, 2010, 10:51:18 AM »
Ah yes, I knew I forgot something. Modifying the first post.

Edit: Modified! Thanks Blargel <3
« Last Edit: January 24, 2010, 11:07:20 AM by Helepolis »

Iryan

  • Ph?nglui mglw?nafh
  • Cat R?lyeh wgah?nagl fhtagn.
Re: [Tutorial] Understanding paths and directories
« Reply #3 on: January 24, 2010, 11:16:54 AM »
YES!  :D

Thank you so much for this.

Although...
Correct pathing:
  • let bossimg = "script\superscript\img\boss.png";

- We fixed the first line by placing the correct absolute path for the image.
... I don't think this is good pathnaming unless you release the entire th_dnh folder. For example, if I have already downloaded tons of projects from different people and want to organize them, I couldn't create a seperate Helepolis folder inside my script folder and shove superscript in there. In my opinion the only really good way would be:
  • let bossimg = GetCurrentScriptDirectory ~ "img\boss.png";
Old Danmakufu stuff can be found here!

"As the size of an explosion increases, the numbers of social situations it is incapable of solving approaches zero."

Helepolis

  • Charisma!
  • *
  • O-ojousama!?
Re: [Tutorial] Understanding paths and directories
« Reply #4 on: January 24, 2010, 12:06:14 PM »
Yes, that is up for debate currently. Because everybody has their opinion about this. Should I relate the idea of releasing full dnh folder to usage of absolute paths or else stick to  CSD method?

Any other suggestions, because absolute pathing is something hard to explain proper.


Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: [Tutorial] Understanding paths and directories
« Reply #5 on: January 24, 2010, 12:52:19 PM »
There's usually no good reason to do absolute pathing unless you're doing super complicated stuff. A good example is if the file you're including with #include_function is in turn also using #include_function.

To demonstrate why it would be confusing and absolute pathing would be better, let's take a look at this set up.

MyFolder
|
|---lib
|   |---functions.txt
|   |---morefunctions.txt
|
|---enemies
|   |---enmyFairy1.txt
|   |---enmyFairy2.txt
|   |---bossRumia.txt
|
|---stage.txt


Ignore the fact that there's obviously not enough stuff to make a good stage. Let's just take a look at what could go wrong with relative path naming.

In functions.txt, let's say that there is a line that says to include morefunctions.txt for whatever reason. With absolute paths, it's obviously what you would put: #include_function "script\MyFolder\lib\morefunctions.txt". However, what would you put if you wanted to use relative paths? Contrary to what you might expect, putting #include_function ".\morefunctions.txt" will not work. This is because when you include functions.txt in one of the enemy scripts, the relative path inside functions.txt makes it relative to the script that is including it, in which case Danmakufu will interpret the ".\morefunctions.txt" as "script\MyFolder\enemies\morefunctions.txt" and not find anything.

To fix that, we'd instead put #include_function ".\..\lib\morefunctions.txt" in functions.txt instead and when we include functions.txt into any of the enemies, it'll work perfectly fine. But then what if you want to include functions.txt into the stage? With the relative file path as it is now, when you call #include_function ".\lib\functions.txt" in the stage, it will in turn call ".\..\lib\morefunctions.txt" which will be interpreted as "script\lib\morefunctions.txt" and not be found.

As you can see, the only way to fix this would be for absolute file paths or a restructuring of your folders. Since organization is fairly important to making a big game, restructuring the folders may not be the best solution and absolute file paths would be the only way.
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

Helepolis

  • Charisma!
  • *
  • O-ojousama!?
Re: [Tutorial] Understanding paths and directories
« Reply #6 on: January 24, 2010, 01:01:04 PM »
Blargel's post is exactly the reason I personally dropped relative pathing in my Dance Contest game. Mainly because my spellcards are somewhere else and calling #include_function which are again calling include_functions sometimes as well.

But at the end, I know 100% sure my game is going to be released fully with the entire dnh folder. This gives the downloader no ability to organise things themselves in their own private dnh folder. But you cannot always please both sides. I think the debate is generally up to what you are releasing. Large games will more often released as full dnh folder ( CtC for example ) and smaller contest scripts will be released as script only.

This is exactly the part that I didn't include yet in the tutorial, because it is not always the same. And we want to have a global tutorial which makes it extra hard =S

Iryan

  • Ph?nglui mglw?nafh
  • Cat R?lyeh wgah?nagl fhtagn.
Re: [Tutorial] Understanding paths and directories
« Reply #7 on: January 24, 2010, 01:02:18 PM »
Should I relate the idea of releasing full dnh folder to usage of absolute paths or else stick to  CSD method?
This would be my suggestion.

Maybe add my reasoning for why not to do otherwise, reformulated. Like...


Quote
Unless you're planning to release an entire th_dnh folder, it is usually a better idea to use relative rather than absolute path naming. This is simply a matter of convenience; If someone downloads your script, he may want to put the folder somewhere else than the regular script folder. For example, let's say you have gathered a huge amount of scripts, and your regular danmakufu script folder is overflowing. To tidy it up, you put all works by Blargel in a seperate folder named "Blargel", and so on.

If Blargel was using absolute path names, this would make the directory descriptions in his scripts invalid, and the scripts wouldn't work anymore. If he uses relative path names instead, everything will work out fine.


...something like that.
« Last Edit: January 24, 2010, 01:05:39 PM by Iryan »
Old Danmakufu stuff can be found here!

"As the size of an explosion increases, the numbers of social situations it is incapable of solving approaches zero."

Azure Lazuline

  • Looooove!!
  • PM me for free huggles and love!
    • Entanma Project - indie game development
Re: [Tutorial] Understanding paths and directories
« Reply #8 on: January 24, 2010, 02:26:04 PM »
Well, in that example, I suggest having all scripts on the same "level" even if they're in different folders.

folder
 |
 |--functions
 |  |--functions.txt
 |  |--cutins.txt
 |  |--text.txt
 |
 |--img
 |  |--reimu.png
 |  |--bg.png
 |  |--fairy.png
 |
 |--script
    |--stage.txt
    |--reimu.txt
    |--fairy.txt


This way, if you want to call functions.txt, then GetCurrentScriptDirectory ~ "..\functions\functions.txt" will always work. It might seem slightly confusing at first, but I can keep things organized like that when I have ten different folders in two "levels" (the example is only one), so it's not as hard as it looks.

Re: [Tutorial] Understanding paths and directories
« Reply #9 on: January 24, 2010, 05:24:27 PM »
This seems like the correct place to post this, seeing that it IS the topic. When I put: #include_function"...Data.txt" in my Lyrica Single,  in the Lyrica Folder, The Image(s) nor do the Sound Effects show up.
If I am reading this right, from Data.txt I can but the Image Directories and such, just by putting GetCurrentScriptDirectory~"imgdirectory"
The Folder is set up like this:

Prismriver
|-data.txt   
|--se
|--|se     
|--bg img
|--|imgs          
|--Lyrica 
|--|Lyrica.png                         
|--| Lyrica 01.txt

But, when I put GSD~Lyrica\Lyrica.png" in the Data.txt, The image doesnt load, So am I doing this right?

EDIT:
Code: [Select]
//The Law
let GSD = GetCurrentScriptDirectory;
//Graphics
let Lunasa = GSD~"Lunasa\Lunasa.png";
let LunasaCutIn = GSD~"Lunasa\LunasaCutIn.png";

let Lyrica = GSD~"Lyrica.png";
let LyricaCutIn = GSD~"LyricaCutIn.png";

let Merlin = GSD~"Merlin\Merlin.png";
let MerlinCutIn=GSD~"Merlin\MerlinCutIn.png";

let PrisimriverCutin =GSD~"PrisimriverCutIn.jpg";

//BackGround Images

let MerlinBG = GSD~"BG_imgs\M1.png";
let LyricaBG = GSD~"BG_imgs\LY1.png";
let LunasaBG = GSD~"BG_imgs\LU1.png";
let All01BG = GSD~"BG_imgs\Prisim.png";
let All02BG = GSD~"BG_imgs\all.png";

//Functions
#include_function".\..\cutin.txt"

//Sounds

let BGM = GSD~"Levo Lution - Phantom Ensemble.mp3";

let ching = GSD~"se\kira0.wav";
let tsing = GSD~"se\kira1.wav";
let tsung = GSD~"se\kira2.wav";

let tsew = GSD~"se\tan0.wav";
let pew = GSD~"se\tan1.wav";
let pow = GSD~"se\tan2.wav";

let wosh = GSD~"se\water.wav";

//Lazer Sounds

let lazer0 = GSD~"se\lazer0.wav";
let lazer1 = GSD~"se\lazer1.wav";
let lazer2 = GSD~"se\lazer2.wav";

//Finished Sounds

//Animation

let attpose= 0;
let animation = 1;
let hover = 0;
let attframe = 0;

sub Load{
//Graphics
LoadGraphic(Lunasa);
LoadGraphic(LunasaCutIn);
LoadGraphic(Lyrica);
LoadGraphic(LyricaCutIn);
LoadGraphic(Merlin);
LoadGraphic(MerlinCutIn);
LoadGraphic(PrisimriverCutin);
LoadGraphic(MerlinBG);
LoadGraphic(LyricaBG);
LoadGraphic(LunasaBG);
LoadGraphic(All01BG);
LoadGraphic(All02BG);
//Sound Effects
LoadSE(ching);
LoadSE(tsung);
LoadSE(tsing);
LoadSE(tsew);
LoadSE(pew);
LoadSE(pow);
LoadSE(wosh);
LoadSE(lazer0);
LoadSE(lazer1);
LoadSE(lazer2);
//Finish Loading
}
}

Ok, I got rid of the Draw Stuff for space and irrelevance, but this is how It is set up, I changed GSD~Lyrica\Lyrica.png and such to GSD~Lyrica.png because after that it the image loaded

EDIT:Disregard the..."odd" string names for the sounds, I go on sound, Sound[1] and such is hard to remember which sound effect is which


BWAHAHAHAHAHAH! I GOT IT!
OH MY GOD I GOT THE CODE BOX TO WORK AGAIN!!!!
« Last Edit: January 24, 2010, 06:16:15 PM by Demonbman »

Stuffman

  • *
  • We're having a ball!
Re: [Tutorial] Understanding paths and directories
« Reply #10 on: January 24, 2010, 05:56:45 PM »
Added to the Tutorial Thread. (Even though it's a narrow subject, there wasn't really any clean place to merge it to.)

Re: [Tutorial] Understanding paths and directories
« Reply #11 on: January 24, 2010, 06:16:49 PM »
Disregard My post, I figured it out, I had no idea you could use the .\..\ method with GSD~

But alas I have stumbled upon another problem, I get two different error messages when I try to run a Plural Script, they have to do with Data.txt, because it says something is wrong with the Load sub, but in the single it works perfectly.
« Last Edit: January 24, 2010, 06:23:39 PM by Demonbman »

Drake

  • *
Re: [Tutorial] Understanding paths and directories
« Reply #12 on: January 24, 2010, 06:54:06 PM »
VERY IMPORTANT NOTE CAUTION CAUTION

Especially with all the retarded results I've seen in the latest contest thread, one thing definitely needs to be said. Because you guys need major help.

You can use absolute pathnames and still release it without anyone having problems. When you rar up your folder, include the "script" directory. Don't just give them the name of the main folder you're using. Anything that includes absolute pathnames must have the rar file go back to the "script" folder.

When people will open the rar file, they see the script folder. Then, extract it into the dnh folder because the "script" folder is already included. Thus, everything sorts itself. There is really no other option, unless you want a script\script\game folder and you're a complete idiot.


A Colorful Calculating Creative and Cuddly Crafty Callipygous Clever Commander
- original art by Aiけん | ウサホリ -

Re: [Tutorial] Understanding paths and directories
« Reply #13 on: January 25, 2010, 01:23:54 AM »
There is really no other option, unless you want a script\script\game folder and you're a complete idiot.

hahahahahah

Yes, that is up for debate currently. Because everybody has their opinion about this.

There is no debate. Use .\ and GetCurrentScriptDirectory if you do not include a copy of th_dnh.exe in your script. Do whatever you want if you do. No confusion.

Fujiwara no Mokou

  • Hourai Incarnate
  • Oh, so this trial of guts is for ME?
    • Profile