Loading data recursively

Started by
1 comment, last by JinJo 19 years, 11 months ago
This is anoying as its so simple and i done these type of problems at school and college a million times but now i just cant think properly. I am loading information from a text file, i''ll give a really cut done example: shaderlist { effect id num of shaders shaders { effect id sub shaders } } now the shaders loading code is easy, just a simple for loop. But subshaders are exactly the same as shaders, meaning they may have subshaders with subshaders etc etc and i wont be able to know how many. How do i load this data, do/while loop?, should i use a stack? I actually feel a little embarrassed here lol. Oh well the simple things you forget when your brains been working overtime.
Advertisement
Load the data recursively:
Shader readshader(filestream f):  read whatever header punctuation and discard  s : new Shader(read effect id)  n : read num of shaders  for i : 0..n-1    s->addSubshader(readshader(f))  read whatever footer punctuation and discard 


The filestream object (ifstream/ofstream in C++) is responsible for keeping track of the file position: the change is seen when you return from the recursion, so you can just pick up where you left off.

To purify your thinking, it may help to think of the shader list as itself being a "root" shader, whose children are the actual top-level shaders.
Cheers, I feel stupid, it should have been so obvious to me to recursively call a function, I even said in the question Recursively, just shows what your brain does to you sometimes when your working on something exciting, you forget some of the obvious/easy beginners stuff, or at least I have done so a few times now.

This topic is closed to new replies.

Advertisement