Passing variables into the RESTORE command
Blitz3D Forums/Blitz3D Beginners Area/Passing variables into the RESTORE command
| ||
hi folks. can anyone tell me how to pass a variable into a restore command? heres waht i wan to do. I have several map levels stored as data: .level 1 *data here* .level 2 *data here* etc.... and i have a function that reads the data and builds the level: function Build_level(level$) restore level$ <------note this point here!!! *read data and build level here* end function and when i call the level i do: currentlevel$ = "level1" Build_Level(currentlevel$) now, whenever i run the program, teh point where i have noted in the function, it gets and "expects 'end function'" error. How can i force blitz to recognise that 'level$' after the Restore command should be read as a variable? I have tried str(level$) but it makes no diff. Can anyone help? Thanks.... |
| ||
I don't think you can do this. I had to build a big select statement to handle the case of each levels label and restore them separately.select Level case 1 restore level1 case 2 restore level2 case 3 restore level3 etc end select |
| ||
You can't do that. I'm sure the reason why is because that would allow a variable with ANY VALUE to appear at runtime, which, if happened, would cause all sorts of garbage data to be read in or even cause the program to try to read memory outside of it's range (which would crash the program). So, like EpicBoy stated, you have to use a SELECT statement or similar method. |
| ||
and when i call the level i do: currentlevel$ = "level1" Build_Level(currentlevel$) BTW, here are some tips: First of all, Integers are processed much faster than Strings so it would be better to change your currentlevel$ variable to just currentlevel (an integer variable) and then just assign it a value of 1 instead of the string "level1". This speeds up the process of assigning the currentlevel variable AND it speeds up the process of passing it as a parameter in your function. Secondly, if you are not reusing the currentlevel variable elsewhere in your code, then don't bother assigning it a value (this takes time too). Just call your Build_Level function directly (i.e. Build_Level(1) ) Some may say that I'm wasting my time optimizing code that's infrequently executed, but there's no harm in forming a habit of thinking this way. |
| ||
Ah thanks guys, , the reason i assigned "level 1 " to the variable was so that i could change the variable value when i wanted to change level. I never realised that ints were processed faster than strings, but now i think about it, it makes sense. thanks folks :D ~V~ |