ismetteren wrote:Rebornxeno wrote:I get really confused when you talk about scripting, mainly because I have never used one and don't understand why it's needed. If you need something done, why not do it in the native code you are writing in? Why switch to something working on-top to do it?
Because you don't have to deal with compiling and linking when using an interperted language. It sound like what you want actually
is code working on top of your application, therefore a language that works on top of it makes sense.
Sorry I confused you, ismetteren makes a good point, were you do not have to worry about compiling/linking. In addition it makes your application more "extensible" for example:
in C++ I may have a class "Game Object".
Code: Select all
class GameObject
{
protected:
float x, y, z;
bool initialized, update;
public:
//Imaginary encapsulation.//
//Typically how I set up my "Game Objects".
virtual void Initialize() {
}
virtual void Update() {
}
virtual void Destroy() {
}
void ChangeState()
{
//Stuff.//
}
};
Well this is great; right? Lets add a singleton.
Code: Select all
//This has importance later.//
class Camera
{
//Stuff.//
void MoveTo( float x, float y )
{
//Stuff.//
}
};
//Our singleton.//
struct Engine
{
Camera camera;
std::vector<GameObject*> objects;
void MainLoop()
{
unsigned int size = objects.size();
for( unsigned int i = 0; i < size; ++i )
{
objects[i]->ChangeState();
if( objects[i]->GetInitialize() == true )
objects[i]->Initialize();
else if( objects[i]->GetUpdate() == true )
objects[i]->Update();
else
{
objects[i]->Destroy();
GameObject *obj = objects[i];
objects[i] = NULL;
objects.erase( objects.begin() + i );
delete obj;
}
}
void Add( GameObject *obj ){
objects.push_back( obj );
}
};
Okay if you noticed our "Game Object" class
doesn't really do anything, well if we want to make it do something
we can just inherit it and use the vector of GameObject pointers in the
singleton to call our overwrites of Initialize(), Update() and Destroy() right?
Code: Select all
class MyNewObject: public GameObject
{
//Overwrites.//
//Im just ganna show Update() cuz im lazy :-P
void Update()
{
camera.MoveTo( x, y );
}
};
Wait dont we have to set reference to the engine to get the camera and yada yada yada
just to move the camera? Well that is kinda a pain in the butt.
Why not just put everything in one place?
Why not just write one class and not worry about OOP for
the rest of your app development?
Why not do live debugging?
:-D
---Enter interpreted languages---
Our GameObject class could have been simplified by doing something like the following.
Code: Select all
#include <My Fake Interpreted Language API>
class GameObject
{
//Protected if you choose to expand upon this class
//which is not entirely necessary.//
protected:
float x, y, z;
std::string init, update__, destroy;
bool initialized, update;
public:
void RunScript( std::string script ) {
fakeLang.SendToScript( this );
fakeLang.DoFile( script );
}
virtual void Initialize() {
//Stuff.//
RunScript( init );
}
virtual void Update() {
//Stuff.//
RunScript( update__ );
}
virtual void Destroy() {
//Stuff.//
RunScript( destroy );
}
};
No changes to the engine code needed!!!
Just call this method (and add it to the singleton);
Code: Select all
void Send() {
fakeLang.SendToScript( this );
fakeLang.SendToScript( &camera );
}
You could implement the exact same thing you
were going to do in native code much easier.
Code: Select all
//---Update.fakeScript---//
//Your game object.
//object
//The camera.
//camera
//The singleton.
//engine
//Call a method :
camera:MoveTo( object:GetX(), object:GetY() )
You can make more objects do different things with the same class (you would have to tell each one what script to run though, I use a simple ID system).
Code: Select all
//---Update2.fakeScript---//
//Makes the object move.//
if( object:GetX() < 100 ):
object:SetX( object:GetX() + 1 )
else:
object:SetY( object:GetY() - 1 )
These script's would be called every frame AND (duuuunananana) live debugging!
You could even tell the program to re-write these.
Any changes made to them are applied next cycle after they have been overwritten/saved!
Hope this was not too confusing XD