🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Build fails

Started by
1 comment, last by SomeFusion 19 years ago
Hi, can anybody see why this code won't work? This is my PrintTest class:

// header file
#pragma once

#include <iostream>
#include <string>

class PrintTest
{
public:
	PrintTest(void);
	~PrintTest(void);

	void ConstructPrintTest(PrintTest* pt);
	void DestructPrintTest(PrintTest* pt);
	void printText(void);
	void printText(std::string sometext);
};


// .cpp file

#include ".\printtest.h"

PrintTest::PrintTest(void)
{
	std::cout << "in printtest constructor" << std::endl;
}

PrintTest::~PrintTest(void)
{
	std::cout << "in printtest destructor" << std::endl;
}

void PrintTest::ConstructPrintTest(PrintTest* pt)
{
	new(pt) PrintTest();	
}

void PrintTest::DestructPrintTest(PrintTest* pt)
{
	pt->~PrintTest();
}

void PrintTest::printText(void)
{
	std::cout << "call of method without arguments" << std::endl;
}

void PrintTest::printText(std::string sometext)
{
	std::cout << sometext << std::endl;
}

And this is how I initialze AngelScript and register the void PrintTest::printText(void) method:

#include "angelscript.h"
#include "PrintTest.h"

#include <assert.h>
#include <string>
#include <fstream>

std::string LoadScript(const std::string &scriptfile);

class COutputStream : public asIOutputStream
{
public:
	void Write(const char *text) { std::cout << text; }
};

int main()
{
	int r = 0;
	asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
	if(engine == 0)
		std::cout << "could not create the script engine" << std::endl;


	
	r = engine->RegisterObjectType("PrintTest", sizeof(PrintTest), asOBJ_CLASS_CD);
	assert(r >= 0);
	r = engine->RegisterObjectBehaviour("PrintTest", asBEHAVE_CONSTRUCT, "void f()",asMETHOD(PrintTest, ConstructPrintTest), asCALL_THISCALL);
	assert(r >= 0);
	r = engine->RegisterObjectBehaviour("PrintTest", asBEHAVE_DESTRUCT, "void f()",asMETHOD(PrintTest, DestructPrintTest), asCALL_THISCALL);
	assert(r >= 0);

	//r = engine->RegisterObjectMethod("PrintTest", "void printText()", asMETHODPR(PrintTest, printText,(void), void ), asCALL_THISCALL);
	r = engine->RegisterObjectMethod("PrintTest", "void printText(void)", asMETHOD(PrintTest, printText), asCALL_THISCALL);
	assert(r >= 0);													  

	std::string script = LoadScript("testscript.txt");
	r = engine->AddScriptSection("module", "test", script.c_str(), script.length());
	assert(r >= 0);

	COutputStream out;
	r = engine->Build("module", &out);
	assert(r >= 0);

	int funcID = engine->GetFunctionIDByDecl("module", "void test()");
	
	
	asIScriptContext* testContext = 0;
	engine->CreateContext(&testContext);

	return 0;
}


std::string LoadScript(const std::string &scriptfile)
{
	std::ifstream fin(scriptfile.c_str());
	char ch;
	std::string script_contents;

	while(fin.get(ch))
	{
		script_contents += ch;
	}

	fin.close();
	return script_contents;
}

This is my script file:

void test()
{
	PrintTest pt;
	pt.printText(void);
}
The assertion at engine->Build() fails. The out stream says: test (4, 15) : Error : Expected identifier Whats wrong with my script? when I comment out pt.printText(void); then suddenly everything works. Keep up the great wik with AS! thanks, SomeFusion
Advertisement
Remove that 'void' argument, and see if it works. That's c-syntax and is not required by C++ or AS.
Wohoo! it works now after some other problems :). One of wich were the same ESP problem like descriped in that topic (http://www.gamedev.net/community/forums/topic.asp?topic_id=315578).
This should be corrected in the manual, in the section "Overview of AngelScript" in "Constructors and destructors" since it may cause problems to other people too. :)

This topic is closed to new replies.

Advertisement