• 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
zexee

Why need to copy when using const T &in

5 posts in this topic

HI,

 

First of all, I am looking for such (c++ style) script for a long time and very happy that angelscript exists.

 

I am working on design even simpler interface to register c++ objects / functions so that I can easily register many good c++ libraries and make it a general purpose language.

 

I met some problem when dealing with arguments passing to functions. The const T&in type will always cause a copy constructing for the object passed in and the object will be destroyed after the function ends, and const T&out will create a new object to pass into the function and copy it back to the original object after the function ends. These two behaviors happen no matter for value object type or reference object type. These behaviors are different from c++ and will cause some problem.

 

Since for reference object type, neither const T &inout or T &inout do the copy and I think const T&in should be the same as const T &inout, so that is not a implementation problem.

 

I am wondering what's the original thought of &in and &out? Since the caller will always hold a reference or the value so the object will not be destroyed during the call, and const will control whether the function can modify it or not, so why do we need &in and &out?

0

Share this post


Link to post
Share on other sites

The real reason for &in and &out is to support sandboxing, which means that the script language must guarantee that references and handles are always valid.

 

In C++ it is quite easy to write code that invalidates a reference passed to a function. This is OK for C++ because the programmer is responsible for making sure everything is working ok, but with scripting it is not always possible to assume the script writer will be responsible (or even have a clear understanding over how things work).

 

// C++
vector<string> g_array;
void foo(string &value)
{
   g_array.clear();
   value = "crash since the reference is no longer valid";
}
void main()
{
   g_array.push_back("a");
   foo(g_array[0]);
}

 

Within the script itself &in doesn't make much sense, since the end result is the same as passing the argument by value. But in order to maintain compatibility with C++ where many input arguments are passed by & for performance reason it is needed to differentiate between by value and input reference.

 

&out is needed as it is the only way a function can give additional return values.

 

 

Now, if you don't care about sand-boxing you can change the behaviour of AngelScript by setting the engine property asEP_ALLOW_UNSAFE_REFERENCES to true. When this is set you will be able to use &inout on all types, including primitives and registered value types, which makes it equivalent to C++.

1

Share this post


Link to post
Share on other sites

Thank you very much for the detailed answer! It prevents the passed in arguments being destroyed inside the current function. If it is a reference object, the argument always holds a reference, so &inout is allowed. 

 

I tried asEP_ALLOW_UNSAFE_REFERENCES, and it works very well, except for it still disallow register a reference cast behavior to a value type. Since it is already unsafe, this cast should be no more unsafe than passing value type by ref right?

0

Share this post


Link to post
Share on other sites

For example, I want to have a

template<int N>
class Vector;

I want some specific method for specific Vector<N>, for example CrossProduct are only for Vector<3>. But also, all Vector can have some common method like operator+(). So here is what I implemented

template<int N>
class VectorBase{ /* common methods */}
template<int N>
class Vector : public VectorBase;
template<>
class Vector<3> : public VectorBase<3> { /* specific methods */ }

The operator+ can only return a VectorBase, so many functions only need to accept VectorBase& as arguments. So I want Vector& be implicit cast to VectorBase&.

0

Share this post


Link to post
Share on other sites

Thanks for the example. 

 

I'll take this into consideration when deciding whether or not to add the implicit ref casts for value types.

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