Tutorial on formspec?

User avatar
scifiboi
Member
 
Posts: 96
Joined: Wed Jul 18, 2012 21:28

Tutorial on formspec?

by scifiboi » Fri Mar 22, 2013 17:45

Can anyone give me a tutorial on how to make a node open up a form? The minetest dev wiki doesn't really help me, and I don't understand the code of other mods that use formspec. Can someone please help?
This is a signature virus. Add me to your signature so that I can multiply.
My mod: Thaumtest
 

prestidigitator
Member
 
Posts: 632
Joined: Thu Feb 21, 2013 23:54

by prestidigitator » Fri Mar 22, 2013 18:49

The formspec is stored in the node metadata, which means it has to be set whenever the node is placed. This can be done from the node's on_construct or after_place_node callbacks. Use the former if you just care about any time the node is created, and use the latter if you only want to do it when a player specifically places the node from inventory. To set the actual formspec, you'll use code like:

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_node(
   ...,
   {
      ...,

      on_construct = function(pos)
         local meta = minetest.env:get_meta(pos)
         meta:set_string("formspec", myFormspecString)
      end,

      ...
   })


As for the formspec string itself, currently the documentation in the lua_api.txt file that comes with the game is probably a little more complete and easy to understand than the stuff in the developer wiki. I've been meaning to synchronize the two a bit but haven't gotten to it yet. Pay particular attention to where there are semicolons (;) because it is very picky about those. If there is an extra one at the end of a command that doesn't need one, the formspec won't show, and if there is one missing in a command which is supposed to have one at the end, the formspec won't show. Also, just ignore anything that seems to imply you should use "context" for the node's inventory list; it must be "current_name" instead, or the formspec will not show.

Hope that helps and is a good start to exploring things. This functionality is a little finicky, but it shouldn't be too hard to get a very simple working prototype and expand little by little from there.
 

1244
Member
 
Posts: 45
Joined: Fri Jul 13, 2012 16:40

by 1244 » Fri Mar 22, 2013 21:19

I have a question for formspec too. In wchich way I can make when I click button i formscpec the variable X changes to 1 and when I clicked again changes to 0?
 

prestidigitator
Member
 
Posts: 632
Joined: Thu Feb 21, 2013 23:54

by prestidigitator » Fri Mar 22, 2013 22:11

Oh. I forgot to mention the new minetest.show_formspec(...) function too. You can use this at any arbitrary time to display an arbitrary formspec. It could be when someone right-clicks on an entity, uses an item, digs a node, (re-)spawns, or whatever. Great addition!
 

prestidigitator
Member
 
Posts: 632
Joined: Thu Feb 21, 2013 23:54

by prestidigitator » Fri Mar 22, 2013 22:18

1244 wrote:I have a question for formspec too. In wchich way I can make when I click button i formscpec the variable X changes to 1 and when I clicked again changes to 0?


This is a little trickier. Other than the functionality of moving around inventory items, formspecs work a bit like web forms. You'll need to use minetest.register_on_player_receive_fields(callback) and filter using the form name passed to the function to find the one you are interested in. Another of the arguments passed to the callback will give you the list of all fields and their values. I'm not sure, but I THINK (and hope) the list of fields contains the name of the button clicked to submit the form. To "keep the form up" after it is submitted you might need to immediately redisplay it with minetest.show_formspec(...).

EDIT: Oh. I forgot. For node formspecs there's also a node-specific on_receive_fields(...) callback.

To learn more I suggest looking at how signs and the creative inventory are done.
Last edited by prestidigitator on Fri Mar 22, 2013 22:22, edited 1 time in total.
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 56 guests

cron