Page 1 of 1

[Solved] Script Class Help

Posted: Sun Feb 14, 2010 8:36 pm
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.

Re: Script Class Help

Posted: Sun Feb 14, 2010 9:34 pm
by avansc
http://s000.tinyupload.com/index.php?fi ... k=transfer

that should give you a decent start.

Re: Script Class Help

Posted: Mon Feb 15, 2010 3:48 am
by K-Bal
Seems more like data initialising then scripting ;) I would use something like YAML for this.

Re: Script Class Help

Posted: Mon Feb 15, 2010 6:16 am
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