drarem Posted January 28, 2015 Share Posted January 28, 2015 How do I access a variable used in another script (LUA) attached to an object? Maybe wanting to see the monster's health during runtime for example? Thanks. Quote Link to comment Share on other sites More sharing options...
Rick Posted January 28, 2015 Share Posted January 28, 2015 myEntity.script.variable If there is a script attached to an entity it'll have the .script field which gives you access to all functions and variable of that script. 1 Quote Link to comment Share on other sites More sharing options...
drarem Posted January 29, 2015 Author Share Posted January 29, 2015 There is a script attached to an entity, but that's the pivot. The math is handled in the MonsterAI.lua and I want to get the health from there. The MonsterAI is 'prefabbbed' with a zombie prefab, not in the scene. Quote Link to comment Share on other sites More sharing options...
Rick Posted January 29, 2015 Share Posted January 29, 2015 Not following what you want. Can you show screenshot of the scene tab explaining what you want and from what script you want it in? Quote Link to comment Share on other sites More sharing options...
Aaron Symons Posted January 29, 2015 Share Posted January 29, 2015 Hiya, You can refer to the monster's health this way: Script.monster = nil --entity "Monster" function Script:Start() if (self.monster == nil) then Debug:Error("Please attach 'monster' script.") end end function Script:PostRender(context) context:SetBlendMode(Blend.Alpha) -- Get the current health local monsterHealth = self.monster.script.health -- Set the draw color to white context:SetColor(1, 1, 1) -- Display the health on screen context:DrawText(tostring(monsterHealth), 2, 2) context:SetBlendMode(Blend.Solid) end The first line will allow you to link the Monster's script to this one via the Leadwerks Editor view. Script.monster = nil --entity "Monster" Simply attach this script to a pivot in your scene. After doing so, a blank box should be displayed labeled "Monster". Drag and drop the Monster entity to this (from the Scene tree). You will now be able to access all variables and functions contained therein via this script. I hope this helps! Quote Win7 64-bit | Intel i7-3770 3.40GHz | NVIDIA GeForce GTX 660 Link to comment Share on other sites More sharing options...
Rick Posted January 29, 2015 Share Posted January 29, 2015 I was wondering if this is what he wanted to do. However I'm assuming that he would find this tedious to say the least as he could have a lot of monsters in his scene. Linking them all to some other script wouldn't be the most efficient way to handle such a thing which is why I think he's asking. The question comes down to when does he need to know about the monster in question. If he needs to know about the monster when the player is attacking it then he's get the monster he is attacking via other means. If he was shooting in a FPS game then you'd get it inside the player script via picking. At that point you would have that monster entity (if the raycast hit it) and then could access the script then. Generally speaking linking entities that you could have a lot of like enemies isn't the most efficient way to handle it. It's generally better to get these from either the collision function or picking functions. Quote Link to comment Share on other sites More sharing options...
Aaron Symons Posted January 29, 2015 Share Posted January 29, 2015 Hi Rick, I don't know why, but I never thought of that. Good points! I'll keep my mind open and get back if I think of something, if no one else does in the meantime. Quote Win7 64-bit | Intel i7-3770 3.40GHz | NVIDIA GeForce GTX 660 Link to comment Share on other sites More sharing options...
Aaron Symons Posted January 29, 2015 Share Posted January 29, 2015 How about a "MonsterManager" script that keeps track of each entity that are referred to in a Lua table? This could be done dynamically as well: as you load from the prefab, add the monster entity to the Lua table, then refer. The "MonsterManager" could also have a function to delete/kill the each monster in the table, perhaps by setting a boolean, eg: monsters[index].killed = true? I have a similar setup for displaying game achievements. I could adapt it to keeping track of entities. I can work on some code/pseudo code to better explain the idea if wanted. Quote Win7 64-bit | Intel i7-3770 3.40GHz | NVIDIA GeForce GTX 660 Link to comment Share on other sites More sharing options...
Rick Posted January 29, 2015 Share Posted January 29, 2015 Yep, that's what I did here http://www.leadwerks.com/werkspace/page/games/_/bombkiller-r39 This style works well for dynamically created entities if you want to track them. The question still remains from the OP why do you want to access all these entity scripts at any point in time from inside another script that you couldn't do with a collision function or picking method? Quote Link to comment Share on other sites More sharing options...
Aaron Symons Posted January 29, 2015 Share Posted January 29, 2015 The only reason I can think of is if you wanted a "herd" of entities to stay together, following a designated leader, perhaps? I suppose, unless i did want a "herd" for some reason, I'd still use Pick() or Collision(). It'd be simpler anyway. Quote Win7 64-bit | Intel i7-3770 3.40GHz | NVIDIA GeForce GTX 660 Link to comment Share on other sites More sharing options...
Rick Posted January 29, 2015 Share Posted January 29, 2015 My first thought on a herd would be ForEachEntityInAABBDo() to get other entities around you/bad guy/whoever but yeah you could go this route as well. Quote Link to comment Share on other sites More sharing options...
drarem Posted January 29, 2015 Author Share Posted January 29, 2015 I need a central bucket where i collect the stats, like deaths, kills, health, etc and display them in the main game loop. I find the above example provided by Aaron Symons works well, but how do I get the stats over to say another script which could save the current game with the stats? Or even just display all the stats in the primary screen renderer? Thanks. Quote Link to comment Share on other sites More sharing options...
BluHornet Posted January 29, 2015 Share Posted January 29, 2015 Well if you don't have a lot of game stats you could just use global variables. Those variables are accessible to all scripts. Quote Link to comment Share on other sites More sharing options...
drarem Posted January 29, 2015 Author Share Posted January 29, 2015 How would i define a global var in another script and access from my primary one? I have this in one script after attaching in the Start() local killc = enemy.script.health The drawtext shows 0, but the drawtext in the MonsterAI script shows the correct health. Quote Link to comment Share on other sites More sharing options...
drarem Posted January 29, 2015 Author Share Posted January 29, 2015 Figured something out.. I can call the function and do a return health into my main script. Thanks for all the help. Action.lua .. killc = entity.script.GetHealth() MonsterAI.lu function Script:GetHealth() return health end Quote Link to comment Share on other sites More sharing options...
Rick Posted January 29, 2015 Share Posted January 29, 2015 There should be no difference in entity.script:GetHealth() vs entity.script.health given the GetHealth() is returning health. How are you defining health in the MonsterAI file? Are you using self.health = value or local health = value? Normally you want to use self.health = value inside entity scripts. However, that said I agree that functions are better, but it's important to know why direct access didn't work. If health is defined as local health = 50 then I can see why you can't access it with entity.script.health as that's not the same thing as if it was defined as self.health = 50 -- if inside Script:Start() Script.health = 50 -- if defined outside any functions 1 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.