Jump to content

Noob question about including files


epsilonion
 Share

Recommended Posts

This is basic stuff really and a noob question.

 

 

Is it possible in lua to place functions in a seperate file and to call them from lua.app

 

 

What I am wanting to do is put the code for my main menu in a function or method in another file and have it loaded in to be called in the app.lua file.

 

something like

 

require_once ('scripts/functions.lua')

 

then call a function to display a menu

 

menu();

 

 

 

and in the functions file have all my functions like savegame, respawn, inventury etc.

 

I like to do things like this as it keeps everything nice and tidy, also easy to find things when problems arise...

Link to comment
Share on other sites

Yep you can do that. Look at one of the AI scripts to see how it includes the animation script. The animation script is more of a class but this shows how you can include lua files.

 

Just note that those lua functions are global and so if you don't keep track of your function names and you have a lot of files around you may end up overwriting one of your function definitions and Lua won't be kind enough to tell you that :)

Link to comment
Share on other sites

I am not getting error on the import("Scripts/functions.lua") now but its not doing it really..

 

this is the contents of the functions.lua

 

I get errors with the self. so i deleted it then errors on the context trying to index self. (nill value)

when I deleted the self I got the same but error trying to index contect (nill value)

 

I have googled it etc but not got much help there, I looked at some examples on the site and they work if you are using them in the editor on a asset but I want to call it from the App.lua file to tidy up the coding.. :D

 

function SplashScreen()

 

splash = Texture:Load("Materials/splash.tex")

 

if startUp >= 1 then

 

self.context:SetColor(0,0,0)

self.context:Clear() -- Clear the context

self.context:SetColor(1,1,1)

self.context:DrawImage(splash,0,0) --Draw the splash screen

self.context:Sync(true) -- Sync so it loads the screen

 

Time:Delay(10000) -- Time delay of 10 sec's while the splash is displayed

startUp = 0 -- set the startUp flag to 0 so the splash screen does not

-- display on the next pass in the loop.

 

end -- end the if statement

 

return startUp --return the startup flag value so the splash screen only runs on startup of the application

 

end

Link to comment
Share on other sites

There is no such thing as "self" inside a normal function. The "self" variable is only available in "classes". If you've ever used C++ "self" is basically equal to "this" or in VB to "Me".

 

If you want access to the context variable you can either use App.context (IF you are using a Lua only app) or Context:GetCurrent() as in

 

local context = Context:GetCurrent()

 

and now you can use context in your function. This is probably the best option.

 

"self" can be confusing if you don't know object oriented programming. It's actually a hidden variable that Lua gives you inside table functions when you use the syntax sugar of calling the function.

Link to comment
Share on other sites

I might not use Time:Delay(10000) as it will pause your game for 10 seconds. You could be doing other useful things in the background like loading the first map. If you get the current time, add 10000 and store it in TenSecondsFromNow you could then process what you need to then check if the current time > TenSecondsFromNow, if it is let the game continue.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   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.

 Share

×
×
  • Create New...