Jump to content

using variable from another script


drarem
 Share

Recommended Posts

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!

Win7 64-bit | Intel i7-3770 3.40GHz | NVIDIA GeForce GTX 660

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Win7 64-bit | Intel i7-3770 3.40GHz | NVIDIA GeForce GTX 660

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Win7 64-bit | Intel i7-3770 3.40GHz | NVIDIA GeForce GTX 660

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

  • Like 1
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...