norbert Posted January 10, 2014 Share Posted January 10, 2014 Hello, how can I access variables from other script ? function App:Loop() .. .. self.context:DrawText(self.FPSPlayer.Player.health,2,240) .. .. end is show the error "attempt to index field 'FPSPlayer' (a nil value)" can somebody help me. gruss norbert Quote Link to comment Share on other sites More sharing options...
Rick Posted January 10, 2014 Share Posted January 10, 2014 I would use the FPSPlayer's script PostRender() function to draw this. You can access the context in that script with: App.context:DrawText() I say do it this way, because App is global and you'll only ever have 1 in your program, so accessing it in all the other scripts makes more sense. This way you can leave App.lua alone. Drawing information related to the player should go with the player script so you aren't making a dependency in your App.lua file that you may change later. If you ever remove that FPS script your App.lua file will give an error. However, you'll never remove App.lua. 1 Quote Link to comment Share on other sites More sharing options...
norbert Posted January 10, 2014 Author Share Posted January 10, 2014 Thanks, it is possible to access the camera its create in FPSPlayer.lua from the app.lua script ? self.FPSPlayer.Player:Hide() gruss norbert Quote Link to comment Share on other sites More sharing options...
Rick Posted January 10, 2014 Share Posted January 10, 2014 So 'self' will only mean the current script you are in. So when you use it in App.lua it means the variables and functions only in App.lua. Why are you looking at doing that in App.lua? You could do that in the FPSPlayer script if you like with self.camera:SetClearColor(). I personally don't like that a camera is created in that script though. https://www.youtube.com/watch?v=dTaSP1fKD7c If you watch this tutorial you can see how I remove that script from making the camera and bring in a camera as an entity to the scene and bring it in as a parameter. Once you do this, you could make a script and attach it to the camera entity and do what you want. Generally speaking you probably want to avoid changing App.lua unless you really are comfortable with the engine and have a very good reason to. Quote Link to comment Share on other sites More sharing options...
beo6 Posted January 10, 2014 Share Posted January 10, 2014 One thing i always wonder: If i have something like player health in the player script as far as i know the health get reset as soon as i load the next map. The only thing i can think of is to have health global again and set the health on script load from the global value. But i am not sure if that would be the best idea. And the same would get even more complicated with an inventory script. Quote Link to comment Share on other sites More sharing options...
Rick Posted January 10, 2014 Share Posted January 10, 2014 This depends on if you need that information between runs of the exe too. Most of the time if you need that information saved between maps you also need it saved between runs. In that case I'd save the information to disk and load it back on each map startup. Leadwerks has file commands: http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/stream/ http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/filesystem/ Use GetAppDataPath() to get the path your application should write too. You can then create any folder/file under this path for your game (like a save file). This makes sure it writes to the correct places per OS that is allowed by any security measures that OS has. If you only did want to save between maps then yes, it would be best to make a global structure that stores all the information you need, then load it back to the script information on a new map. I would make Save()/Load() functions that maybe do both so it's abstracted away. Quote Link to comment Share on other sites More sharing options...
beo6 Posted January 13, 2014 Share Posted January 13, 2014 Thanks. Good idea saving it to disk. That way i don't need to add global application variables. 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.