Archived

This topic is now archived and is closed to further replies.

Scriptable Classes class

This topic is 5114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been comtemplating a class that scriptable classes will contain or be derived from... what is the general consensus of something like the following:
class CCompiledScript 
{
public:
	CCompiledScript();
	~CCompiledScript();
	bool LoadScript(const std::string filename, const std::string scriptname);
	bool ReLoadScript();
	void Execute();
private:
	std::string m_ScriptName;
	std::string m_FileName;
	PBYTE		m_Script;
	DWORD		m_dwScriptLenghth;
};

CCompiledScript::CCompiledScript(): m_Script(NULL), m_dwScriptLenghth(0)
{
	m_ScriptName.clear();
	m_FileName.clear();
}

CCompiledScript::~CCompiledScript()
{
	if(m_Script)
	{
		delete [] m_Script;
		m_Script = NULL;
	}
}

bool CCompiledScript::ReLoadScript()
{
	if(!m_ScriptName.empty())
	{
		return( LoadScript( m_FileName, m_ScriptName ) );
	}
	return(false); // was the script loaded???

}

bool CCompiledScript::LoadScript(const std::string filename, const std::string scriptname)
{
	if(m_Script)
	{
		delete [] m_Script;
		m_Script = NULL;
	}

	m_FileName   = filename;
	m_ScriptName = scriptname;
	
	HANDLE hInFile = CreateFile( filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hInFile == INVALID_HANDLE_VALUE) 
	{
		return(false);
	}
	// Get the filesize and allocate memory for the script

	m_dwScriptLenghth = GetFileSize (hInFile, NULL);
	m_Script = new BYTE[m_dwScriptLenghth];
	ZeroMemory(m_Script, m_dwScriptLenghth);

	// Load up the file into the uncompressed buffer.

	DWORD dwBytesRead = 0;
	if(!ReadFile(hInFile, m_Script, m_dwScriptLenghth, &dwBytesRead, NULL))
	{
		// We had a problem loading the uncompressed file - clean up and return false

		CloseHandle(hInFile);
		delete [] m_Script;
		m_Script = NULL;
		m_ScriptName.clear();
		return(false);
	}
	CloseHandle(hInFile);
	return(true);
}
 
-edited code tag to source -Bumped Dave "Dak Lozar" Loeser [edited by - Dak Lozar on June 3, 2003 3:06:26 PM] [edited by - Dak Lozar on December 9, 2003 10:42:26 PM]

Share this post


Link to post
Share on other sites
Bump, OK, this post is old and I''d really like some feedback. I''m getting ready to implement scripting into my engine and want to know if something such as this would be useful or would it come back to bite me in the future. Any of you guys that have implemented scripting with Lua have any tips from the trenches?

Thanks,

Dave Dak Lozar Loeser
The WM_NULL message performs no operation. An application sends the WM_NULL message if it wants to post a message that the recipient window will ignore. - MSDN

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''d suggest just going out and trying it. That''s the only way you''re ever going to get an answer for your question. As it is, the code you''ve written is pretty generic and doesn''t have anything to do with scripting.

Share this post


Link to post
Share on other sites
Dak Lozar - Check this out. It''s pretty much what you want to do


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
Dak Lozar - Check this out. It''s pretty much what you want to do


Thanks for the link - I''ve already seen this and I prefer to use Lua over Python. Thanks again.

Dave Dak Lozar Loeser
The WM_NULL message performs no operation. An application sends the WM_NULL message if it wants to post a message that the recipient window will ignore. - MSDN

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://catmother.sf.net

They have embedded LUA in their game and they have taken an interesting approach even though it is kinda messy.

I''d say go for it, and see how it turns out. You only really learn stuff, by trying it out and then tuning it and such.

Good Luck.

Share this post


Link to post
Share on other sites