Resource Loader and Manager - Madour/pyNasNas GitHub Wiki
The resource loader and manager is a very important part of the framework, as it bring at lot great things.
The static class ns.Res acts as a resource loader and manager.
Loading resources
Before using any resource, you need to load them. To do so, simply call ns.Res.load() once at
the start of the game.py file.
The resource loader will look into your assets folder and sub-folders and load any resource
it finds (currently loaded : png, jpg, bmp, ttf, tmx)
If you want to check which resources were loaded, you can call ns.Res.print_tree() which will
print a representation of your asset folder and sub-folders with all the resources it loaded.
For example :
img/res_tree.png
If you want to have a custom name for your resource folder, you can change the value of
(not supported yet)
The loader will look into the folder specified by this constant. By default ns.RES_DIR.ns.RES_DIR = "assets".
Using resources
After loading, you can access a specific resource this way : ns.Res.path_to.my_res.filename.
For example, to get the snake Texture from previous example : ns.Res.gfx.monsters.snake;
to get the level TiledMap : ns.Res.maps.level.
The way you access your resources depends on how you organize them in your assets folder.
The resource manager also provides shortcuts to access a specific type of resource directly without typing the whole path.
ns.Res.Texturesto access textures (e.g.ns.Res.Textures.snake)ns.Res.Fontsto access fonts (e.g.ns.Res.Fonts.arial)ns.Res.Mapsto access tmx maps (e.g.ns.Res.Maps.level)
You can also iterate over Res or any of its shortcuts above. They behave like a dictionary.
For example :
import NasNas as ns
ns.Res.load()
# printing names of files and folders found at the root of 'assets'
for name in ns.Res:
print(name)
# printing all textures filenames and sizes found in 'assets' folder and sub-folders
for name, resource in ns.Res.Textures.items():
print(name, resource.size)
To use the resource manager efficiently, there are some naming rules to follow for your assets:
- All your files and folders names should follow variable naming rules (no special characters other than underscore '_' , name should not start with a number, no spaces)
- None of your files or folders names should start with an underscore
- The names
Textures,MapsandFontsare reserved (although you can usetextures,mapsandfonts)
<- [Window and Game loop]] ](/Madour/pyNasNas/wiki/[[Scenes,-Layers-and-Camera) ->