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

switch .. case .. and const that are not

Started by
4 comments, last by WitchLord 19 years, 8 months ago
First, there is a workaround. So you can continue (Andreas) working on native arrays. This is what's going on. I've got a C++ class that expose variables to script :

in_pAsEngine->RegisterObjectType("CXPNTree", sizeof(CXmlValue), asOBJ_CLASS);
in_pAsEngine->RegisterObjectProperty("CXPNTree", "uint16 m_wMsgParam", offsetof(CXPNTree, m_wMsgParam));
in_pAsEngine->RegisterObjectProperty("CXPNTree", "uint32 m_lMsgParam", offsetof(CXPNTree, m_lMsgParam));

As you can see, there is no 'const' keyword. Then in a script I was doing this :

// --------------------------------------
// XPNTree : Message SKFPresenceDetection
// --------------------------------------
CString csPageNameWait("Wait");

switch ( Navigator.m_wMsgParam) {
case 3 :
	Navigator.AskForNavigate(Navigator.m_csDefaultPageName);
	break;
case 4 :
	Navigator.AskForNavigate(csPageNameWait);
	break;
}
This script code wasn't working. So I have put some trace using a MessageBox.

// --------------------------------------
// XPNTree : Message SKFPresenceDetection
// --------------------------------------
CString csPageNameWait("Wait");
bstr bstrTmp = bstrFormat(Navigator.m_wMsgParam);

MessageBox(Presenter.GetHwndPresenter(), bstrTmp, "Detection", 0);

switch ( Navigator.m_wMsgParam) {
case 3 :
	MessageBox(Presenter.GetHwndPresenter(), "Présence", "Detection", 0);
	Navigator.AskForNavigate(Navigator.m_csDefaultPageName);
	break;
case 4 :
	MessageBox(Presenter.GetHwndPresenter(), "Absence", "Detection", 0);
	Navigator.AskForNavigate(csPageNameWait);
	break;
}
The MessageBox was telling me that Navigator.m_wMsgParam get value of 3 and 4 but, the switch case wasn't working (no messagebox displayed). So I decided to take another way. Declare an int in the script then assign it the value then do the switch case on that int. That was a mistake (m_wMsgParam is declared as uint16).

int iSwitch = Navigator.m_wMsgParam;
CString csPageNameWait("Wait");
bstr bstrTmp = bstrFormat(Navigator.m_wMsgParam);

MessageBox(Presenter.GetHwndPresenter(), bstrTmp, "Detection", 0);

switch (iSwitch) {
case 3 :
	MessageBox(Presenter.GetHwndPresenter(), "Présence", "Detection", 0);
	Navigator.AskForNavigate(Navigator.m_csDefaultPageName);
	break;
case 4 :
	MessageBox(Presenter.GetHwndPresenter(), "Absence", "Detection", 0);
	Navigator.AskForNavigate(csPageNameWait);
	break;
}
AS gives me "Can't implicitly convert from 'const uint' to 'int'" I get arounf this error doing a cast :

int iSwitch = int(Navigator.m_wMsgParam);
And magically, the script was working fine. Conclusion : The external variable was found to be 'const' without the explicit specification from me. This const leads to the switch not to be evaluated (I think). What do you think about that ? AbrKen.
Advertisement
I'll have to investigate this.

I can't say for sure what is happening, but I believe that the script engine reads the uint16 from your object but doesn't clean the upper word which makes the value fall outside the switch case.

Of course, there might be some problem with the implicit cast as well, since it is reporting 'const uint'.

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

Thanks for investigating !

The implicit cast gives me 100% good result !
Quote: The implicit cast gives me 100% good result !


What do you mean?

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

I mean doing "int iSwitch = int(Navigator.m_wMsgParam);" gives 100% good result.
I understand. Though that is an explicit cast [wink]

You might be able to do this as well:

switch(int(Navigator.m_wMsgParam))
{
...
}

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

This topic is closed to new replies.

Advertisement