Save and recover entity (advanced)

Post Reply
User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Save and recover entity (advanced)

by runs » Post

I have an entity (an unique npc entity, not a template). I want to save it during a game and recover when a new game starts.

I prefer not to iterate over all the entities to search for.

I try to save the entity name in mod_storage. And then recover the entity when a new game starts.

I tried with 'minetest.luaentities[objectID]' according the wiki

But I get no result. What is that objectID? The name? What?

My code:

To create the entity (it works OK):

Code: Select all

local objectref = minetest.add_entity(pos, "name")		
local entity = objectref:get_luaentity()		
mod_storage:set_string("ent_name", entity.name)		
To recover the entity (fails):

Code: Select all

local ent_name = mod_storage:get_string("ent_name")	
local entity = minetest.luaentities[ent_name]
Thanks in advance.
Last edited by runs on Sun Nov 25, 2018 18:33, edited 1 time in total.

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: Save and recover entity

by Krock » Post

You don't need to store it manually like this. There are entity callbacks to do that. example code

`on_activate`: Called when the object is loaded in the map -> use the supplied staticdata to save your stuff
`get_staticdata`: Called when the object is saved to the disk (also when it's getting unloaded)

The luaentities table is not thought for mod use since it's required by the engine to have the object data ready when it's required to send it.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Save and recover entity (advanced)

by rubenwardy » Post

This chapter explains some things about how entities work: https://rubenwardy.com/minetest_modding ... jects.html
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Re: Save and recover entity (advanced)

by runs » Post

Thanks on_activate was what I wanted!

PD: 'after_activate' is the related custom function for Mobs Redo.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests