AwfulRanger Posted November 5, 2014 Share Posted November 5, 2014 I'm working on a medkit, and I need it to get the player's max health, along with the current health. I've tried a few things, but I can't seem to get it working. This is what I have in medkit.lua:local assets ={ Asset("ANIM", "anim/bandage.zip"), }local function fn( Sim ) print( "medkit loaded" ) local inst = CreateEntity() inst.entity:AddTransform() inst.entity:AddAnimState() MakeInventoryPhysics( inst ) inst.AnimState:SetBank( "bandage" ) inst.AnimState:SetBuild( "bandage" ) inst.AnimState:PlayAnimation( "idle" ) inst:AddComponent( "inspectable" ) inst:AddComponent( "inventoryitem" ) --inst:AddComponent( "healer" ) --inst.components.healer:SetHealthAmount( 90 ) local firstplayer = TheSim:FindFirstEntityWithTag( "player" ) if firstplayer.components.health then print( "attempting to set plymaxhealth" ) plymaxhealth = firstplayer.components.health.maxhealth print( "plymaxhealth set" ) print( "attempting to set plycurhealth" ) plycurhealth = firstplayer.components.health.currenthealth print( "plycurhealth set" ) restorehealth = plymaxhealth - plycurrenthealth --firstplayer.components.health:SetVal( restorehealth ) print( plymaxhealth ) print( plycurhealth ) print( restorehealth ) end print( "medkit finished loading" ) return inst endreturn Prefab( "common/inventory/medkit", fn, assets ) When I spawn it in I get this in the console: giving medkit ../mods/medkit/scripts/prefabs/medkit.lua(10,1) medkit loaded ../mods/medkit/scripts/prefabs/medkit.lua(33,1) attempting to set plymaxhealth ...tarve/data/../mods/medkit/scripts/prefabs/medkit.lua:34: assign to undeclared variable 'plymaxhealth'LUA ERROR stack traceback: =[C] in function 'error' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/strict.lua(14,1) C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/../mods/medkit/scripts/prefabs/medkit.lua(34,1) in function 'fn' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(126,1) =[C] in function 'SpawnPrefab' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(160,1) =(tail call) ? C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/consolecommands.lua(125,1) in function 'c_give' c_give("medkit")(1,1) in main chunk =[C] in function 'pcall' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/screens/consolescreen.lua(88,1) in function 'Run' ... C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/widgets/textedit.lua(70,1) in function 'OnRawKey' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/widgets/widget.lua(94,1) in function 'OnRawKey' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/widgets/widget.lua(94,1) in function 'OnRawKey' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/widgets/widget.lua(94,1) in function 'OnRawKey' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/screens/consolescreen.lua(40,1) in function 'OnRawKey' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(705,1) in function 'OnRawKey' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(118,1) in function 'fn' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/events.lua(46,1) in function 'HandleEvent' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/input.lua(157,1) in function 'OnRawKey' C:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/input.lua(324,1)scripts/frontend.lua(723,1) SCRIPT ERROR! Showing error screen I'm fairly certain that I'm getting these errors because it can't set plymaxhealth, but I don't know how else to retrieve the player's max/current health. Any help is appreciated.medkit.zip Link to comment Share on other sites More sharing options...
Mobbstar Posted November 5, 2014 Share Posted November 5, 2014 Just put a "local" before new variables. That makes them local to the function they're being declared in, including all functions within that function. Why don't you use the healer method? EDIT: seronis had an even better idea Link to comment Share on other sites More sharing options...
XopherPM Posted November 5, 2014 Share Posted November 5, 2014 "...tarve/data/../mods/medkit/scripts/prefabs/medkit.lua:34: assign to undeclared variable 'plymaxhealth'" It looks like you need to declare plymaxhealth as a variable, ie "local plymaxhealth = ".Same with "plycurhealth" and "restorehealth".Also it looks like spawning this will set the players health to the difference of their current health and their max health, which would kill them if they are full (once line 41 is not commented out of course.) While that seems kind of awesome, I suspect that's not the intention. If I'm right about that try "DoDelta" instead of "SetValue" Link to comment Share on other sites More sharing options...
seronis Posted November 5, 2014 Share Posted November 5, 2014 If you're adjusting by the difference between max health and current health, you're effectively assigning to max health. Just do that instead of applying the delta. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.
Please be aware that the content of this thread may be outdated and no longer applicable.