[Solved] Script Class Help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

[Solved] Script Class Help

Post by GroundUpEngine »

Just a quick half assed script class I wrote, for my map editor to load/store 3D objects ;)

Please check it out, give me a tip, tell me what I'm doing wrong, etc.. heres what I got so far ->

Script.h

Code: Select all

#ifndef		__SCRIPT_H
#define		__SCRIPT_H

#include "Globals.h"
#include "Entity.h"

namespace GroundUpEngine
{
    class Script {
	  // Class Private Variables //
	  private:
		ifstream script;
		int object_count;

		list<Entity*>Entitys;
		list<Entity*>::iterator node;

	  public:
		// De/Constructor //
		Script();
		~Script();

		// Script Functions //
		void Read();
	};

} // namespace GroundUpEngine

#endif	// __SCRIPT_H
Script.cpp

Code: Select all

#include "Script.h"

namespace GroundUpEngine
{
    // ---------------------------------------- De/Constructor ---------------------------------------- //
	Script::Script()
    {
        object_count = 0;
    }
	Script::~Script()
	{
        node = Entitys.begin();

        while(node != Entitys.end())
        {
            if((*node) != NULL)
            {
                delete *node;
                *node = NULL;
            }
            ++node;
        }
	}

	// ---------------------------------------- Script Functions ---------------------------------------- //
	void Script::Read()
	{
         cout << "What script do you want run?\t";
         string filename;
         cin >> filename;
         script.open(filename.c_str());
             // Loop Until end of File //
             while(!script.eof())
             {
                 string temp;
                 // Read for engine commands //
                 script >> temp;
                 if(temp == "object")
                 {
                     object_count++;
                     cout << "\nLoading Object..." << endl;

                     Entitys.push_back(new Entity);
                     node = Entitys.begin();

                     string temp;
                     script >> temp;
                     (*node)->index = atoi(temp.c_str());

                     script >> temp;
                     (*node)->mesh_name = temp;

                     script >> temp;
                     (*node)->texture_name = temp;

                     cout << "Object Number: " << (*node)->index << endl;
                     cout << "Object Mesh: " << (*node)->mesh_name << endl;
                     cout << "Object Texture: " << (*node)->texture_name << endl;

                     ++node;//Increment object count
                 }
                 if(temp == "position")
                 {
                     cout << "\nInitializing Object..." << endl;

                     list<Entity*>::iterator temp_node = Entitys.begin();

                     string temp;
                     int temp_index;
                     script >> temp;
                     temp_index = atoi(temp.c_str());

                     for(int i = 0; i <= temp_index; i++)
                     {
                         ++temp_node;
                     }

                     script >> temp;
                     (*temp_node)->x = atoi(temp.c_str());

                     script >> temp;
                     (*temp_node)->y = atoi(temp.c_str());

                     script >> temp;
                     (*temp_node)->z = atoi(temp.c_str());

                     cout << "Object Number: " << temp_index << endl;
                     cout << "Object Position: " << (*temp_node)->x << ", "
                                     << (*temp_node)->y << ", " << (*temp_node)->z << endl;
                 }
             }
         script.close();

         cout << "\nObject's In Level: " << object_count << endl;
	}

} // namespace GroundUpEngine
testscript.txt

Code: Select all

object 1 Crate.obj Crate.png

position 1 0 0 0
Thanks in advance.
Last edited by GroundUpEngine on Mon Feb 15, 2010 3:40 pm, edited 1 time in total.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Script Class Help

Post by avansc »

http://s000.tinyupload.com/index.php?fi ... k=transfer

that should give you a decent start.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Script Class Help

Post by K-Bal »

Seems more like data initialising then scripting ;) I would use something like YAML for this.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Script Class Help

Post by GroundUpEngine »

avansc wrote:http://s000.tinyupload.com/index.php?fi ... k=transfer

that should give you a decent start.
K-Bal wrote:Seems more like data initialising then scripting ;) I would use something like YAML for this.
Thanks guys! I'll check them out :)

p.s. My goal is for this tool to be very general so you can load maps, edit maps, make player starts and let you play with the map / models you just setup. So scripts could be generated with the tool after saving a map / levels, they could be used to load map files, load object logic, etc..

--

Edit: Map with 1 Tree in YAML would look like this I think:

Forest.map

Code: Select all

object:
	index : 1
	mesh: Tree.obj
	texture: Tree.png
	state: static
	
position:
	index : 1
	- x: float
		value: 5.0
	- y: int
		value: 0
	- z: float
		value: 5.0
Model with 1 animation like this:

Homer.anim

Code: Select all

object:
	index: 1
	mesh: Homer.md2
	texture: Homer.png
	state: animated

animation:
	index: 1
	- walk: 20 fps
		start_frame: 1
		end_frame: 20
Post Reply