Updating Entity Values
- sirrobzeroone
- Member
- Posts: 593
- Joined: Mon Jul 16, 2018 07:56
- GitHub: sirrobzeroone
- Contact:
Updating Entity Values
I've been trawlling the forums as I thought I had read a simple way to do this but I cant find it.
So I'm using the mod Mobs Redo and I'm trying to create a simple dog (loyal) NPC that can be renamed by using a form spec and right click (more as fun exercise to learn than anything).
I can get the dog (okay wolf shaped) npc okay and setup the values I can predefine his name okay by borrowing code from the trader npc. I can create a basic formscript and prepopulate the text field with current name. However when I change his name I 'm unsure how to get the registered callback data back to the NPC Dog to update his name, since self no longer works as the formspec has closed and the game no longer has any clue what entity (object instance) I'm dealing with as a player.
I can get the new name outputted as a chat string okay. If this has been done before update an npcs name via formscript in game - pointing me at the thread would be much appreciated. If not a bit of a clue as to a starter function that reselects a specific object instance...I did find a function that seemed to list all entites from a position ina radius. I can probably also manage to send the object instance id as part of the formspec....if again I know how to reference this.
Anyways any help and pointers appreciated.
Thanks heaps
So I'm using the mod Mobs Redo and I'm trying to create a simple dog (loyal) NPC that can be renamed by using a form spec and right click (more as fun exercise to learn than anything).
I can get the dog (okay wolf shaped) npc okay and setup the values I can predefine his name okay by borrowing code from the trader npc. I can create a basic formscript and prepopulate the text field with current name. However when I change his name I 'm unsure how to get the registered callback data back to the NPC Dog to update his name, since self no longer works as the formspec has closed and the game no longer has any clue what entity (object instance) I'm dealing with as a player.
I can get the new name outputted as a chat string okay. If this has been done before update an npcs name via formscript in game - pointing me at the thread would be much appreciated. If not a bit of a clue as to a starter function that reselects a specific object instance...I did find a function that seemed to list all entites from a position ina radius. I can probably also manage to send the object instance id as part of the formspec....if again I know how to reference this.
Anyways any help and pointers appreciated.
Thanks heaps
- sirrobzeroone
- Member
- Posts: 593
- Joined: Mon Jul 16, 2018 07:56
- GitHub: sirrobzeroone
- Contact:
Re: Updating Entity Values
Think I'm getting there dug through the wiki and the forums a bit more. Haven't made it work yet but I think this info is in the right direction for what I need:
https://dev.minetest.net/LuaEntitySAO:get_entity_name
viewtopic.php?f=47&t=17181
https://dev.minetest.net/LuaEntitySAO:get_entity_name
viewtopic.php?f=47&t=17181
Re: Updating Entity Values
Mobs Redo API already has nametags for right clicking tamed mobs and giving them a name.
- sirrobzeroone
- Member
- Posts: 593
- Joined: Mon Jul 16, 2018 07:56
- GitHub: sirrobzeroone
- Contact:
Re: Updating Entity Values
Thanks Tenplus1 any idea which mob pack has an example/has it implemented? I thought I'd trawled the various packs pretty well checking for functionality...
- sirrobzeroone
- Member
- Posts: 593
- Joined: Mon Jul 16, 2018 07:56
- GitHub: sirrobzeroone
- Contact:
Re: Updating Entity Values
Sorry my bad to clarify, I'm trying to update the "nametag" of of an entity not retrieve and change entity:name on the fly. not this
NPC trader in mob mods has the values game_name and nametag, which i borrowed.
Formspec loads okay on right click and I can spawn them okay, i seem to have multiple ways to Identify the NPC as an object but I think the unique one is possibly added by mob mods:
eg value 1441960mobs_npc:npc_female70756
However formspec on exit then ends the function and I then have to kick off a new function to register the callback and kickoff updating the game_name.
I start of using minetest.get_objects_inside_radius using the known position of the npc above as the pos x,y,z and setting radius to 0.5. If I'm understanding correctly I know essentially have an object with a bunch of objects inside it with values for those objects...or as many websites seemed to explain it almost nested arrays/tables.
How I think its conceptually stored using get objects_inside_radius
I can count the number of objects in objects_in_radius fairly easily (normally 1 unless I set radius higher). I'm not to worried at this point about errors or picking up the player by mistake. For testing I have been trying to simply send some player value to the chat window..... However I either get a nil error a userdata or table element error wrappering those then in tostring gives me a nice 0x657890eg to the chat window. My question is how or can you address into the underlying values.
another snipet
essentially all i wish to do is take the self.ID in function 1 use it with the new name data from the formspec return in function2 to identify out my npc by matching the self.ID -function1 to object_radius-function2 values and then update the game_name
writing it seems very easy but although I have the object sitting in a table I can't get seem to access the underlying values. I even ran a few agnostic examples in from lua were i set the data and then ipairs/pairs the data those worked okay, although they did have assumptions around types.
Side question why when I do this:
does the "self" reference not work? My best guess is because there is no click event so self is undefined?
Once again sorry for all the questions and thank you for any help as I'm running out of places to read and trial and error is getting me nowhere fast.
Code: Select all
minetest.add_entity(pos, "name", [staticdata])
Formspec loads okay on right click and I can spawn them okay, i seem to have multiple ways to Identify the NPC as an object but I think the unique one is possibly added by mob mods:
Code: Select all
rename_object_id = self.id
Code: Select all
minetest.show_formspec(player, "mobs_npc:npc_female", "size[8,10]" ..
default.gui_bg_img ..
default.gui_slots ..
"field[1,1;3,1;newname;Name;"..S("@1" ,self.game_name) .."]"..
"label[0.5,-0.1;".. S("My Name is @1", self.game_name) .. "]"..
"button_exit[1,3;1,3;nameupdate;Save]")
I start of using minetest.get_objects_inside_radius using the known position of the npc above as the pos x,y,z and setting radius to 0.5. If I'm understanding correctly I know essentially have an object with a bunch of objects inside it with values for those objects...or as many websites seemed to explain it almost nested arrays/tables.
How I think its conceptually stored using get objects_inside_radius
Code: Select all
objects_in_radius:
{object 1{val=1 val=2 val=3}}
{object 2{val=1 val=2 val=3}}
{object 3{val=1 val=2 val=3}}
I can count the number of objects in objects_in_radius fairly easily (normally 1 unless I set radius higher). I'm not to worried at this point about errors or picking up the player by mistake. For testing I have been trying to simply send some player value to the chat window..... However I either get a nil error a userdata or table element error wrappering those then in tostring gives me a nice 0x657890eg to the chat window. My question is how or can you address into the underlying values.
another snipet
Code: Select all
local _2,obj2
local object = {}
for _2,obj2 in ipairs(all_objects) do
if not obj2:is_player_connected("singleplayer") then
table.insert(object, obj2)
minetest.chat_send_player(player, "match=" ..tostring(obj2))
end
writing it seems very easy but although I have the object sitting in a table I can't get seem to access the underlying values. I even ran a few agnostic examples in from lua were i set the data and then ipairs/pairs the data those worked okay, although they did have assumptions around types.
Side question why when I do this:
Code: Select all
minetest.register_on_player_receive_fields(function (self, formname, fields)
[i]bunch of code[/i]
)
Once again sorry for all the questions and thank you for any help as I'm running out of places to read and trial and error is getting me nowhere fast.
Re: Updating Entity Values
sirrobzerrone _ the nametag is built into the Api, give yourself a nametag and right click any tamed mob
- sirrobzeroone
- Member
- Posts: 593
- Joined: Mon Jul 16, 2018 07:56
- GitHub: sirrobzeroone
- Contact:
Re: Updating Entity Values
Thanks again TenPlus1 that was just the pointer I needed, I have it working now using your working code as a guide. Can't say I understand why lua does some of what it does but I expect thats the massive big holes in my understanding of OOP in general.
- Hybrid Dog
- Member
- Posts: 2828
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
Re: Updating Entity Values
Changes of the object, e.g. position, velocity and acceleration, need to be passed via a provided method, otherwise the changes are only on lua side.
To change the nametag you likely need to change the object properties:
https://github.com/minetest/minetest/bl ... .txt#L5539
https://github.com/minetest/minetest/bl ... .txt#L5038
The nametag attributes function may also work:
https://github.com/minetest/minetest/bl ... .txt#L5047
To print the contents of an object or table, you can use the dump function: print(dump(obj))
Fyi: All objects share the same functions as methods. Calling for example obj:set_pos(p) does the same as obj.set_pos(obj, p). Players are also objects, so even minetest.get_player_by_name("singleplayer").set_pos(obj, p) does the same.
The functions are stored in the ObjectRef metatable, so not every object has pointers for each method but instead all objects share the same table of pointers.
The methods can thereby be overridden to e.g. disallow players to teleport from the nether:
https://github.com/HybridDog/nether-pac ... #L245-L268
To change the nametag you likely need to change the object properties:
https://github.com/minetest/minetest/bl ... .txt#L5539
https://github.com/minetest/minetest/bl ... .txt#L5038
The nametag attributes function may also work:
https://github.com/minetest/minetest/bl ... .txt#L5047
To print the contents of an object or table, you can use the dump function: print(dump(obj))
Fyi: All objects share the same functions as methods. Calling for example obj:set_pos(p) does the same as obj.set_pos(obj, p). Players are also objects, so even minetest.get_player_by_name("singleplayer").set_pos(obj, p) does the same.
The functions are stored in the ObjectRef metatable, so not every object has pointers for each method but instead all objects share the same table of pointers.
The methods can thereby be overridden to e.g. disallow players to teleport from the nether:
https://github.com/HybridDog/nether-pac ... #L245-L268
Who is online
Users browsing this forum: No registered users and 6 guests