Hello, it's Metalclunch here, and I've got a pretty simple and straightforward question: How would one implement a scripting language? Prefferably Python. Thing is, I'm pretty much a beginner, I know about the if, else if, for, while statements and functions.

Okay. Here goes. I've made a very simple text-based game. I've also got a .cfg (Configuration) file all set up, so I know (atleast) something. My game is around 400 lines of text (I have a LOT of functions), and I just feel that all the If and Else If stuff is simply cluttering it up and making it un-readable.

This is why I want to implement a scripting language. Here's some of my ideas.

[B]In C++.[/B]
if (ComputerStamina < 15) {
	while (ComputerStamina < 35) {
		ComputerStamina++; }
}

[B]In the Python scripting file.[/B]
(begin), #Begin the script.
    (lt, "$ComputerStamina", 15), #If the global variable ($) is less than 15, continue.
    (while|lt, "$ComputerStamina", 35), #While ComputerStamina is less than 35, continue.
    (value_add, "$ComputerStamina", 1), #Add the value of 1 to ComputerStamina.
(end), #End the script.

I hope you understand what I aim to. Then, the game would read the infromation from the .py script file. Does anyone have any ideas how to implement this kind of system? Any kind of links to good articles or something?

Thanking ahead,
metalclunch.

Links,
Scripting Language
SUMMARY:

The origin of the term was similar to its meaning in "a movie script tells actors what to do": a scripting language controlled the operation of a normally-interactive program, giving it a sequence of work to do all in one batch.

Scripting language

Hm, thanks for that. I'll try to crack it later, for now, my mind is full of other kind of ideas. :P

if (ComputerStamina < 15) {
	while (ComputerStamina < 35) {
		ComputerStamina++; }
}

Maybe I am missing the point, but why would anyone want to do this? It's just this, only much slower:

if (ComputerStamina < 15)
     ComputerStamina = 35;

And what are you trying to do? Write Python in C++, then run your original C++ program through your scripting program so it reduces the number of if statements?

The while loop is because I wanted to add another function what you'd be able to modify in the .cfg file I made. while (ComputerStamina < RestoreComputerStamina)

I'm trying to do this: Write a simply script in a file, say, script.py. Then compile the script.py to script.txt and make the C++ program read the script.txt file for the data.

But it seems to be way too much for me. So, at it's simplest, I'd like a system like this: Write a simple script in script.py and make the C++ program read the script.py for data.

And yup, it's just to keep the main program code a lot cleaner. Not only the if statements, all of it. I want just the main functions, etc, to stick in the C++, but all the main process to be in the .py file for example.

Write a simply script in a file, say, script.py. Then compile the script.py to script.txt

I know nothing about Python, but I think normally you don't "compile" a Python script; you just run it. And if you were to compile something, it seems like it would end up as an executable file, not a data file. Do you mean you want to execute a Python script and have that create a text file full of words (not executable code)?

and make the C++ program read the script.txt file for the data.

Lots of ways to do this.

Write a simple script in script.py and make the C++ program read the script.py for data.

So you want to write a Python interpreter in C++? Why not stick with the actual real Python interpreter? I'm not entirely sure I'm following 100%.

And yup, it's just to keep the main program code a lot cleaner. Not only the if statements, all of it. I want just the main functions, etc, to stick in the C++, but all the main process to be in the .py file for example.

I think I'd have to see it. The project seems quite elaborate. Unless this is just all for fun, I think I would pick either C++ or Python and stick with it. If I wanted to clean up the C++ code or the Python code, I'd use a text editor or an IDE.

Best of luck on this. It sounds kind of interesting, though I still can't picture it.

Well, I eventually decided to use this kind of system:

switch (Choice) {
			case 1:
				AttackEnemy = 1;
				break;
			case 2:
				DefendFromEnemy = 1;
				break;
		}


		if (AttackEnemy == 1) {
			DoStuffHere;
		}

I also put all the functions in one header file, variables, constants and function prototypes to another header file and .ini file loader to another header file.

Now, I've gotta say, this way the whole code is a lot more cleaner. Before, I used the case 1: DoStuffHere; system, and that simply doesn't work.

Guess there's no need for a scripting language right now...

You could put all your non-inline functions in another source file instead of a header file.

That would make sure they aren't defined multiple times when your project grows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.