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

'=' operator

4 posts in this topic

Back on AngelScript integration after having other projects running ... Well, I've got a request or question : Working with 1.9.0 WIP 4 (I will as soon as I can integrate 1.9.2) I am having troubles with the '=' operator. I am using a class (that I have grabbed from the net) that is using CString. Peharps you don't know about CString, it's an MFC string class that is using reference counting for not duplicating strings but rather having reference counting. So, when you're making csTest1 = csTest2 , the CString = operator is called (MFC/SRC/STRCORE.CPP) :
const CString& CString::operator=(const CString& stringSrc)
{
	if (m_pchData != stringSrc.m_pchData)
	{
		if ((GetData()->nRefs < 0 && GetData() != _afxDataNil) ||
			stringSrc.GetData()->nRefs < 0)
		{
			// actual copy necessary since one of the strings is locked
			AssignCopy(stringSrc.GetData()->nDataLength, stringSrc.m_pchData);
		}
		else
		{
			// can just copy references around
			Release();
			ASSERT(stringSrc.GetData() != _afxDataNil);
			m_pchData = stringSrc.m_pchData;
			InterlockedIncrement(&GetData()->nRefs);
		}
	}
	return *this;
}
As you can see, the '=' operator use InterlockedIncrement to not duplicate the real string but rather increment a reference count. Ok, this is just to explain what's going on with AS. I have referenced a class like this :
	in_pAsEngine->RegisterObjectType("CSMTPAddress", sizeof(CSMTPAddress), asOBJ_CLASS);
	in_pAsEngine->RegisterObjectBehaviour("CSMTPAddress", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(CSMTPAddress_Constructor), asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CSMTPAddress", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(CSMTPAddress_Destructor), asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectMethod("CSMTPAddress", "CString GetRegularFormat(int bEncode, const CString& sCharset)",asMETHOD(CSMTPAddress,GetRegularFormat), asCALL_THISCALL);
	in_pAsEngine->RegisterObjectMethod("CSMTPAddress", "void SetNameAndAdress(bstr sFriendly, bstr sAddress)",asMETHOD(CSMTPAddress,SetNameAndAdress), asCALL_THISCALL);
	in_pAsEngine->RegisterObjectProperty("CSMTPAddress", "CString m_sEmailAddress", offsetof(CSMTPAddress, m_sEmailAddress));
	in_pAsEngine->RegisterObjectProperty("CSMTPAddress", "CString m_sFriendlyName", offsetof(CSMTPAddress, m_sFriendlyName));
As you can see, there is NO asBEHAVE_ASSIGNMENT behaviour declared. Now, this is the script :
CSMTPMessage msgSMTP;
CString csText;
CSMTPBodyPart csBodyPart;
CPJNSMTPConnection smtpCnx;
CSMTPAddress adrTo, adrFrom;

adrTo.SetNameAndAdress("zorba le grec", "donotemail@me.fr");
adrFrom.SetNameAndAdress("garou le hibou", "donotsend@hotmail.com");

csText = "Bonjour d'AngelScript";
csBodyPart.SetTitle(csText);
csText = "BodyPart\nvia angelscript\net\nSMTP PJ Naughter";
csBodyPart.SetText(csText);

msgSMTP.AddRecipient(adrTo, 0);
msgSMTP.m_From = adrFrom;
msgSMTP.m_Priority = 0;
msgSMTP.AddBodyPart(csBodyPart);

if (smtpCnx.Connect("smtp.wanadoo.fr", 0, "", "", 25, "") == 1) {
	smtpCnx.SendMessage(msgSMTP);
	smtpCnx.Disconnect(1);
}
This code is well compiled and not so well executed. You can notice the :
msgSMTP.m_From = adrFrom;
The '=' operator is working but there is not '=' operator behaviour defined ! So, I don't know what AS is doing but the crash come at the msgSMTP destruction because, the CString that is member of CSMTPMessage is deleted and there is corrupted data in it. How did I solve the pb ? Simply in referencing the '=' operator behaviour that way :
CSMTPAddress *asCSMTPAddressCopy(const CSMTPAddress *src, CSMTPAddress *dst)
{
	(*dst) = (*src);
	return dst;
}

...

	in_pAsEngine->RegisterObjectBehaviour("CSMTPAddress", asBEHAVE_ASSIGNMENT, "CSMTPAddress &f(const CSMTPAddress &)", asFUNCTION(asCSMTPAddressCopy),      asCALL_CDECL_OBJLAST);
So, my request would be this one : Is this a normal feature of AS to have a default '=' operator ? If Yes, could it become No because it can cause some troubles on some referenced classes. If No, I think that you can fire a bug event ! Regards, AbrKen.
0

Share this post


Link to post
Share on other sites
I know of the CString class, though I don't use it anymore. I've dropped MFC a long time ago.

The default assignment operator is a direct copy of data stored in the object. So it is only expected that you'd get problems if you don't register the assignment operator.

I noticed that you wrote a wrapper function for registering the assignment operator. That is actually not necessary, you could register the operator directly like this:


r = engine->RegisterObjectBehaviour("string", asBEHAVE_ASSIGNMENT, "string &f(const string &)", asMETHODPR(CString, operator =, (const CString&), CString&), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_ADD_ASSIGN, "string &f(const string &)", asMETHODPR(CString, operator+=, (const CString&), CString&), asCALL_THISCALL); assert( r >= 0 );




What I can do to improve this behaviour is to have AngelScript verify that an object registered with the asOBJ_CLASS_ASSIGNMENT flag actually have the assignment behaviour registered as well. The same for asOBJ_CLASS_DESTRUCTOR and asOBJ_CLASS_CONSTRUCTOR.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
The default assignment operator is a direct copy of data stored in the object. So it is only expected that you'd get problems if you don't register the assignment operator.


Ok, this is from where comes the problem !

This mean that if I have :

class A {
B *ptrMember;
int intMember;
public:
A() {ptrMember = NULL;};
virtual ~A() {if (ptrMember) delete ptrMember;};
void DoSomeStuffThatFillptrMember() {ptrMember = new B();};
}


and a script where class A have been referenced :

void scriptFunc()
{
A var1, var2;

var1.DoSomeStuffThatFillptrMember();
var2 = var1;
}


At the end of the script function when var2 and var1 call their destructors, there should be a crash.

Is that correct ?
0

Share this post


Link to post
Share on other sites
Yes, that is correct.

Do you think I need to add some way of disabling the assignment operator? Isn't it better to define an assignment operator that does a correct assignment or sets a script exception?

0

Share this post


Link to post
Share on other sites
To be honest, I don't know what is the good answer to this.
Having the '=' operator always defined is a good feature but, that should be avoided when the object stores some values in it's data members that needs to be 'copied' and not 'memcpy'.
So how AS can handle this ?
I see 2 solutions :
- Avoid the '=' operator when not defined as a behaviour.
- Don't change anything.

The first solution lead AS user to change their AS integration when they were using '=' and not defining the behaviour. This lead to insatisfaction of some AS users and this is bad.
So you can add to AS the Not behaviour that let user define that an object do not implement the '=' behaviour. This is much better since this is an add on and not a change to the library.

You can also don't change anything and put a big warning to AS users, because even with some class where there is no pointer, this kind of crash can happen.

Really, do what you feel.

Now that I have understood the '=' feature, I can understand some crash that were happening and that I was solving in using pointers rather than script constructed objects !

AbrKen.
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