Programming & Scripting

View unanswered posts | View active topics


Post a new topicPost a reply
Previous topic | Next topic 

  Trainee TraineeMember since: 03.08.2009, 11:47Posts: 153 Likes: 0
 

 Post Topic: Re: What is LUA used for?
PostPosted 05.06.2012, 08:24 
Wow, thanks for the help guys :)
I may use it to 'prototype' stuff the put them in C++ in GameDLL. I still feel the final product should be hardcoded, when you're dealing with adding a new movement system for example. But for some tweaks to existing stuff, yeah I guess you can use it instead.

  Uber Modder Uber ModderMember since: 11.08.2004, 12:38Posts: 2227 Likes: 51
 

 Post Topic: Re: What is LUA used for?
PostPosted 05.06.2012, 12:40 
Quote by robe_id:
sorry for my inocent question, how difficult is to learn LUA for someone that have a very high expertise level on c++?

Learning the Lua language itself should be easy, much of it is similar to C
Here's a small fragment from the flight model:
.
Code:
--************** DAMPING ***************************** 
if (UCVertForce > 1) then
    self.vz=self.vz/(1+self.GearVerticalDamping*self.dt);  -- Vertical damping (ground)
    self.wy=self.wy/(1+self.RollDampingGround*self.dt);  -- Roll damping
    self.wx=self.wx/(1+self.PitchDampingGround*self.dt);  -- Pitch damping
  else
    self.wy=self.wy/(1+self.RollDampingAir*self.dt);  -- Roll damping (in flight)
    self.wx=self.wx/(1+self.PitchDampingAir*self.dt);  -- Pitch damping
  end

.
As noted, one minor difference is that Lua uses -- for comments!
In this example all the variables are prefixed with 'self'. Although initially a slight inconvenience, it's actually a bit of magic. Of course, 'self' means it's a variable belonging to the local entity. But if you have the ID of another entity, say EntityID, then you could write code that directly changes variables in that entity e.g.
Code:
EntityID.vz = EntityID.vz / 3;

You can even call functions in another entity. This allows very easy communication between entities, something I use a lot.
You can declare local variables which are destroyed when execution exits the current function. That's why the variable 'UCVertForce' is not preceded by 'self'.
.
It's important to remember that Lua is case sensitive. Also, typing has been mentioned. The lack of typing in Lua probably does seem a bit weird, but in fact it's perfectly fine. Variables are typed according to the context, you don't specifically define variable types. There are probably different schools of thought on this, but all I know is that automatic context-sensitive typing works just fine. Also, where required you can easily convert numerical variables to string variables and vice versa.
.
Of course, learning the Lua language is only part of the job, you also need to know how to apply it to create new entities etc. There's an excellent tutorial by ins, on how to create entities with Lua. It's in the docs section. It was written for CE2 but it's applicable to CE3, but you can ignore the bit about mods. It was actually this tutorial that allowed me to make my first aircraft for CE2 (the code requires a few minor changes to work in CE3).
I have toyed with the idea of writing a tutorial that assumes no prior knowledge of Lua: 'Creating vehicles with Lua'. Whether I'll get around to doing it is another matter!
.
@Mr.Hankey
The Lua documentation is certainly far from perfect, for example many API entries still don't have any description at all, so you have to guess how to use it. Also some descriptions are pretty useless. For example, the description may refer to a table that is essential for use of the API function, but it gives no information on the actual table structure, making it essentially useless.
However, compared to the CE2 Lua API documentation, it is almost literally infinitely better. That's because Crytek did not release any Lua API documentation for CE2 at all. It was only because of help on the forums that I was able to make progress.
.
Finally, one extremely important note about Lua (only joking!)
Many people assume its name is an acronym, but it's not. Lua is actually the Portuguese word for 'moon'. Go figure, as the Americans say....
Regards,
Chris
User avatar   Trained Modder Trained ModderMember since: 20.09.2011, 23:12Posts: 214Location: mexico Likes: 14
 

 Post Topic: Re: What is LUA used for?
PostPosted 05.06.2012, 13:32 
Thanks cwright, I'll pass the text to a friend who is an expert in c + +, see whether it is encouraged to collaborate on a project of mine in CryENGINE. he handles c++, c#, phyton and java
  Trainee TraineeMember since: 03.08.2009, 11:47Posts: 153 Likes: 0
 

 Post Topic: Re: What is LUA used for?
PostPosted 08.06.2012, 17:34 
cwright, about your flying LUA code, where do you call it? are they custom flownodes which you call, or something else?
  Uber Modder Uber ModderMember since: 11.08.2004, 12:38Posts: 2227 Likes: 51
 

 Post Topic: Re: What is LUA used for?
PostPosted 09.06.2012, 12:12 
Quote by sceluk:
cwright, about your flying LUA code, where do you call it? are they custom flownodes which you call, or something else?

Hi sceluk,
The movement and rotation of the aircraft is controlled by the flight model, which is entirely written in Lua. It uses player inputs (e.g. joystick or mouse flight control) and it calculates all the forces generated by the wings, ailerons and flaps etc. When all forces are calculated it then calculates the acceleration, which of course depends on the mass of the aircraft. When the acceleration is known, it calculates the new velocity. And when the velocity is known it finally calculates the new position. It also calculates the new angles in a similar way.
.
The flight model is called from OnUpdate(), which means it is called once per frame. This makes it ideal for any real-time calculations.
When the position is calculated for each frame, the aircraft position is set by self:SetPos(x,y,z)
Setting the angles is a little more complex. self:SetAngles(angles) is no good, because it sets global angles. Angles must be set locally, otherwise aircraft rotation will be unrealistic.
I use a flowgraph node to set the angles. One node (for CE2) was specifically made to give local angle rotation, and a node in CE3 can be set to local rotation.
I also use several FG nodes for functions that can't be done directly in Lua, most notably to get keyboard input.
.
So, to summarise, all functions are programmed directly in Lua (around 6000 lines of code) but the code also uses several FG nodes for specific functions. Actually, it's possible I can replace the FG with Lua code, it's something I need to look into.
Hope that helps. If you want to know more or ask questions about the Lua implementation for flyable aircraft, why not ask on my thread:
viewtopic.php?f=280&t=25876&start=1260
By the way, I hope to release a small aircraft demo for CE3 in the near future.
Best regards,
Chris
  Trainee TraineeMember since: 03.08.2009, 11:47Posts: 153 Likes: 0
 

 Post Topic: Re: What is LUA used for?
PostPosted 09.06.2012, 18:32 
So you're calling your code from inside the c++ code right? and for a level to have aircraft, you need to import the FG's?
Is your flying code open source? so I can take a look and understand :)
  Uber Modder Uber ModderMember since: 11.08.2004, 12:38Posts: 2227 Likes: 51
 

 Post Topic: Re: What is LUA used for?
PostPosted 10.06.2012, 12:57 
Quote by sceluk:
So you're calling your code from inside the c++ code right? and for a level to have aircraft, you need to import the FG's?
Is your flying code open source? so I can take a look and understand :)


Hi sceluk,
My code is automatically called by the game engine, I don't use any additional C++ code. The system uses what are referred to as 'callbacks'. For example, if I add a function called OnInit() e.g.
Code:
function CL215:OnInit()
  do stuff....
end

then that code will be executed when the game is initialised, which occurs when the game is started. This allows any variables etc to be initialised when the game is started.
More important, the function OnUpdate() is automatically called once per frame, so this is ideal for real time control:
Code:
function CL215:OnUpdate()
  do real time stuff such as the flight model....
end

Simply using these standard function names ensures that the code will be run by the game engine.
.
Yes, it's completely open source. I've made a number of public releases which include all the aircraft Lua code. Here's an example:
https://dl.dropbox.com/u/4380128/AircraftUpdate3.zip
It's an obsolete update file that includes several aircraft Lua files. The last public release was 6 months ago (for CE2 only) but these files are very similar and will give you an idea of how they work. For example, to see the flight model function, search for 'flight_model'
The aircraft Lua files are located in Scripts\Entities\Aircraft
.
As a matter of fact I don't code each aircraft individually. As I have around a dozen aircraft in the hangar it would be a nightmare updating them all using copy and paste. Instead, I've created a utility named Aircraft Factory which automatically creates the Lua code. As I develop the code I update the Aircraft Factory code library. After developing new code features I can update all the aircraft in literally 30 seconds or so, simply be running them all through AF. AF also supports ships, submarines, trains and, now, spacecraft. It creates code for CE2 and CE3.
Best regards,
Chris
  Trainee TraineeMember since: 03.08.2009, 11:47Posts: 153 Likes: 0
 

 Post Topic: Re: What is LUA used for?
PostPosted 10.06.2012, 18:28 
Ah, I see. Thanks man :)
I guess it would be possible to translate all this to C++ and make them vehiclemovement types right?
Nice job on the aircraft, I checked out your thread and the work you've done looks amazing.

Thanks to everybody. I finally (kinda) understand what Lua's used for. heh.
  Uber Modder Uber ModderMember since: 11.08.2004, 12:38Posts: 2227 Likes: 51
 

 Post Topic: Re: What is LUA used for?
PostPosted 11.06.2012, 13:32 
Hi sceluk,
Thanks for the comments!
It would probably be fairly easy to translate the Lua code into C++, but I'm not sure if there would be any great advantage. Performance should be better, but only slightly. Although Lua code is normally saved as text, Lua is compiled, not interpreted.
.
All the coding I do is effectively standalone, for example it doesn't use the game engine vehicle system in any shape or form.
As I probably mentioned, one of the great things about Lua is the development cycle, it's incredibly fast, so you can easily add a line or two just for a quick test. It can literally be a matter of seconds between saving your edited code and running it in Sandbox.
Best regards,
Chris
  Beginner BeginnerMember since: 10.06.2012, 08:15Posts: 5 Likes: 0
 

 Post Topic: Re: What is LUA used for?
PostPosted 12.06.2012, 22:23 
Quote by kantaki:
Okay thanks, but we all now that CryMono is in development.

I know that mono should run around 50% as fast a c++ code, and lua should be at around 80%?
I also know that lua does not use OOP, it uses tables which should be similar.

What advantages has Lua over Mono ?


not sure how you comparing them, I was just trying to find stats like this too and from what I noticed:

C# takes on average 2-2.5x as much time to process similar things (it really depends, and you can't forget that having managed memory has a LOT of extra benefits other than just pure speed of execution)
LUA on the other hand from what I've read can be 20x slower or worse (again depending on what's being processed.

But like others have been saying, it's not necessarily the speed at which the operations are getting called, but the fact that you can test/implement the ideas a lot more quickly in LUA due to not needing to compile it everytime you make a change and want to test it.
  Trained Modder Trained ModderMember since: 05.04.2012, 14:13Posts: 254 Likes: 13
 

 Post Topic: Re: What is LUA used for?
PostPosted 13.06.2012, 10:53 
Quote by Sleaker:
Quote by kantaki:
...
LUA on the other hand from what I've read can be 20x slower or worse (again depending on what's being processed.
...


Lua is almost as fast as compiled C++




Intel i5-2500K 4x@4.9GHz@EKWB Supremacy Nickel water block
ASUS Maximus IV Gene-Z/Gen3 @Z68
Gigabyte HD 7950 @ 1100MHz/6000MHz@Alphacool NexXxos ATXP water block
24GB Corsair Vengeance 1600MHz CL10
  Uber Modder Uber ModderMember since: 11.08.2004, 12:38Posts: 2227 Likes: 51
 

 Post Topic: Re: What is LUA used for?
PostPosted 13.06.2012, 12:38 
In the docs here at Crydev:
http://freesdk.crydev.net/display/SDKDOC5/Script+Usage
it states
'Runtime performance - Careful usage of the available resources can result into scripts running nearly as fast as compiled code.'
This is probably not surprising. When doing any heavy-duty work, the Lua code is mostly calling functions that are compiled C++.
.
My aircraft Lua code has around 6000 lines of code, but even when there are lots of aircraft in the air (and they're trying to shoot each other down) I get excellent performance.
Regards,
Chris
User avatar   Uber Modder Uber ModderMember since: 19.03.2009, 12:21Posts: 2036Location: Sweden Likes: 34
 

 Post Topic: Re: What is LUA used for?
PostPosted 13.06.2012, 14:01 
Quote by Sleaker:
Quote by kantaki:
Okay thanks, but we all now that CryMono is in development.

I know that mono should run around 50% as fast a c++ code, and lua should be at around 80%?
I also know that lua does not use OOP, it uses tables which should be similar.

What advantages has Lua over Mono ?


not sure how you comparing them, I was just trying to find stats like this too and from what I noticed:

C# takes on average 2-2.5x as much time to process similar things (it really depends, and you can't forget that having managed memory has a LOT of extra benefits other than just pure speed of execution)
LUA on the other hand from what I've read can be 20x slower or worse (again depending on what's being processed.

But like others have been saying, it's not necessarily the speed at which the operations are getting called, but the fact that you can test/implement the ideas a lot more quickly in LUA due to not needing to compile it everytime you make a change and want to test it.


Image

Comparing the two languages (C# and Lua) is mostly meaningless, as they (as all programming languages do) have their ups and downs.

I won't go into detail where they "beat"eachother, but I just want to elaborate that when discussing CryMono you should not limit yourself to C#. That has been our core focus, but as we speak we are utilizing the Mono runtime to allow running multiple languages on CryENGINE optimally. We could even host a replication of CryENGINE's Lua implementation if we so wanted. :)


  Beginner BeginnerMember since: 27.03.2012, 14:18Posts: 22Location: Sweden Likes: 0
 

 Post Topic: Re: What is LUA used for?
PostPosted 13.06.2012, 14:08 
How do you spawn just your Lua based aircraft instead of the default player?
  Uber Modder Uber ModderMember since: 11.08.2004, 12:38Posts: 2227 Likes: 51
 

 Post Topic: Re: What is LUA used for?
PostPosted 14.06.2012, 14:53 
Quote by Mockarutan:
How do you spawn just your Lua based aircraft instead of the default player?

I'm not sure exactly what you mean, but here goes....
To load a Lua aircraft and fly it:
1. Select the aircraft entity from the rollup and drag it onto the terrain (i.e the same as any other game entity).
2. With the aircraft selected, load the standard flow graph (the same FG is used for all my Lua vehicles).
3. CTRL-G for game mode, walk up to the aircraft and press F. Start her up and take off...
Any more questions, don't hesitate to ask.
Chris