Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

Creating an output class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 desertcube   Members   -  Reputation: 514

Like
0Likes
Like

Posted 22 September 2004 - 09:24 AM

Hi there, I've been trying to reproduce cout in the scripts but I'm not having much sucess! My idea was to register a simple type and create a function and register it using asBEHAVE_BIT_SLL, as this is the << operator. The function would need to take two arguments, what to print (for the sake of simplisity, I'm just trying to print a number) and the stream object (again, just an int). This is the code I have so far, it's based on the TestNegateOperator() function in the testframework. The error I'm getting is from ExecuteString(), it's returning -17 - asINVALID_CONFIGURATION;
#include "angelscript.h"
#include <iostream>

#define TESTNAME "TestIOStream"

static bool called = false;
static int outStream = 0;

static int output(int value, int * out) 
{
	called = true;
	std::cout << value << std::endl;
	return *out;
}

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

bool TestIOStream()
{
	bool fail = false;

 	asIScriptEngine * engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
	engine->RegisterObjectType("obj", sizeof(int), asOBJ_PRIMITIVE);
	engine->RegisterObjectBehaviour("obj", asBEHAVE_BIT_SLL, "obj f(int)", asFUNCTION(output), asCALL_CDECL_OBJLAST);
	engine->RegisterGlobalProperty("obj testVal", &outStream);

	COutStream obj;
	std::cout << engine->ExecuteString(0, "testVal = testVal << 20", &obj, 0) << std::endl;

	if( !called ) // failure
	{
		std::cout << TESTNAME << "\n: behaviour function was not called from script\n\n";
		fail = true;
	}
	engine->Release();
	return fail;
}


I may just be going in the wrong direction, but does anybody else know a way to emulate "operator<<" in angel script? EDIT: Source/code tags!

Sponsor:

#2 Deyja   Members   -  Reputation: 920

Like
0Likes
Like

Posted 22 September 2004 - 10:39 AM

Why are your variables and functions declared static? They are already at global scope!

The power of std::basic_stream's << operator is that it is a template, so it can output ANYTHING. You can NOT mimic that behavior in AngelScript. There are no templates. There are no template specializations. You can make it look like cout some of the time, but never all of the time, not even most of the time. A family of Print functions will serve you better.

#3 desertcube   Members   -  Reputation: 514

Like
0Likes
Like

Posted 22 September 2004 - 11:08 AM

Like I said, I just used the TestNegateOperator test file and modified it a bit, so the static is there to ensure that the variables only last for the scope of the CPP file (IIRC), plus I didn't want to change anything in the TestNegateOperator() function as I know that works!
I know that the power of std::basic_stream's <<operator is that it's a template, but I'm not trying to use a template in my script! I want to provide various overloads (int, float, double, string etc) but first of all I just want to get it so it can print a number!
Do you still think this is not possible in the script?

#4 Andreas Jonsson   Moderators   -  Reputation: 3450

Like
0Likes
Like

Posted 23 September 2004 - 02:52 AM

Of course it is possible :)

AngelScript only support the << operator as a global operator, so you'll have to do something like this:


engine->RegisterObjectBehaviour(0, asBEHAVE_BIT_SLL, "obj &f(obj &, int)", asFUNCTION(output), asCALL_CDECL);

int *output(int *out, int value)
{
std::cout << value << std::endl;
return out;
}


That ought to work, though I didn't actually test it. Let me know if you need more help.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#5 desertcube   Members   -  Reputation: 514

Like
0Likes
Like

Posted 23 September 2004 - 08:03 AM

WitchLord, you da man! It works like a charm!

Now the fun part of creating and registering all the functions used for cout... Anyway, cheers.

I'll keep you updated with how the class looks, might be usefull for other people. My plan is to have a class and you register an object with it where the output goes. So this means that you could have the script write stuff to a file, but for the time being, it'll use cout.

#6 Andreas Jonsson   Moderators   -  Reputation: 3450

Like
0Likes
Like

Posted 23 September 2004 - 08:39 AM

I'll be happy to upload your class to the site (or link to your site if you prefer), once it is finished.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#7 Deyja   Members   -  Reputation: 920

Like
0Likes
Like

Posted 23 September 2004 - 04:14 PM

I meant it was not possible to mimic the power of the << override in C++ in angelscript. If I create a new class in C++, and stream it to cout, it will compile - though it probably won't do what you want. In angelscript, it won't compile at all.

Regardless; the statics are totally unecessary. If it's declared in a cpp file, it's already scoped to that file.

#8 Andreas Jonsson   Moderators   -  Reputation: 3450

Like
0Likes
Like

Posted 24 September 2004 - 02:17 AM

Deyja:

You are correct that AngelScript doesn't have the power that C++ have with templates. But that shouldn't stop him from implementing the functionality, should it? Writing a family of operator overloads would require the same amount of work as a family of printf functions, don't you think? Besides, AngelScript doesn't have all that many types, and the script writer can't declare his own types either.

I don't believe you are correct in your statement about static. If you define the same function in two different modules without the static modifier the linker will complain. A function, or global variable that is declared as static can't be exported to other modules, thus it will not conflict with other functions/variables of the same name.

I use static in every module in the test framework since I don't want to have to worry about conflicts with other test cases. Sometimes that is not enough (e.g. class definitions) and I have to use a namespace as well to separate the testcases even more.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#9 Deyja   Members   -  Reputation: 920

Like
0Likes
Like

Posted 24 September 2004 - 01:24 PM

It's odd if it requires it for you. It never has for me. That doesn't mean my compiler is right, though. Not a bug, it's a feature!

[edit]Actually, if you declare a type in a cpp file, it shouldn't leave that file at all. Code only sees the declaration it got, and nothing else. This is why libraries can declare a class one way for internal use, and another way for external use.[/edit]

#10 Andreas Jonsson   Moderators   -  Reputation: 3450

Like
0Likes
Like

Posted 24 September 2004 - 01:51 PM

I agree. It should be like that, but at least MSVC++ seems to have trouble following those rules. But then again, MSVC++ isn't the most C++ standard compliant compiler around. [wink]


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS