How to check a players health with lua?

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

How to check a players health with lua?

by webdesigner97 » Wed Sep 26, 2012 13:22

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?
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Sep 26, 2012 14:32

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
})
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 14:50

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?
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Sep 26, 2012 14:56

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.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:00

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
Last edited by webdesigner97 on Wed Sep 26, 2012 15:02, edited 1 time in total.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:01

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?
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Sep 26, 2012 15:05

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
})

 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Sep 26, 2012 15:05

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.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:05

New problem: When I define one texture for the nodebox, a part of it uses a texture of another node! Why?!
Last edited by webdesigner97 on Wed Sep 26, 2012 15:06, edited 1 time in total.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:06

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
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Sep 26, 2012 15:06

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?
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:08

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
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:10

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
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Sep 26, 2012 15:10

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.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:14

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"
Last edited by webdesigner97 on Wed Sep 26, 2012 15:15, edited 1 time in total.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Sep 26, 2012 15:17

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.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:21

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
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:24

Ok, maybe I could modify the health without apples (cheating!), is there a way to change health with code?
 

User avatar
Echo
Member
 
Posts: 121
Joined: Tue Jul 31, 2012 08:11

by Echo » Wed Sep 26, 2012 15:29

Use the function set_hp
Have you read minetest\doc\lua_api.txt? It's a very good start to get an overview.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Sep 26, 2012 15:30

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)
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:51

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
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Wed Sep 26, 2012 15:53

Ok, this part works now, THX to all of you.... :)
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 18 guests

cron