Jump to content

Charrua

Developers
  • Posts

    230
  • Joined

  • Last visited

Everything posted by Charrua

  1. 4) How to call an entity's script LUA function passing parameters following code is some what weird but you only have to pay attention on a few things the idea is Push the parameters in the order they are declared in the lua function (from left to right) and pay attention in the Interpreter:Invoke method. You have to set the first parameter (number of staked values) one more of the amount of parameters passed. consider the following LUA function: function Script:turnMe(turn) System:Print("calling me from C") self.entity:Turn(turn.x,turn.y,turn.z,true) end note that "turn" is a Vec3 variable so you have to push the correct type of variable to lua stack and then tell Interpreter::Invoke that there are 2 things on the stack (as the code explains, one is always present: thank's Josh for that beautiful piece of code!) here is a generic function to call any LUA script function that has one parameter of type Vec3: bool call_turnMe(Entity* e, string funcName, Vec3 vec3){ if (e->component == NULL) return false; bool success = false; //Get the component table int stacksize = Interpreter::GetStackSize(); //Get the global error handler function int errorfunctionindex = 0; #ifdef DEBUG Interpreter::GetGlobal("LuaErrorHandler"); errorfunctionindex = Interpreter::GetStackSize(); #endif e->Push(); Interpreter::GetField("script"); //test if entity has a script if (Interpreter::IsTable()) { System::Print(" entity has script"); Interpreter::GetField(funcName); if (Interpreter::IsFunction()) //test if funcName is a function { System::Print(" " + funcName + " defined"); Interpreter::PushValue(-2); //Push script table onto stack Interpreter::PushVec3(vec3); //we only pass one variable but stack has one more #ifdef DEBUG errorfunctionindex = -(Interpreter::GetStackSize() - errorfunctionindex + 1); #endif success = Interpreter::Invoke(2, 0, errorfunctionindex); // send 2 params, receive none (next tut for receive parameters) } } Interpreter::SetStackSize(stacksize); //keep stack under control } Interpreter has a nice amount of pushes: pushAABB, pushFloat, pushInt, pushString, pushCObject, pushObject, pushBool, pushNull, pushMat3, pushMat4, pushVec2, pushVec3, pushVec4, pushValue just remember: push parameters with the correct type and in order in which they was declared in the LUA function. next tut, show how to pass 3 ints and receive a float, and also show how to receive more than one value from a lua script, because Return in lua may contain a list of values. that's it Next, Home, Previous
  2. 3) How to call an entity's script LUA function easely (without parameters) That's really easy. Suppose you have an entity in your scene with a script attached in which a function "noParams" has been declared function Script:noParams() System:Print("executing noParams") end using the loop exposed on tut 2/7 of this series, you can find that entity and then, to call one of their functions you only have to do: entity->CallFunction("noParams"); // yes only the name also you may check if the entity has a script at all: this is a more generic function to call functions with no parameters: void call_noParams(Entity* e, string funcName){ if (e->script != NULL) e->CallFunction(funcName); // CallFunction only if entity has a script attached } that's it! Next, Home, Previous
  3. 2) How to send objects created by Cpp code to LUA Here is a way of sendign CPP created objects to LUA scripts as GLobal Variables, so any LUA script on you map can simply use it. I create on cpp: window, world and context and pass them this way: System::Print("sending objects...."); stacksize = Interpreter::GetStackSize(); Interpreter::PushObject(window); Interpreter::SetGlobal("window"); Interpreter::PushObject(context); Interpreter::SetGlobal("context"); Interpreter::PushObject(world); Interpreter::SetGlobal("world"); Interpreter::SetStackSize(stacksize); so simple, only after knowing it! i modify App.lua to: not define window, world and context and use them as: if window:Closed() or window:KeyDown(Key.Escape) then --logFile:Release() return false end --Update the app timing Time:Update() --Update the world world:Update() --Render the world world:Render() note : that's a fragment of App.lua main loop (function App:Loop() ) instead of the default: if self.window:Closed() or self.window:KeyDown(Key.Escape) then --logFile:Release() return false end --Update the app timing Time:Update() --Update the world self.world:Update() --Render the world self.world:Render() with tuts 1 and 2 we has ways of sending Leadwerks objects (any Leadwerk's object) to LUA and retrieving LUA objects. Objects sent to lua, will be global to any function on any LUA scritp. Next, Home, Previous
  4. 1) How to get objects created on the editor from Cpp Easy as it seems, getting an editor created object from cpp code is done basically looping on the entities collection, but there are some things that is good to know. First one simple way of doing it: System::Print("start scanning...."); for (auto iter = world->entities.begin(); iter != world->entities.end(); iter++) { Entity* entity = *iter; System::Print(entity->GetKeyValue("name")); if (entity->GetKeyValue("name") == "findMe"){ System::Print(" found entity findMe !!!"); if (entity->script != NULL){ foundEntity = entity; } } } It's supposed that you have a map with an entity named "findMe" if you like you can use: SetKeyValue to give some "custom" attributes to your entities and then instead of testing by a already existing key like name you may test by any custom key of your's if (entity->GetKeyValue("room_number") == "8"){ System::Print(" entity of room 8"+entity->GetKeyValue("name")); if (entity->script != NULL){ foundEntity = entity; } } entities collection has all of them, cameras, lights, models, etc but in some particular situations searching for specific class is necessary: cameras, light has special methods and to work properly it's better to use the correct object. for example to get a camera created on the editor you may: //now look for the editor camera: for (Camera* e : world->cameras) { System::Print(e->GetKeyValue("name")); if (e->GetKeyValue("name") == "EditorCamera"){ camera = e; System::Print("found editor camera"); } } note "camera" is declared on app.h as: Camera* camera; so it's a global already defined that's it, so simple, he? Next, Back
  5. Thnigs i will cover: 1) How to get objects created on the editor from Cpp 2) How to send objects created by Cpp code to LUA 3) How to call an entity's script LUA function easely (without parameters) 4) How to call an entity's script LUA function passing parameters 5) How to receive returned parameters from LUA functions (yes one or more) 6) How to expose a Cpp Class to LUA using tolua++ I will split this post acordingly to the items listed above for easy referencing/reading Here a complete Leadwerks app: cpp source, map and scripts of a working example of the all above. //no longer valid! https://dl.dropboxusercontent.com/u/78894295/leadwerks/cppLuaBindings.zip //2021/12/14 update https://drive.google.com/file/d/1JdewNls8joRJygZ5zkNnuHrnCqNqDl_S/view?usp=sharing (see description of what executable does and how to operate it on a note at the end of this post) I'm not English native, so apologies if i do linguistic mistakes, and i'm not a so experienced cpp programmer (ok i'm not a programmer, I'm a technician in electronics, so for any inconsistency or semantic error/mistake pm me and i'll edit/modify the post, thank's) This site has many topics about this subject but for someone like me many of them has almost all, but normally there are some or too much details which are not exposed. Normally it takes a while to get to the point: do try, pray, try again, reread until ok OR tooLate My intention is to give "allincluded" for new Leadwerks cpp users which is asumed that if they are cpp programmers normally are advanced ones, but being new to Leadwerks normally the learning curve is shorten with material like i'll expose. Here some interesting links on the subject (not all of them) http://www.leadwerks.com/werkspace/topic/11007-calling-lua-script-function-from-c-with-arguments/page__hl__callfunction http://www.leadwerks.com/werkspace/topic/7548-access-cpp-global-via-lua/page__hl__tolua#entry60531 http://www.leadwerks.com/werkspace/topic/8802-tolua-exposure-problems/ http://www.leadwerks.com/werkspace/topic/7866-lua-version-in-30/ http://www.leadwerks.com/werkspace/blog/138/entry-1288-pushing-complex-lua-tables-from-c/ there are many references to: http://www.leadwerks.com/werkspace/files/file/216-tolua/ but this just display: sorry we couldn't find that! so, here is the tolua++ source https://github.com/LuaDist/toluapp documentation https://www8.cs.umu.se/kurser/TDBD12/VT04/lab/lua/tolua%2B%2B.html precompiled http://www.codereactor.net/tolua-windows-precompiled-binary-exe-file-download-and-more-instructions-on-tolua/ (note, the link addressed by autors to download: tolua++ can be downloaded from the sites below: http://www.codenix.com/~tolua/ is broken! ) How hard is to get all the links up to date! isn't it? Items coming soon... jio --------------------------------------------------------------- Notes about the executable: In the terminal window, each Lua function and cpp method throws some info, so move the main window a little to the right, so you can see the terminal window. You'll se something like that, without my comments in italic: app.lua start //things that happens LUA side App.lua Start c2luaTest not nil //instance of object form c created and not nil lua is calling me! //calling an object's method from lua OK 3.3561e+008 //calling average: (10+35+15)/3 :(WRONG! see bellow) start scanning.... //this is cpp side on App.cpp, App::Start //list of objets from the map (world) Directional Light 4 EditorCamera findMe //objet searched found entity findMe !!! // find OK, set a pointer to it for future use. Directional Light 4 EditorCamera //now searching only for cameras found editor camera //found editor camera, got camera from editor! now, put manin window on focus. Press a, s, d, f a: calls average(10, 15, 25) and print the results on the terminal window s: calls average( 5, 10, 4) and print the results on the terminal window d: calls turnMe(vec3) with a default value of: turn.x=10; turn.y = 20; turn.z = 30; if ok, you see the red cube turn this amount per axis on each keyhit. f: calls noParams() and print "executing noParams" As you press each letter, new messages appear on the terminal window confirming functions of lua correctly called from c++ code. if you press a, s, d, f in this sequence, you should see entity has script average defined executing average 16.6667 entity has script average defined executing average 6.33333 entity has script turnMe defined calling me from C executin noParams WRONG display in calling average cpp method from LUA, guess what? a bug. in Start function inside App.lua i do: c2luaTest.intA = 10 c2luaTest.intA = 35 c2luaTest.intA = 15 System:Print(c2luaTest:average()) so I only initialize intA, leaving intB and intC with garbage, so, expect to see any number on each run! Please edit the App.lua for me!
  6. thxs agrror is back again!, i thought he stopped flowgui dev, seems that an update is coming jio
  7. Hi As the subject says. Last forum thread about cegui is about 5 years, I spent last weekend without success. i like to know if currently someone get it working with leadwerks and if it worth the effort. I need a gui for leadwerks, so any advice about some other gui would be appreciated I was testing awesomium but it´s just an interface for a html generated web page/form/dialog and so you have to start codign html, javascript... jquery, jquery UI... i do like a gui that has a layout editor and/or create windows, controls etc, by code. thank's in advance jio
  8. Hi I need 5 cameras and show what each is seeing in separate views. the description of the function SetRenderTarget says: so, i create the cameras, create the textures and set them as render target but textures don't get updated as i guessed. so textures can be drawn on screen but don't get updated. Searching i found this thread http://www.leadwerks.com/werkspace/topic/13211-trouble-with-render-targets/page__hl__setrendertarget#entry93400 and seems that SetRenderTarget can't be used as i supposed it should be. I started to use buffers but seems that i have to do one render world per camera/buffer to get what i want. is it possible to do the same thing without 5 renders? in a smart way which i don't imagine here is the code i get working #include "App.h" using namespace Leadwerks; App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {} App::~App() { delete world; delete window; } Model* models[5]; //just to show something Camera *cams[5]; // five cameras one for each view Buffer *buffs[5]; // one buffer per view const int numViews = 5; bool App::Start() { window = Window::Create(); context = Context::Create(window); world = World::Create(); Light* light = DirectionalLight::Create(); light->SetRotation(35, 35, 0); // create one object per view models[0] = Model::Box(); models[0]->SetColor(1.0, 0.0, 0.0); models[0]->SetPosition(-4, 0, 0); models[1] = Model::Cylinder(); models[1]->SetColor(0.0, 1.0, 0.0); models[1]->SetPosition(-2, 0, 0); models[2] = Model::Cone(); models[2]->SetColor(0.0, 0.0, 1.0); models[2]->SetPosition(0, 0, 0); models[3] = Model::Cylinder(); models[3]->SetColor(0.0, 1.0, 1.0); models[3]->SetPosition(2, 0, 0); models[4] = Model::Box(); models[4]->SetColor(1.0, 0.0, 1.0); models[4]->SetPosition(4, 0, 0); //create each camera and buffer for (int i = 0; i < numViews; i++){ cams[i] = Camera::Create(); cams[i]->Move(-4 + i * 2, 0, -6); cams[i]->Hide(); buffs[i] = Buffer::Create(context->GetWidth() / numViews, context->GetHeight()); } return true; } bool App::Loop() { if (window->Closed() || window->KeyDown(Key::Escape)) return false; for (int i = 0; i < numViews; i++){ models[i]->Turn(Time::GetSpeed()*0.2*(i+1), 0, 0); } Time::Update(); world->Update(); for (int i = 0; i < numViews; i++){ buffs[i]->Enable(); cams[i]->Show(); world->Render(); //is it neccesary to do 5 world renders? cams[i]->Hide(); buffs[i]->Disable(); } context->Enable(); //draw each buffer for (int i = 0; i < numViews; i++){ context->DrawImage(buffs[i]->GetColorTexture(), i*context->GetWidth()/numViews, 0); context->DrawLine(i*context->GetWidth() / numViews, 0, i*context->GetWidth() / numViews, context->GetHeight()); } context->Sync(); return true; } thank's in advance jio
  9. find a way to send keystrokes! i'm using something like: WebKeyboardEvent keyEvent; char chr; for (int i=1; i<256; i++){ char chr=i; keyEvent.type = WebKeyboardEvent::kTypeKeyDown; keyEvent.native_key_code = chr; webView->InjectKeyboardEvent(keyEvent); keyEvent.type = WebKeyboardEvent::kTypeChar; keyEvent.text[0] = chr; webView->InjectKeyboardEvent(keyEvent); keyEvent.type = Awesomium::WebKeyboardEvent::kTypeKeyUp; keyEvent.native_key_code = chr; webView->InjectKeyboardEvent(keyEvent); } with this code, there are no A/a upper/lower so some key mapping will be necessary
  10. OK, thank's to Rick y now have a complete working example of integrating leadwerks and awesomium the example now has 2 buttons, one to SayHello the other to exit the application and work as expected! 7z file has the Source folder with all the source files needed, those files which came from awesomium form part of one of the tutorials general gelp on awesomium: http://wiki.awesomium.com/getting-started/ tut files: https://github.com/awesomium/tutorial-framework/archive/master.zip complete c++ source and executable for you to try: https://dl.dropboxusercontent.com/u/78894295/leadwerks/awe1.7z had happens to me that the first time i run seem not to respond to mouse down events... but works ok if exit and run again do not know why can it be! enjoy jio
  11. at least should be goo to get a working all included example to start with I'll will update this thread as soon as i get something new to show. (too much information for me to digest.. it will take a while...) thank's again jio
  12. rick, i was writing my second post when you answer my first thank's for your input, i have to read it carefully about keyboard input... je je, the docs say not to much... even tells us to read some other doc which has no link! would be nice if some one else has a code for that jio
  13. forgot to include an executable for you to try if you change the boton.html for another more elaborated html it should work also. but i'm only using 300x200 pixels for the ui texture, so not too much space for being much creative. int the app dir, a snapshoot of the html as is after loaded is saved as result.jpg here the 7z file: https://dl.dropboxusercontent.com/u/78894295/leadwerks/awe.7z enjoy jio
  14. Hi, i was trying to get awesomium to work with leadwerks. Mi knowledge about javascript and html are almost nothing, (ok, my knowledge of c++ isn't too much also) I need a ui for leadwerks, which is a must, i guess. The following code is what i could make to work: 1) load a local html file 2) on every loop send mouse events to the html "web view" get how it look on a leadwerks texture show it on the screen (context->DrawImage) What i miss is how to bind java events to c++ functions Does someone has a simple example of how to do that. A snapshoot: here the complete source: /* Awesomium - Leadwerks tests by Juan Ignacio Odriozola, aka charrua http://wiki.awesomium.com/getting-started/ What this code does load a simple html page: just a label and a button get what it show and copy that info to a Leadwerks texture Send to the html mouse events Display the texture on screen with a cube rotating on the 3d space if you hover the button, mouse down or mouse up, the control draws as spected. What it doesn't the code do not bind button press and do not call any c++ function OnMouseHit SO I'm asking if anyone has a simple code to showme how to do that. I'm know nothing about html neither javascript, i will try to learn abot them if i got awesomium to work with leadwerks! Hope this code will catch interest in other about awesomium or short the learning path that's the html source of "boton.html": <html> <body> <h1>Awesomium Test</h1> <button onclick="app.sayHello()">Say Hello</button> </body> </html> */ #include "App.h" #include <Awesomium/WebCore.h> #include <Awesomium/BitmapSurface.h> #include <Awesomium/STLHelpers.h> using namespace Awesomium; WebCore* web_core = NULL; WebView* my_web_view = NULL; BitmapSurface* uiSurface = NULL; Texture* uiTex = NULL; unsigned char* pixels = NULL; using namespace Leadwerks; App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {} App::~App() { delete world; delete window; } Model* model = NULL; JSValue jsResult; bool App::Start() { window = Window::Create(); context = Context::Create(window); world = World::Create(); camera = Camera::Create(); camera->Move(0, 0, -3); Light* light = DirectionalLight::Create(); light->SetRotation(35, 35, 0); // Create the WebCore singleton with default configuration web_core = WebCore::Initialize(WebConfig()); // al salir: WebCore::Shutdown(); my_web_view = web_core->CreateWebView(320, 200); WebURL url(WSLit("file:///./boton.html")); //acceso al dir local! my_web_view->LoadURL(url); uiTex = Texture::Create(320, 200); pixels = (unsigned char*)malloc(uiTex->GetMipmapSize(0)); // Wait for our WebView to finish loading while (my_web_view->IsLoading()) web_core->Update(); Sleep(300); web_core->Update(); uiSurface = (BitmapSurface*)my_web_view->surface(); // para esto hay que: #include <Awesomium/BitmapSurface.h> if (uiSurface != 0) { uiSurface->SaveToJPEG(WSLit("./result.jpg")); //just to verify html was loaded and how it look! uiSurface->CopyTo(pixels, uiTex->GetWidth() * 4, 4, true, false); uiTex->SetPixels((char*)pixels); } // now a cube and btw texture it with the html view //Create a material Material* material = Material::Create(); //use uiTex as texture material->SetTexture(uiTex); //Load and apply a shader Shader* shader = Shader::Load("Shaders/Model/diffuse.shader"); material->SetShader(shader); shader->Release(); //Create a model and apply the material model = Model::Box(); model->SetMaterial(material); model->SetPosition(0, 0, 1); return true; } bool mouseLeftDown = false; bool App::Loop() { if (window->Closed() || window->KeyDown(Key::Escape)) { free(pixels); my_web_view->Destroy(); WebCore::Shutdown(); return false; } model->Turn(Time::GetSpeed()/3, Time::GetSpeed()/4, Time::GetSpeed() / 2); //get new pixel data from html view if (uiSurface != 0) { uiSurface->CopyTo(pixels, uiTex->GetWidth() * 4, 4, true, false); uiTex->SetPixels((char*)pixels); } // Inject a mouse-movement my_web_view->InjectMouseMove(window->MouseX(), window->MouseY()); if (window->MouseDown(1)){ // Inject a left-mouse-button down event my_web_view->InjectMouseDown(kMouseButton_Left); mouseLeftDown = true; } else { if (mouseLeftDown) { mouseLeftDown = false; // Inject a left-mouse-button up event my_web_view->InjectMouseUp(kMouseButton_Left); } } web_core->Update(); Time::Update(); world->Update(); world->Render(); context->DrawImage(uiTex, 0, 0); context->Sync(); return true; } more snapshoots, when mouse hover the button and when i'ts clicked thank's in advance jio
  15. ok solved the init errors.. (most my fault)! now, i think i get lost forever, when i try to update the gui, then: i tested version 1060 and 1110, 1060 do not pass the initiate function version 1110 pass the init (initiate is deprecated in this new version) so some checks like valid window and graphics devices are passed. i can call many of the functions to create and set parameters, just update seems to fail
  16. Hey, thank's for you fast response:) the first step seemed to be the easiest one and... i'm stuck on it! but when i call the init function the message isn't about window o graphics pointers ... is about that the pointer to input is null. so... i'll keep trying
  17. Hi i'm trying to connect Leadwerks to a gui, which development has stopped, but that i used and use at present with other engines (albalynx). One of the firsts steps consist in passing pointers of the window's window and the graphics device (openGL i guess), albalynx is supposed to support DirecX9,10,11 and OpenGL 4 I found some functions and properties like: Context* context->getCurrent(); Window* window->getCurrent(); window->whnd; i readed about Window:GetHandle() http://www.leadwerks.com/werkspace/topic/10381-requested-features-and-improvements/page__st__40#entry76787 but seems to not exists. any help is appreciated! thank's in advance jio
  18. i always been interested in this subject i know that the following isn't the best way, but is better than nothing basically i use the GetKeyValue and SetKeyValue to store pairs: key,value on a "messenger" entity i create a pivot named "messenger" in my map (or the number needed) and name it the same then in c++ scan the world.entities and catch that entity. once i have it's handle (address, pointer...) then i have a simple, brute force, but working line of comunication: -------------------------------------------------- more explained create a messenger object (a pivot) in your map name it "messenger" in your lua start function you may: self.messenger:SetKeyValue("Hit","0") self.messenger:SetKeyValue("xPos","0") self.messenger:SetKeyValue("yPos","0") -------------------------------- c++ side Entity* messenger; //after map load for (auto e : world->entities){ if (e->GetKeyValue("name") == "messenger"){ messenger = e; //catch messenger entity } } //from c++ ent->SetKeyValue("Hit", "1"); //set Hit property to 1 to signal something spected in lua --------------------------------- lua side //UpdateWorld perhaps if self.messenger:GetKeyValue("Hit")=="1" then self.messenger:SetKeyValue("Hit","0") // may signal : readed //do whatever HIT does/needs end (sorry my bad English) Juan
  19. thxs i'm going to do so i receive a pm from josh about a steam key so, thread resolved!
  20. i have the stand alone version 3.3 is only for the ones that has steam version? my updater tells me that there are no files to update. do i have access to that version? do i have to pay for it? i was off for a while, do i miss something obvious? thank's in advance Juan
  21. hi i am getting some troubles, due to my lack of knowledge. the easy part is: add #include "lua.h" in your App.h so, any scripted object will work, but: if you create a camera in your map (via editor) you have to make some tricks to use that camera and not create another via c++ your initila c++ code, creates a context, window etc, you have to write your scripts using those objects... you may write something like this on your Scripts functions: function Script:PostRender() local window = Window:GetCurrent() local context = Context:GetCurrent() if window:KeyHit(Key.M) then self.showtext = not(self.showtext) end if self.showtext context:SetBlendMode(Blend.Alpha) context:SetColor(255,255, 255) context:DrawText("hello", x, y) end end as i get some troubles using a camera defined in c from lua scripts (sure i am doing something wrong) i define the camera inside the editor and then i use that camera from c++ see: http://www.leadwerks.com/werkspace/topic/10154-set-camera-to-a-camera-in-the-scenelevelmap/ Juan
  22. thank's i have to seriously start studying c++ and lua by the moment i have to evade call a script function from c++, till i get older! thank's again
  23. i have many objects with scripts attached if i get one of them via collision or ray pick, can i call a function declared inside the entity's script? something like pickinfo.entity.script.Hit(), if Hit is defined inside the script thank's in advance Juan
  24. i used this to use an entity rotation and transform it to a normalized rotation, perhaps is very indirect, but seems to work guess there should be a simpler mathematical way, but still unknown for me. hope that helps: if self.usepivotpin then local pvtTemp = Pivot:Create() --get ent rotation, to set hinge pin if pvtTemp ~= nil then pvtTemp:SetRotation(self.entity:GetRotation()) pvtTemp:Move(0,0,1) --transform from euler angles to 0..1 axis coords ... a vector of lenght one: nomarized local rot = pvtTemp:GetPosition(false) self.pin.x = rot.x self.pin.y = rot.y self.pin.z = rot.z --System:Print("pin: " .. self.pin.x .. ", " .. self.pin.y .. ", " .. self.pin.z) pvtTemp:Release() end end i used to set hinge joint pin, btw
×
×
  • Create New...