loading code

Started by
1 comment, last by samosa 21 years, 7 months ago
im working on my basecode(basically making nehe''s code more custom to myself) anyway, i was wondering when you want to execute code from a textfile(you know, so you dont have to recompile everytime you want to make a change) can you just save the code into a string and then execute that, for instance, have a function that initializes my players stats, can i just put
  
int playerhealth = 0;
  
into a text file then load that and use the string so that the actual program will execute that code when run or do i have to make if/switch statements and determine if certain variables are true or false and then base it on that, i hope im being clear, if not i will try to clear anything up on request, thank you in advance ,Matt -= kill one your a murderer, kill thousands your a conquerer =-
-= kill one you're a murderer, kill thousands you're a conquerer =-
Advertisement
I see what your trying to do, basical you want to define infomation in a text file and use that to make small changes to the program to reduce the hardcoding.

Basical, how you want to do it, load text and execute isnt gonna happen without building some kinda scripting system.

all you really need is a text file which, apon running of your program, get read in and the values used, and because its your own system you can define it how you want, so basical say your doing player health and armor could do have a txt file with

parmor 50
phealth 100

in it, when you start your problem, open the text file, read in the first line, get the number from it, set your players armor, repeat for health, simple isnt it.
and as your code will only read what you tell it to, after those line you could put what you want and then change your code later to read it when you are ready for it.

However, thats just one idea, i belive the system is called ''data driven programming'' so you might wanna google that
On a more basic note, Samosa, you need to learn the difference between a ''compiled'' language and an ''interpreted'' programming language.

Now, directly altering code from a file read isn''t always possible. Let''s take C++ as an example of ''compiled'': You edit your code, then the compiler works on it and creates extremely streamlined machine code to make an executable file. Now since you''re not going to code a full-fledged C++ compiler as part of your application (I hope! ) no C++ program could read -code- from a file and execute it. The same goes for other ''compiled'' languages.

There are ''interpreted'' languages out there. Several versions of Basic (Not VB, however) will actually read their files on-the-fly and send machine code to your OS as it reads the file. As a rule of thumb, those languages are slower than ''compiled'' languages. BUT some might enable you to alter code on the fly.

I know for a fact that LISP is capable of sending chunks of its own code to its own functions as parameters and alter it on the fly. It''s no surprise that it''s a good language for university AI projects.


== Teachers and leaders should remember: Give not the people what they want, give them what they need! ==
=^.^= Leaders and teachers should remember: It is best to offer others what they Need, not what they Want.

This topic is closed to new replies.

Advertisement