• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
desertcube

Creating an output class

9 posts in this topic

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!
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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]

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0