void LuaScriptEventInvoker::HandleLuaScriptEvent(StringHash eventType, VariantMap& eventData) { LuaFunction* function = static_cast<LuaFunction*>(GetEventHandler()->GetUserData()); if (!function) return; // Keep instance alive during invoking SharedPtr<LuaScriptInstance> instance(instance_); if (instance) { if (function->BeginCall(instance)) { function->PushUserType(eventType, "StringHash"); function->PushUserType(eventData, "VariantMap"); function->EndCall(); } } else { if (function->BeginCall()) { function->PushUserType(eventType, "StringHash"); function->PushUserType(eventData, "VariantMap"); function->EndCall(); } } }
bool LuaScript::ExecuteFunction(const String& functionName) { LuaFunction* function = GetFunction(functionName); return function && function->BeginCall() && function->EndCall(); }
void LuaIntegration::CreateScene() { auto* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume // (-1000, -1000, -1000) to (1000, 1000, 1000) scene_->CreateComponent<Octree>(); // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree, // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist Node* zoneNode = scene_->CreateChild("Zone"); auto* zone = zoneNode->CreateComponent<Zone>(); // Set same volume as the Octree, set a close bluish fog and some ambient light zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f)); zone->SetFogColor(Color(0.1f, 0.2f, 0.3f)); zone->SetFogStart(10.0f); zone->SetFogEnd(100.0f); auto* scriptFile = cache->GetResource<LuaFile>("LuaScripts/Utilities/Rotator.lua"); if (!scriptFile) return; // Create randomly positioned and oriented box StaticModels in the scene const unsigned NUM_OBJECTS = 2000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* boxNode = scene_->CreateChild("Box"); boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f)); // Orient using random pitch, yaw and roll Euler angles boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f))); auto* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); // Add our custom Rotator script object (using the LuaScriptInstance C++ component to instantiate / store it) which will // rotate the scene node each frame, when the scene sends its update event auto* instance = boxNode->CreateComponent<LuaScriptInstance>(); instance->CreateObject(scriptFile, "Rotator"); // Call the script object's "SetRotationSpeed" function. LuaFunction* function = instance->GetScriptObjectFunction("SetRotationSpeed"); if (function && function->BeginCall(instance)) { function->PushUserType(Vector3(10.0f, 20.0f, 30.0f), "Vector3"); function->EndCall(); } } // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can // bring the far clip plane closer for more effective culling of distant objects cameraNode_ = scene_->CreateChild("Camera"); auto* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(100.0f); // Create a point light to the camera scene node auto* light = cameraNode_->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); light->SetRange(30.0f); }