Jump to content
  • entries
    945
  • comments
    5,899
  • views
    929,842

Some backwards-compatible changes to the main script


Josh

3,318 views

 Share

The design of the App table in Lua was originally created to work with mobile, which has a more restricted program flow. The application calls App:Start() once to initialize it, and then will continually call App:Loop() until the function returns false. These two functions are found in the file "Scripts\App.lua".

 

Since we are focused on the PC now, it does not make sense to keep this structure. It also makes it more confusing to teach scripting. A conventional program layout with one main script is much simpler than having two functions that are called by the interpreter itself. We are going to transition to a conventional program layout in a way that doesn't break compatibility with existing scripts.

 

First, if you do nothing and keep the App.lua script as it is, your program will continue to work. However, your App.lua file no longer is required to contains the App table, or the two Start() and Loop() functions. If your App.lua file just contains text like this, it will work just fine:

window = Window:Create()

context = Context:Create(window)

world = World:Create()

local camera = Camera:Create()
camera:Move(0,0,-3)

local light = DirectionalLight:Create()
light:SetRotation(35,35,0)

model = Model:Box()
model:SetColor(0.0,0.0,1.0)

while true do
   if window:Closed() or window:KeyHit(Key.Escape) then return false end

   model:Turn(0,Time:GetSpeed(),0)

   Time:Update()
   world:Update()
   world:Render()
   context:Sync()
end

 

Doesn't that code look simpler?

 

Second, we are going to introduce a new file called "Main.lua" into the project template, and remove App.lua from the original template. If both App.lua and Main.lua are present, then App.lua will be called. If only Main.lua is present, then it will be used. This means that new projects will use our preferred method of calling Main.lua, but existing projects will be unaffected, even when the project is updated.

 

All existing code examples in the documentation will continue to work when pasted into App.lua or Main.lua.

 

TLDR: Changes are coming but it won't affect your existing projects.

 Share

8 Comments


Recommended Comments

This seems fine, but the trend we are seeing is Lua getting a ton of focus and C++ sort of being pushed aside. Now I'm fine with that as long as you don't drop allowing us to code in C++. Your pattern generally becomes removing features when you deem something else is best to focus on (instead of just leaving it there) and I just want to make sure you aren't thinking of removing us from having the C++ library to code in and just giving us a pre-built exe and force us to only use Lua.

  • Upvote 4
Link to comment

There are no plans to change support for C++. Many people are still buying the standard edition. C++ programmers usually already know what they want to do, so the tutorials aren't focused on them.

  • Upvote 1
Link to comment

Many people are still buying the standard edition.

I wonder if you've taken into consideration that C++ costs twice as much as Lua. In fact, considering that it's the only feature the DLC adds, you could say it costs a lot more.

Link to comment

I'm sorry if this is a stupid question, but for accessing global variables from the new Main.lua, would we instead use Main.myVar rather than App.myVar ?

Link to comment

I'm sorry if this is a stupid question, but for accessing global variables from the new Main.lua, would we instead use Main.myVar rather than App.myVar ?

It would just be myVar.

 

You can still use the App table in the main script if you want to, but it's not necessary. You don't need to change any code unless you prefer the simpler style that Main.lua uses.

Link to comment

So how does this work technically? Does the engine automatically load Main.lua if it exists? Looks like the source C++ files did not update. I'll most likely convert my game to use this later.

Link to comment

If App.lua is present, that takes priority. Otherwise it falls back to Main.lua.

 

No matter which file it uses, it first runs the script, then looks for the App:Start() and App:Loop() function. If they aren't found it quits without warning (because the whole game ran when the script was first run).

 

The result is it works with the new way, and old projects are unaffected.

 

The standard edition source is being updated now.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...