Stingray Forum (Read Only)
Welcome to Autodesk’s Stingray Forums. Share your knowledge, ask questions, and explore popular Stingray topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Forgive me I am a rookie to Lua and I have a question

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
thomalex89
440 Views, 2 Replies

Forgive me I am a rookie to Lua and I have a question

In the main_menu.lua file there is a statement

 

Project.MainMenu = Project.MainMenu or {}

 

If Project.MainMenu is not already a table then you make it one.

 

From little example I tried I came up with this.

 

--packed.car = {}     when I comment this out I get this attemp to index global 'packed' (a nil value) if I un-comment it I have no poblems it works fine.

 

packed.car = packed.car or {}

 

packed.car = 7
packed.bus = 2300

if(packed.bus > packed.car)then
print("This bus is packed and need to walk or take my car!")

else
print("I will take my car I like being alone")
end

for _, value in pairs(packed)do
print(value)
end

 

How does this "Project.MainMenu = Project.MainMenu or {}"  not produce an error?

Is Project already a table somewhere and does main_menu lua file have access through the project lua file?

Does it exisit in  core/appkit/lua/util' or  require 'core/appkit/lua/simple_project'?

 

Sorry for so many questions.

2 REPLIES 2
Message 2 of 3
kainevg
in reply to: thomalex89

Hi Thomalex.

 

The topic in which your questions are relating to is 'Modules' in Lua.

 

I'd recommend watching these videos (I recommend the whole series, up until video 16 which is the Modules video, however you can probably pick and choose if you want to):

Lua Basic Tables - KarmaKilledtheCat

Lua Modules by KarmaKilledtheCat

 

Also, this is the official Lua documentation on OOP (Again, this the document which this is from is very extensive, covering all aspects of the Lua language, so definitely worth a read):

Lua - OOP

 

Now to be a bit more specific!

 

To clarify, the line:

 

Project.MainMenu = Project.MainMenu or {}

isn't checking to see if it's already a table or not, it's checking whether or not Project.MainMenu == false. In Lua only two values are considered false, the first obviously being a boolean set to false, and the second is the 'nil' keyword. That's not really that important though.

 

 

In a module, you're creating a table, and everything you want to access from that module you will store in the table (this is a very surface explanation, for more detail check out the links I gave you). Looking at your code, both:

 

packed.car = {}

packed.car = packed.car or {}

should return errors (tested on my end and they both give the same error). This is because the variable 'packed'  does not exist. We'll get back to this in a second. First, let's get rid of 'packed.car' and we'll replace it with the variable name 'm' (It could be named anything, however I'm using to stand for module). So if we do that, you code now looks like this:

 

m = m or {}

 

m.car = 7
m.bus = 2300

if(packed.bus > packed.car)then
print("This bus is packed and need to walk or take my car!")

else
print("I will take my car I like being alone")
end

for _, value in pairs(m)do
print(value)
end

 

 

This is on the right track now, however there is one line of code that is missing. We've now defined a valid table, without referencing a nil value, however when another Lua module 'requires'  this script so it can use it, all that will happen is the script will be run, however you won't have access to any of the variables or functions you've put inside.

 

For example:

 

-- This code takes place in another Lua file, i.e Project.lua

local carLogic = require 'script/lua/car_logic' -- This is the file path to the lua script that 
-- contains our newly revised modules

print(carLogic.bus) -- Prints nil, however we'd expect it to print the value of variable bus; 2300

 

This is because when we're using the require keyword, the module we're 'requiring' is not returning anything to our variable 'carLogic', meaning we have no reference to the module and are therefore unable to access or use it's data. To fix this, all we need to do is return out modules table at the end of the script like so:

m = m or {}     --when I comment this out I get this attemp to index global 'packed' (a nil value) if I un-comment it I have no poblems it works fine.
--packed.car = packed.car or {}

 

m.car = 7
m.bus = 2300

if(m.bus > m.car)then
print("This bus is packed and need to walk or take my car!")

else
print("I will take my car I like being alone")
end

for _, value in pairs(m)do
print(value)
end

return m

Now when we test the code we get: 

-- This code takes place in another Lua file, i.e Project.lua

local carLogic = require 'script/lua/car_logic' -- This is the file path to the lua script that 
                                                -- contains our newly revised modules

print(carLogic.bus) -- Prints 2300

 

 

Now the reason Project.MainMenu does not return the same error is like you said, because Project has already been defined in the project.lua file. At the top you'll see:

 

-- This is the global table name used by Appkit Basic project to extend behavior
Project = Project or {}

the MainMenu script doesn't access it via Util OR SimpleProject. Project as you can see is set as a global variable (because it doesn't have the keyword local in front) meaning it can be accessed from anywhere in the program. Therefore, if you had declared the variabled 'packed' before hand, the error would be fixed as it is no longer referencing a nil value. For example:

packed = {}
packed.car = packed.car or {}

 

Keep in mind your original script will return an error (this is something to be very careful of) because if you look lower in your script you overwrite the table with a number value:

packed.car = packed.car or {} -- Sets the table to it's existing value or if it doesn't exist, creates a new one

packed.car = 7 -- You're now overwriting the table with the number 7
packed.bus = 2300 -- this isn't being stored in your module as it's a brand new variable

-- You would want something more like this:

packed.transport = packed.transport or {} -- same as before, however renamed the variable for clarity
local transport = packed.transport -- This just means when we want to access the table, to either store or retrieve variables, we only need to write transport.variable, instead of packed.transport.variable

transport.car = 7
transport.bus = 2300

(Note: Keep in mind I've cut off the other lines in this function needed, this still needs all the other lines we've discussed)

 

It's good practice to create the local variable as I did above so we only have to reference transport.variable to access data, it means your Lua is more clear.

 

I think that's everything.. I'd recommend you check out the video posted in this thread:

Stingray Lua Project - Ankur

 

It goes over creating a Stingray project using Lua without the Appkit, while the Appkit provides some great out of the box functionality, you don't get to learn how lua is interacting with the Engine, this should give you a better understanding, and then you can go back to using the Appkit.

 

I hope this has been helpful, if anyone spots mistakes please point them out! Let me know how it goes and if you have any questions at all 🙂

 

Cheers,

Kaine

 

Message 3 of 3
thomalex89
in reply to: kainevg

Hey thanks I had the feeling that it was global.  I love karamkilledthekat he does a great job.   That is what I thought.  I am learning.  Have a great day.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report