Exemplo n.º 1
0
/**
*  @brief
*    Runs a script
*/
void Application44::RunScript(const String &sScriptFilename)
{
	// Get the script source code
	const String sSourceCode = LoadableManager::GetInstance()->LoadStringFromFile(sScriptFilename);
	if (sSourceCode.GetLength()) {
		// Create the script instance by using the extension of the given filename to detect the script language
		Script *pScript = ScriptManager::GetInstance()->Create(ScriptManager::GetInstance()->GetScriptLanguageByExtension(Url(sScriptFilename).GetExtension()));
		if (pScript) {
			// Tell the script about "Application44::GetMyRTTIClassInstance"
			pScript->AddGlobalFunction("GetMyRTTIClassInstance", Functor<MyRTTIClass*, MyRTTIClass*>(&Application44::GetMyRTTIClassInstance, this));

			// Set the script source code
			if (pScript->SetSourceCode(sSourceCode)) {
				// Print the name of the used script language
				System::GetInstance()->GetConsole().Print("-- " + pScript->GetScriptLanguage() + " script language --\n");

				// Call the script function "OOP"
				FuncScriptPtr<void>(pScript, "OOP").Call(Params<void>());

				{ // Call the script function "UseCppRTTIObject"
					Params<MyRTTIClass*, MyRTTIClass*> cParams(m_pMyRTTIClass);
					FuncScriptPtr<MyRTTIClass*, MyRTTIClass*>(pScript, "UseCppRTTIObject").Call(cParams);
					if (cParams.Return != m_pMyRTTIClass)
						System::GetInstance()->GetConsole().Print("Error, script returned invalid RTTI object instance!\n");
				}

				// Cleanup
				delete pScript;

				// Print new line
				System::GetInstance()->GetConsole().Print("--\n");
			} else {
				// Error!
				System::GetInstance()->GetConsole().Print("Failed to use the script source code \"" + sScriptFilename + "\"\n");
			}
		}
	} else {
		// Error!
		System::GetInstance()->GetConsole().Print("Failed to load the script \"" + sScriptFilename + "\"\n");
	}

	// Print new line
	System::GetInstance()->GetConsole().Print('\n');
}
Exemplo n.º 2
0
/**
*  @brief
*    Runs a script
*/
float Application42::RunScript(const String &sScriptFilename, float fFirst, float fSecond)
{
	float fResult = 0.0f;

	// Get the script source code
	const String sSourceCode = LoadableManager::GetInstance()->LoadStringFromFile(sScriptFilename);
	if (sSourceCode.GetLength()) {
		// Create the script instance by using the extension of the given filename to detect the script language
		Script *pScript = ScriptManager::GetInstance()->Create(ScriptManager::GetInstance()->GetScriptLanguageByExtension(Url(sScriptFilename).GetExtension()));
		if (pScript) {
			// Print the name of the used script language
			System::GetInstance()->GetConsole().Print("-- " + pScript->GetScriptLanguage() + " script language --\n");

			// Functor pointing to the static method "Application42::StaticMethod"
			Functor<int, int> cStaticMethod(StaticMethod);

			// Functor pointing to the static method "Application42::StaticStringMethod"
			Functor<String, String> cStaticStringMethod(StaticStringMethod);

			// Functor pointing to the member method "Application42::Method"
			Functor<int, int> cMethod(&Application42::Method, this);

			// Functor pointing to the script function "ScriptFunction"
			Functor<int, int> cScriptFunction(new FuncScriptPtr<int, int>(pScript, "ScriptFunction"));

			// Tell our script about those functors so that we can use them within the script...
			pScript->AddGlobalFunction("CppFunction", cStaticMethod, "FirstNamespace.SecondNamespace");
			pScript->AddGlobalFunction("CppStringFunction", cStaticStringMethod);
			pScript->AddGlobalFunction("CppMethod", cMethod);

			// The following is possible as well: Script is calling C++, C++ is calling script... *g*
			pScript->AddGlobalFunction("CppScriptFunction", cScriptFunction);
			// ... although it depends on the used internal script API whether or not it actually works without issues. With
			// the Lua, Python and V8 (JavaScript) API there are no issues, but AngelScript can't run another AngelScript while one is already running.
			// But using this way, one can e.g. call an AngelScript function from inside a Lua script function...
			// But in general, to be on the safe side, don't do such crazy things... although, hm... *g*

			// Set the script source code
			if (pScript->SetSourceCode(sSourceCode)) {
				float fFactor = 0.0f;

				{ // Some functor fun
					// Call functors - as you can see, there's no difference whether it's a static method, a member method or a script function
					const int  nValue				 = 42;
					const int  nFunctionResult		 = cStaticMethod(nValue);
					const int  nMethodResult		 = cMethod(nValue);
					const int  nScriptFunctionResult = cScriptFunction(nValue);
					const bool bFunctorResultsEqual  = (nFunctionResult == nValue && nMethodResult == nValue && nScriptFunctionResult == nValue);
					System::GetInstance()->GetConsole().Print(String("Same functor behaviour: ") + (bFunctorResultsEqual ? "Yes" : "No") + '\n');

					{ // Call the script function "CallCpp"
						// Get the typed dynamic parameters
						Params<int, int> cParams(nValue);

						// Call the script function
						FuncScriptPtr<int, int>(pScript, "CallCpp").Call(cParams);

						// Get and check result
						const bool bEqual = (cParams.Return == (nFunctionResult + nMethodResult + nScriptFunctionResult));
						System::GetInstance()->GetConsole().Print(String("Global function behaviour as expected: ") + (bEqual ? "Yes" : "No") + '\n');
					}
				}

				{ // Call the script function "GetFactor"
					// Get the typed dynamic parameters
					Params<float> cParams;

					// Call the script function
					FuncScriptPtr<float>(pScript, "GetFactor").Call(cParams);

					// Get the result
					fFactor = cParams.Return;
				}

				// Call the script function "SetFactor"
				FuncScriptPtr<void, float>(pScript, "SetFactor").Call(Params<void, float>(fFactor + 1.0f));

				{ // Call the script function "Calculate"
					// Get the typed dynamic parameters
					Params<float, float, float> cParams(fFirst, fSecond);

					// Call the script function
					FuncScriptPtr<float, float, float>(pScript, "Calculate").Call(cParams);

					// Get the result
					fResult = cParams.Return;
				}

				{ // Call the script function "ReturnMyString"
					// Get the typed dynamic parameters
					Params<String, String> cParams("MyString");

					// Call the script function
					FuncScriptPtr<String, String>(pScript, "ReturnMyString").Call(cParams);

					// Check the result
					System::GetInstance()->GetConsole().Print(String("Got my string: ") + (cParams.Return == "MyString" ? "Yes" : "No") + '\n');
				}

				// Print message
				System::GetInstance()->GetConsole().Print('\'' + sScriptFilename + "' input was " + fFirst + " and " + fSecond + ", result is " + fResult + '\n');

				{ // Call the script function "SayHello" which is inside the "PublicFunctions"-namespace
					// Get the typed dynamic parameters
					Params<String> cParams;

					// Call the script function
					FuncScriptPtr<String>(pScript, "SayHello", "PublicFunctions").Call(cParams);

					// Get the result
					System::GetInstance()->GetConsole().Print("The script function \"SayHello\" which is inside the \"PublicFunctions\"-namespace says \"" + cParams.Return + "\"\n");
				}
			} else {
				// Error!
				System::GetInstance()->GetConsole().Print("Failed to use the script source code \"" + sScriptFilename + "\"\n");
			}

			// Cleanup
			delete pScript;

			// Print new line
			System::GetInstance()->GetConsole().Print("--\n");
		}
	} else {
		// Error!
		System::GetInstance()->GetConsole().Print("Failed to load the script \"" + sScriptFilename + "\"\n");
	}

	// Print new line
	System::GetInstance()->GetConsole().Print('\n');

	// Done
	return fResult;
}