Jump to content

Leadwerks 3.1 Indie access variables from other script ?


norbert
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

  • Upvote 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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