use on_recieve_fields with entity

tbillion
Member
 
Posts: 189
Joined: Wed Apr 03, 2013 16:07

use on_recieve_fields with entity

by tbillion » Thu Dec 24, 2015 10:47

how can you process formspec data in an entity?
 

tbillion
Member
 
Posts: 189
Joined: Wed Apr 03, 2013 16:07

Re: use on_recieve_fields with entity

by tbillion » Thu Dec 24, 2015 12:56

minetest.register_on_player_receive_fields(function(player, formname, fields)
 

User avatar
BrandonReese
Member
 
Posts: 836
Joined: Wed Sep 12, 2012 00:44
GitHub: bremaweb
IRC: BrandonReese
In-game: BrandonReese

Re: use on_recieve_fields with entity

by BrandonReese » Thu Dec 24, 2015 14:18

tbillion wrote:minetest.register_on_player_receive_fields(function(player, formname, fields)


Are you wanting a formspec to show when you click an entity?
 

tbillion
Member
 
Posts: 189
Joined: Wed Apr 03, 2013 16:07

Re: use on_recieve_fields with entity

by tbillion » Thu Dec 24, 2015 17:09

yeah, well actually when you punch an entity with a certain item (car key in this case) to show formspec ...

see : viewtopic.php?f=9&t=13653&start=50#p202523
 

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

Re: use on_recieve_fields with entity

by stu » Thu Dec 24, 2015 17:23

tbillion wrote:yeah, well actually when you punch an entity with a certain item (car key in this case) to show formspec ...

see : viewtopic.php?f=9&t=13653&start=50#p202523

Well you can call minetest.show_formspec() from the entities on_punch callback, though you will probably need to include something in the formname to identify its origin.
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: use on_recieve_fields with entity

by kaeza » Thu Dec 24, 2015 18:06

Another way would be to store a reference to the entity in an (internal) table indexed by player name in on_punch, and use that in on_receive_fields to get back to it.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

tbillion
Member
 
Posts: 189
Joined: Wed Apr 03, 2013 16:07

Re: use on_recieve_fields with entity

by tbillion » Thu Dec 24, 2015 18:39

stu wrote:Well you can call minetest.show_formspec() from the entities on_punch callback, though you will probably need to include something in the formname to identify its origin.


sorry for my lack of posting code i always forget to do this.

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

function car1.on_punch(self, puncher, time_from_last_punch, tool_capabilities, direction)
   local tool = puncher:get_wielded_item():get_name()
   local name = puncher:get_player_name()
   if tool == "mck:equipment_key" then
      local pos = self.object:getpos()
      local meta = minetest.env:get_meta(pos)
      local inv = meta:get_inventory()
      local invid = self.invid
      print(dump(self).." "..self.invid)
      --local meta = minetest.env:get_meta(pos)
      --local inv = meta:get_inventory()
      --local dumptruckdata = minetest.deserialize(meta:get_string("dumptruckdata"))
      --print(dumptruckdata.sww)
      minetest.show_formspec(name, "mck_dumptruck:inventory",
         "size[20,16]"..
         "button_exit[16,13;4,1;drillbutton;Drill]"..
         "button[0,13;2,1;tbmc1;Place All]"..
         "button[2,13;2,1;tbmc2;Take All]"..
         "button[5,13;3,1;tbmc3;Empty Machine]"..
         "list[current_player;"..invid..";0,0;20,13;]"..
         "list[current_player;main;0,14;16,2;]")
   else
    if self.driver then
      self.driver:set_detach()
      local name = self.driver:get_player_name()
      default.player_attached[name] = false
      default.player_set_animation(self.driver, "stand" , 30)
      self.driver = nil
    end
   -- delay remove to ensure player is detached
     minetest.after(0.1, function()
      self.object:remove()
    end)
    if puncher and puncher:is_player() and not minetest.setting_getbool("creative_mode") then
      puncher:get_inventory():add_item("main", "mck:dumptruck")
    end
   end
end



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

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local player_name = player:get_player_name()
   local player_inv = player:get_inventory();
   local inv_list = 'main';

   --local ui_peruser,draw_lite_mode = unified_inventory.get_per_player_formspec(player_name)

   if formname ~= "mck_dumptruck:inventory" then
      return
   end


      if (fields.tbmc1) then
      print(player_name)
      player_inv:get_list("mck:dumptruck")
         for i,v in ipairs( player_inv:get_list( inv_list ) or {}) do
            if( player_inv and player_inv:room_for_item('mck:dumptruck', v)) then
               local leftover = player_inv:add_item( 'mck:dumptruck', v );
               player_inv:remove_item( inv_list, v );
               if( leftover and not( leftover:is_empty() )) then
                  player_inv:add_item( inv_list, v );
               end
            end

         end
      elseif (fields.tbmc2) then
         for i,v in ipairs( player_inv:get_list( 'mck:dumptruck' ) or {}) do
            if( player_inv:room_for_item( inv_list, v)) then
               local leftover = player_inv:add_item( inv_list, v );
               player_inv:remove_item( 'mck:dumptruck', v );
               if( leftover and not( leftover:is_empty() )) then
                  player_inv:add_item( 'mck:dumptruck', v );
               end
            end
         end
      elseif (fields.tbmc3) then
         for i = 1, player_inv:get_size("mck:dumptruck") do
            player_inv:set_stack("mck:dumptruck", i, nil)
         end
      end

end)



im actually already doing what you suggested stu.

kaeza wrote:Another way would be to store a reference to the entity in an (internal) table indexed by player name in on_punch, and use that in on_receive_fields to get back to it.


not sure what kind of reference i would use, now all the dump trucks have a unique id, i figured that out and i named the inventories with the unique ID, i just wasnt sure if there was an actuall call back for entities for on_receive_feilds, from what i have read there is not, which is a bit lame. but im working around it. now i just need a way to emulate on_destruct and remove the created inventories when the machine is destroyed.
 

tbillion
Member
 
Posts: 189
Joined: Wed Apr 03, 2013 16:07

Re: use on_recieve_fields with entity

by tbillion » Thu Dec 24, 2015 19:02

you have to be kidding.. the data in the object is not persistant across server restarts ... jeeze if its not one thing its another. so how would you store data to an entity... there has to be a way .

### Entity definition (`register_entity`)

{
-- Deprecated: Everything in object properties is read directly from here

initial_properties = --[[<initial object properties>]],

on_activate = function(self, staticdata, dtime_s),
on_step = function(self, dtime),
on_punch = function(self, hitter),
on_rightclick = function(self, clicker),
get_staticdata = function(self),
-- ^ Called sometimes; the string returned is passed to on_activate when
-- the entity is re-activated from static state

-- Also you can define arbitrary member variables here
myvariable = whatever,
}


what do they mean by this?:

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
        get_staticdata = function(self),
    --  ^ Called sometimes; the string returned is passed to on_activate when
    --    the entity is re-activated from static state

        -- Also you can define arbitrary member variables here
        myvariable = whatever,


@kaeza: i found what you were talking about in another post (viewtopic.php?f=9&t=4948), a lot of work to store one variable plus if the entity wont store its own id then any information in the table is pointless because you wouldn't be able to tell the trucks apart with out an ID because you never know where an entity will be,,,
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 2 guests