Page 1 of 1

How to check a players health with lua?

PostPosted: Wed Sep 26, 2012 13:22
by webdesigner97
Hope this hasn't been asked before, I used search... :P

For my StreetsMod I'd like to create an "emergency phone", which you can punch. It'll check your health and if you've got less than "X" health, it'll drop an apple in front of it. But how can i realize this check of health? Is it possible?

PostPosted: Wed Sep 26, 2012 14:32
by PilzAdam
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local HEALTH = 4 -- min health of the player in 1/2 hearts to get help

minetest.register_node("modname:help_node", {
    -- normal stuff here
   
    on_punch = function(pos, node, puncher)
        if puncher:get_hp() <= HEALTH then
            player:get_inventory():add_item("main", ItemStack("default:apple"))
        end
    end
})

PostPosted: Wed Sep 26, 2012 14:50
by webdesigner97
thx, I'll try it. Another question: When working with nodeboxes, is it possible to use multiple textures? For the emergency phone, I've got an orange background, a "SOS" text, and a phone-graphic. How can I place them on the nodebox on the right position?

PostPosted: Wed Sep 26, 2012 14:56
by PilzAdam
webdesigner97 wrote:thx, I'll try it. Another question: When working with nodeboxes, is it possible to use multiple textures? For the emergency phone, I've got an orange background, a "SOS" text, and a phone-graphic. How can I place them on the nodebox on the right position?

The textures are "painted" on the nodeboxes the same way it is in normal nodes. So every little box will have a part of the x+ texture of the node; you can not define little textures for all little boxes.

PostPosted: Wed Sep 26, 2012 15:00
by webdesigner97
I get an error message with this code:
local HEALTH = 4
-- Emergency Phone
minetest.register_node("streets:emergency_phone", {
description = "Emergency Phone",
tile_images = {"streets_sos_bg.png"},
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
groups = {cracky=3},
node_box = {
type = "fixed",
fixed = {{-0.15, -0.5, -0.15, 0.15, 0.5, 0.15}, {0.3, 0.5, 0.3, -0.3, 1.4, -0.3}, {0.35, 1.4, 0.35, -0.35, 1.5, -0.35}}
}
on_punch = function(pos, node, puncher)
if puncher:get_hp() <= HEALTH then
player:get_inventory():add_item("main", ItemStack("default:apple"))
end
end
})


Debug.txt:
16:56:06: INFO[main]: [streets ] ["C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\mods\minetest\streets\init.lua"]
16:56:06: VERBOSE[main]: Loading and running script from C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\mods\minetest\streets\init.lua
16:56:06: ERROR[main]: ========== ERROR FROM LUA ===========
16:56:06: ERROR[main]: Failed to load and run script from
16:56:06: ERROR[main]: C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\mods\minetest\streets\init.lua:
16:56:06: ERROR[main]: ...est-c55 Win32\minetest-0.4.1-win32\bin\..\mods\minetest\streets\init.lua:110: '}' expected (to close '{' at line 99) near 'on_punch'
16:56:06: ERROR[main]: =======END OF ERROR FROM LUA ========

16:56:06: ERROR[main]: Server: Failed to load and run C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\mods\minetest\streets\init.lua
16:56:06: INFO[main]: BanManager: saving to C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\worlds\Modding Area\ipban.txt
16:56:06: ERROR[main]: ModError: Failed to load and run C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\mods\minetest\streets\init.lua
16:56:06: INFO[main]: Searching worlds...
16:56:06: INFO[main]: In C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\worlds:
16:56:06: INFO[main]: (invalid: min) Modding Area StreetsModExample test
16:56:06: INFO[main]: 3 found.
16:56:06: INFO[main]: Waiting for other menus
16:56:06: INFO[main]: Waited for other menus
16:56:06: VERBOSE[main]: error_message = ModError: Failed to load and run C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\mods\minetest\streets\init.lua
16:56:06: VERBOSE[main]: Check debug.txt for details.
16:56:06: INFO[main]: Created main menu
16:56:06: INFO[main]: locale has been set to:German_Germany.1252
16:56:06: INFO[main]: locale has been set to:C
16:56:08: INFO[main]: locale has been set to:German_Germany.1252
16:56:08: INFO[main]: locale has been set to:C
16:56:09: INFO[main]: Dropping main menu
16:56:09: INFO[main]: Updating configuration file: "C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\minetest.conf"
16:56:09: INFO[main]: Skipping writing of C:\Program Files (x86)\Minetest-c55 Win32\minetest-0.4.1-win32\bin\..\minetest.conf because content wouldn't be modified

PostPosted: Wed Sep 26, 2012 15:01
by webdesigner97
PilzAdam wrote:
webdesigner97 wrote:thx, I'll try it. Another question: When working with nodeboxes, is it possible to use multiple textures? For the emergency phone, I've got an orange background, a "SOS" text, and a phone-graphic. How can I place them on the nodebox on the right position?

The textures are "painted" on the nodeboxes the same way it is in normal nodes. So every little box will have a part of the x+ texture of the node; you can not define little textures for all little boxes.

:( So something like this isn't possible?

PostPosted: Wed Sep 26, 2012 15:05
by PilzAdam
webdesigner97 wrote:I get an error message with this code:
local HEALTH = 4
-- Emergency Phone
minetest.register_node("streets:emergency_phone", {
description = "Emergency Phone",
tile_images = {"streets_sos_bg.png"},
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
groups = {cracky=3},
node_box = {
type = "fixed",
fixed = {{-0.15, -0.5, -0.15, 0.15, 0.5, 0.15}, {0.3, 0.5, 0.3, -0.3, 1.4, -0.3}, {0.35, 1.4, 0.35, -0.35, 1.5, -0.35}}
},
on_punch = function(pos, node, puncher)
if puncher:get_hp() <= HEALTH then
player:get_inventory():add_item("main", ItemStack("default:apple"))
end
end
})


PostPosted: Wed Sep 26, 2012 15:05
by PilzAdam
webdesigner97 wrote:
PilzAdam wrote:
webdesigner97 wrote:thx, I'll try it. Another question: When working with nodeboxes, is it possible to use multiple textures? For the emergency phone, I've got an orange background, a "SOS" text, and a phone-graphic. How can I place them on the nodebox on the right position?

The textures are "painted" on the nodeboxes the same way it is in normal nodes. So every little box will have a part of the x+ texture of the node; you can not define little textures for all little boxes.

:( So something like this isn't possible?

Maybe, buts its tricky.

PostPosted: Wed Sep 26, 2012 15:05
by webdesigner97
New problem: When I define one texture for the nodebox, a part of it uses a texture of another node! Why?!

PostPosted: Wed Sep 26, 2012 15:06
by webdesigner97
PilzAdam wrote:
webdesigner97 wrote:
PilzAdam wrote:The textures are "painted" on the nodeboxes the same way it is in normal nodes. So every little box will have a part of the x+ texture of the node; you can not define little textures for all little boxes.

:( So something like this isn't possible?

Maybe, buts its tricky.

...ok

PostPosted: Wed Sep 26, 2012 15:06
by PilzAdam
webdesigner97 wrote:New problem: When I define one texture for the nodebox, a part of it uses a texture of another mod! Why?!

Can you explain it better? Screenshots?

PostPosted: Wed Sep 26, 2012 15:08
by webdesigner97
PilzAdam wrote:
webdesigner97 wrote:New problem: When I define one texture for the nodebox, a part of it uses a texture of another mod! Why?!

Can you explain it better? Screenshots?

Sry, not mod, but node. Screenshot:
Image

PostPosted: Wed Sep 26, 2012 15:10
by webdesigner97
PilzAdam wrote:
webdesigner97 wrote:I get an error message with this code:
local HEALTH = 4
-- Emergency Phone
minetest.register_node("streets:emergency_phone", {
description = "Emergency Phone",
tile_images = {"streets_sos_bg.png"},
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
groups = {cracky=3},
node_box = {
type = "fixed",
fixed = {{-0.15, -0.5, -0.15, 0.15, 0.5, 0.15}, {0.3, 0.5, 0.3, -0.3, 1.4, -0.3}, {0.35, 1.4, 0.35, -0.35, 1.5, -0.35}}
},
on_punch = function(pos, node, puncher)
if puncher:get_hp() <= HEALTH then
player:get_inventory():add_item("main", ItemStack("default:apple"))
end
end
})


ok, thx, this part works now

PostPosted: Wed Sep 26, 2012 15:10
by PilzAdam
webdesigner97 wrote:
PilzAdam wrote:
webdesigner97 wrote:New problem: When I define one texture for the nodebox, a part of it uses a texture of another mod! Why?!

Can you explain it better? Screenshots?

Sry, not mod, but node. Screenshot:
Image

Making nodes bigger than one block is not recommendet. The textures are only in normal node side. Make 2 nodes.

PostPosted: Wed Sep 26, 2012 15:14
by webdesigner97
ok, but again a problem with your code :(

player:get_inventory():add_item("main", ItemStack("default:apple"))

--> game crashes when punching with "attempt to index"

PostPosted: Wed Sep 26, 2012 15:17
by PilzAdam
webdesigner97 wrote:ok, but again a problem with your code :(

player:get_inventory():add_item("main", ItemStack("default:apple"))

--> game crashes when punching with "attempt to index"

ooops... it has to be puncher instead of player.

PostPosted: Wed Sep 26, 2012 15:21
by webdesigner97
PilzAdam wrote:
webdesigner97 wrote:ok, but again a problem with your code :(

player:get_inventory():add_item("main", ItemStack("default:apple"))

--> game crashes when punching with "attempt to index"

ooops... it has to be puncher instead of player.

ok, thx

PostPosted: Wed Sep 26, 2012 15:24
by webdesigner97
Ok, maybe I could modify the health without apples (cheating!), is there a way to change health with code?

PostPosted: Wed Sep 26, 2012 15:29
by Echo
Use the function set_hp
Have you read minetest\doc\lua_api.txt? It's a very good start to get an overview.

PostPosted: Wed Sep 26, 2012 15:30
by PilzAdam
webdesigner97 wrote:Ok, maybe I could modify the health without apples (cheating!), is there a way to change health with code?

Its not cheating when its in a mod...
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
puncher:set_hp(puncher:get_hp()+2)

PostPosted: Wed Sep 26, 2012 15:51
by webdesigner97
Yes, I read some parts of it, but it's very difficult to read if your that good in english like the creator of the file.... :)

THX

PostPosted: Wed Sep 26, 2012 15:53
by webdesigner97
Ok, this part works now, THX to all of you.... :)