NUTS

Published June 13, 2007
Advertisement
Well what an adventure today has been.

First of all it was way to hot to do anything so I just stayed in bed till 10 and then spent the rest of the morning reading (The Sicons of Shannara - great book). When it had cooled down a bit I risked turing my computer on and just sat there blankley thinking about what I could do. So eventually I came to the decision of doing more to my engine and doing somthing that I have really wanted to do for a long time - Adding Scripting.

The first question, 'What scripting language should I use?'. Well to tell the truth it took all of 0.2 seconds to think about that and I decided upon 'Squirrel' over 'Lua' because 'Lua' makes me want to die every time I look at how disgusting it is. Anyway after a bit of looking (google) I found the download (sqplus) and started just trying to get it working. Well after much hair pulling I finally figured out how to do stuff with it and have now just been playing around with making the library functions visiable to a script - and it is sooo cool.

As things stand an example script looks like this:
local running = true;while (running == true){	running = UpdateSystem();	UpdateInput();	Cls();	DrawRect(MouseX(), MouseY(), 10, 10);	Flip();	if (KeyDown(27) == true)		running = false;}

Which is exctally the same as for the hardcoded c++ version, cept you dont have to declare types(bool for 'running').

Basically the above code draws a white box at the mouse position.

The C++ Complimenting code is below (it is just test code - im probaly going to create a full sq version of the engine):
/*	Fallen Smash Bash	-----------------------------	A game written using the Fallen Engine	Basic clone of breakout*//*	Fallen Engine - Test Project	Written by Stefan J Lulham*/// System includes#include #include #include #include #include  #include  #include #include #pragma comment (lib ,"squirrel.lib")#pragma comment (lib ,"sqdbglib.lib")#pragma comment (lib ,"sqstdlib.lib")#pragma comment (lib ,"sqplus.lib")// Library includes#include "../Fallen Engine/system.h"#include "../Fallen Engine/graphics.h"#include "../Fallen Engine/input.h"#include "../Fallen Engine/resource_manager.h"// Game includes#include "game.h"// Usee some library'susing namespace Fallen::System;using namespace Fallen::Graphics;using namespace Fallen::Input;using namespace Fallen::ResourceManager;using namespace std;using namespace SqPlus;Key ToFallenKey(SQInteger k){	switch(k)	{	case 27:		return KEY_ESCAPE;	}}SQInteger s_DrawRect(HSQUIRRELVM v){	SQFloat x, y, w, h;	sq_getfloat(v, 2, &x);	sq_getfloat(v, 3, &y);	sq_getfloat(v, 4, &w);	sq_getfloat(v, 5, &h);	CGraphics::DrawRect(x, y, w, h);	return 0;}SQInteger s_SetDrawColor(HSQUIRRELVM v){	SQFloat r, g, b;	sq_getfloat(v, 2, &r);	sq_getfloat(v, 3, &g);	sq_getfloat(v, 4, &b);	CGraphics::SetDrawColor(r, g, b);	return 0;}SQInteger s_MouseX(HSQUIRRELVM v){	sq_pushfloat(v, SQFloat(CInput::MouseX()));	return 1;}SQInteger s_MouseY(HSQUIRRELVM v){	sq_pushfloat(v, SQFloat(CInput::MouseY()));	return 1;}SQInteger s_KeyDown(HSQUIRRELVM v){	SQInteger k;	sq_getinteger(v, 2, &k);	sq_pushbool(v, (SQBool)CInput::KeyDown(ToFallenKey(k)));	return 1;}SQInteger s_Cls(HSQUIRRELVM v){	CGraphics::Cls();	return 0;}SQInteger s_Flip(HSQUIRRELVM v){	CGraphics::Flip();	return 0;}SQInteger s_UpdateInput(HSQUIRRELVM v){	CInput::Update();	return 0;}SQInteger s_UpdateSystem(HSQUIRRELVM v){	sq_pushbool(v, (SQBool)CSystem::Update());	return 1;}SQRESULT register_functions(HSQUIRRELVM v){	SquirrelVM::CreateFunctionGlobal(s_DrawRect, _SC("DrawRect"), _SC("*"));	SquirrelVM::CreateFunctionGlobal(s_SetDrawColor, _SC("SetDrawColor"), _SC("*"));	SquirrelVM::CreateFunctionGlobal(s_MouseX, _SC("MouseX"), _SC(""));	SquirrelVM::CreateFunctionGlobal(s_MouseY, _SC("MouseY"), _SC(""));	SquirrelVM::CreateFunctionGlobal(s_Flip, _SC("Flip"), _SC(""));	SquirrelVM::CreateFunctionGlobal(s_Cls, _SC("Cls"), _SC(""));	SquirrelVM::CreateFunctionGlobal(s_UpdateInput, _SC("UpdateInput"), _SC(""));	SquirrelVM::CreateFunctionGlobal(s_KeyDown, _SC("KeyDown"), _SC("*"));	SquirrelVM::CreateFunctionGlobal(s_UpdateSystem, _SC("UpdateSystem"), _SC(""));	return SQ_OK;}int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){/*	Game* game = new Game();	game->Run();	delete game;*/		// Load modules	CSystem::LoadModule();	CGraphics::LoadModule();	CInput::LoadModule();	// Set the interfaces	CSystem::SetInterface(CSystem::Interface_Win32());	CGraphics::SetInterface(CGraphics::Interface_OpenGL());	CInput::SetInterface(CInput::Interface_Win32());	// Setup the graphics context	CGraphics::CreateContext("Fallen Smash Bash", 800, 600, 32, false);	CGraphics::SetMaskColor(255, 0, 255);	CGraphics::SetVSync(true);	CGraphics::SetHandle(0, 0);		bool running = true;	SquirrelVM::Init();	register_functions(SquirrelVM::GetVMPtr());	SquirrelObject test;	test = SquirrelVM::CompileScript("script.nut");	//while (running == true)	//{		//running = CSystem::Update();		//CInput::Update();	     		//CGraphics::Cls();		SquirrelVM::RunScript(test);		//CGraphics::DrawRect(100, 100, 100, 100);		//CGraphics::Flip();		//if (CInput::KeyPressed(KEY_ESCAPE))		//	running = false;	//}	SquirrelVM::Shutdown();	CGraphics::DestroyContext();	CGraphics::UnloadModule();	CSystem::UnloadModule();	CInput::UnloadModule();	CResourceManager::UnloadResources();}


Oh I also got a really SUPER COOL book the other day that cost about GBP100 -`Character Modeling 2 - /Ballistic/ book number '0145'` It came with a certificate and four prints of some of the works found inside.

Previous Entry PP in UK
Next Entry Scripted Game
0 likes 3 comments

Comments

Ravuya
But how does Squirrel work for functors? One of the big reasons I'm using Lua is how nice LuaPlus is for functors.
June 13, 2007 04:54 PM
diablo_tk
Im not sure about luaplus , but in sqplus you basicaly have something like the following:

SQInteger foo(HSQUIRRELVM v) // SQ Function
{
	float blob;
        sq_getfloat(v, 2, &blob); // Get one argument (2 - Is the offset so to get next argument use 3, 4, etc
        // Call a function or somthing
        GraphicsSetAlpha(blob);
        sq_pushbool(v, (SQBool)true); // push a return value
	return 1; // The function returns a value (0 if it doesnt)
}

// Register the function
// Use createFunctionGlobal to add it to the global table
// arg1: SQFunction (above)
// arg2: Script function name
// arg3: Argument mask (user '*' for any data type)
SquirrelVM::CreateFunctionGlobal(foo, _SC("Foo"), _SC("*"));




Im still lerning my self ;) (I only got it working tonight and it took some figuring out)
June 13, 2007 06:02 PM
John Schultz
Re- functors/closures and script access: SqPlus was derived from LuaPlus and extended for Squirrel. See testSqPlus2.cpp in the SqPlus package: http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/SqPlus.html. More info here: http://squirrel-lang.org/forums/default.aspx.
July 22, 2007 02:22 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Well...

994 views

Updgrade

1019 views

giff (t)

1087 views

Also

1129 views

...

557 views

Is this thing on?

864 views

Rewrite Done

964 views

R

1006 views

Codename

877 views

Playground

835 views
Advertisement