Undac Posted June 23, 2015 Share Posted June 23, 2015 First of all, I would like to salute and wish "good luck with your projects" to everyone! Second, I just finished the tutorials and I have a few questions: 1. as a 'general' rule, when is advised to use c++, and when is advised to use lua scripts? 2. tables From what I've read on the tutorials, I don't quite get how tables work. At first I thought that when you initialize them, the constructor assigns them a data container type (array, struct, queue etc), but it seems that is it not the case. Anyway, I will read the Lua documentation soon and maybe I will make a better idea regarding how they work, but meanwhile I fail to understand how to create a vector of structs. For example, how can I translate these lines to Lua? struct MapSquare{ float height; int obj; float stature; }; MapSquare Map[10]; Map[0].height=1; I tried structMapSquare={ vectorMap={ height; obj; stature; }; } structMapSquare.vectorMap[0].height=3 print(structMapSquare.vectorMap[0].height) and I get the "attempt to index a nil value" err I noticed that the below code works... structMapSquare={ vectorMap={ } } structMapSquare.vectorMap[0]={} structMapSquare.vectorMap[0].value=3; so I ended up with this structMapSquare={ vectorMap={ height; obj; stature; }; } N=10; for i=0,N-1 do structMapSquare.vectorMap[i]={} end structMapSquare.vectorMap[0].height=3 print(structMapSquare.vectorMap[0].height) I don't quite understand why this works, and the first one does not. My best guess is that if I translate them in c++ syntax, I have nested structures and I did not declare vectorMap vectorMap[10]; I also don't know if this is a good way of using containers or not (what actually happens behind the code).. 3. passing parameters to functions I played a bit with Lua and it seems that variables are passed by value, while objects are passed by reference. It there a way to control this? PS: Sorry to bother you with this. PPS: It there a way to enable TAB on these forums - because that's not how I wrote what is between tags. Quote Link to comment Share on other sites More sharing options...
drarem Posted June 24, 2015 Share Posted June 24, 2015 1.) We're currently working on a single player FPS using LUA. For the most part it is all working well. If you know or want to learn LUA, it's definitely not as strict as C++. Lua is quick and easy to get set up. I came from a C++ world so learning curve was a little rough at first, but the more I work with it the easier it gets. http://store.steampowered.com/app/378280/ 2.) ?? I only do basic stuff with tables in Lua. 3.) You probably want to research Lua a bit, that isn't a leadwerks specific question really. 1 Quote Link to comment Share on other sites More sharing options...
Josh Posted June 26, 2015 Share Posted June 26, 2015 Tables are very flexible, and are a little different from the strictness of C arrays: Map = {} Map[0] = {} Map[0].height = 1 This lessons has a lot of information on tables:[/color] http://www.leadwerks.com/werkspace/page/tutorials/_/tables-r18 If I was writing a pretty complex game I would use C++ for the main game code and Lua for scripted objects in maps. Many games can be written with pure Lua though. It depends on what you are making and what your needs are. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Undac Posted June 26, 2015 Author Share Posted June 26, 2015 I read those lessons before starting the thread, and I thank you for posting them. I think however that you should also talk a bit about multi-dimensional arrays in the "Lua tables tutorials": http://www.lua.org/pil/11.2.html . How about my question, after browsing through the Lua documentation a bit, I realised that what I came up with at point 2) is a proper way indeed. If anyone is interested, you can find a couple of things regarding tables here: http://www.lua.org/gems/sample.pdf (pag5). "C++ for the main game code and Lua for scripted objects in maps." I was thinking to do all the "prototyping" with Lua, since it's faster, and to port the 'good' ideas to c++. You have a fair point, thank you! On the other hand, I have to learn how to pass values from lua to c++. "It depends on what you are making and what your needs are. " Party turn-based with RPG features (X-COM UFO Defense can give a decent idea of what I have in mind), so core engine modifications must be made. @drarem Good luck with your project! Quote Link to comment Share on other sites More sharing options...
Josh Posted June 26, 2015 Share Posted June 26, 2015 It is possible to communicate between Lua and C++. However, playing with the Lua stack is rather advanced. I'd like to include some additional functionality to ease this, like automatically generating the Lua glue code from the editor, but it is a detail only a minority of users are concerned with. Lua scripts can exist in their own world, simple things like doors opening and little behaviors, without needing to interact with your game logic. If you try to mix the two, it will be problematic, unless you have a good reason for it. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Rick Posted June 26, 2015 Share Posted June 26, 2015 Maybe a generic message system could help with some of this C++/Lua communication people are asking for. It wouldn't require glue code and messing with the Lua stack that much really and would be more streamlined. Basically in either C++ or Lua you can register to a ReceiveMessage() hook (don't forget passing extra param like some others do) and then you can call SendMessage() from C++ or Lua to trigger the hook. --from lua (all msg data is wrapped as a lua table) SendMessage("update.health", { self.health }) --from C++ (receive msg hook) ReceiveMessageHook(RECEIVE_MSG, &OnReceiveMessage, (void*)extra); // the LuaTable part would be the part needed to make this work. needs to be an easy interface to get data // from the table void OnReceiveMessage(const string msg, const LuaTable& tbl, void* extra) { } 2 Quote Link to comment Share on other sites More sharing options...
Charrua Posted June 26, 2015 Share Posted June 26, 2015 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 1 Quote Paren el mundo!, me quiero bajar. Link to comment Share on other sites More sharing options...
Undac Posted June 26, 2015 Author Share Posted June 26, 2015 @Charrua Another temporary (poor, but fast) solution would be to create a transfer function for lua and one for c++, put the transfer data into a buffer, save it into a file and load it into lua / c++ (don't forget to delete the content of that file before you read on it). @Josh Since it has RPG elements which will be implemented in c++, opening a door cannot be done without recieving data from c++ because that door might or might not trigger a trap, based on current character's perception (to notice it), intelligence (to figure out how to disable it) and dexterity (to be able to disable it). What I however think that "can be in it's own world" for my game is the implementation of quest-line. PS: I am not asking for these features: I understand that I am in the minority, that you have another things on your head, and I want to find my "own" solutions anyway (since this is the best way of improving yourself). I only posted because I don't really want to start with the left foot and to modify a big part of my main game code sometime mid-project and to see what other (& more experienced) people think. Cheers everyone! Quote Link to comment Share on other sites More sharing options...
juanqg Posted July 11, 2015 Share Posted July 11, 2015 Hi, I'm just getting started with leadwerks, sorry if this is not the right thread for my question. I'm following the tutorials particularly Jorn's Videos, so far very straightforward! Thank you for the great work! Here's my question; While following the videos for the Saturn Project (I believe that at video 11) Jorn started the concept of drawing the 2d / overlay, the boxes for health and health calculation are working fine but they're all black (including the draw texture!) they are shown just as black squares, the health bar did updated as expected but just the a black bar is reduced. I'm not pasting the actual "Script" tab here but I did choose the color and even if that were the issue what about the Texture then, why is black as well? Since I'm sure I'm missing something here, it is important to note that I'm using the Steam Version of LeadWerks, also I started the project from scratch, so maybe there is some setting (Perhaps at the World initialization) that I completely missed, the demo works fine the barrels falling, the collision, the door, etc. I could't figure out how to paste an screenshot here, so here is a piece of code is long so I'm going to thank you here for checking!: Script.size = Vec2(160, 25) --Vec2 "Size" Script.backgroundColor = Vec4() --color "Background Color" Script.healthBarColor = Vec4() --color "Health Bar Color" Script.player = nil Script.overlayPath = "" --path "Overlay" "Tex file (*tex):tex" Script.overlay = nil function Script:Start() self.player = self.entity:GetParent() if self.overlayPath ~= "" then self.overlay = Texture:Load(self.overlayPath) end end function Script:PostRender(context) -- Background local color = Vec4( self.backgroundColor.x / 255, self.backgroundColor.y / 255, self.backgroundColor.z / 255, self.backgroundColor.w / 255) context:SetColor(color) context:DrawRect(0,context:GetHeight() - (self.size.y * 2), self.size.x, self.size.y) -- HealthBar local healthFactor = self.player.script.health / self.player.script.maxHealth local color = Vec4( self.healthBarColor.x / 255, self.healthBarColor.y / 255, self.healthBarColor.z / 255, self.healthBarColor.w / 255) context:SetColor(color) context:DrawRect(400,context:GetHeight() - (self.size.y * 2), self.size.x * healthFactor, self.size.y) -- Draw Overlay if self.overlay ~= nil then context:DrawImage(self.overlay, 0, 0, 50, 30) end end Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.