Re: [Mod] ActiveFormspecs [formspecs]
Posted: Mon Feb 24, 2020 23:40
Code Guide: Using Upvalues in Place of a State-Table
Although using a temporary table to track form state may prove helpful in some situations, it's not always necessary. Consider these two slightly different approaches:
Therefore, whenever possible, you should take advantage of upvalues. And indeed this is yet another benefit of form functions, as described in the previous Code Guide. Good luck and keep having fun with formspecs!
Although using a temporary table to track form state may prove helpful in some situations, it's not always necessary. Consider these two slightly different approaches:
- Method 1: Using a temporary table to track form state
Method 2: Using upvalues to track form state
Code: Select all
minetest.register_chatcommand( "score", { func = function ( name, param ) local formspec = "size[4,4]" .. "label[0.2,1.0;How lucky do you feel today?]" .. "button[1,2;2,1;raise_score;Raise Score!]" minetest.create_form( { score = 0, count = 0 }, name, formspec, function ( meta, player, fields ) if fields.raise_score then meta.count = meta.count + 1 meta.score = meta.score + math.random( -5, 10 ) if meta.score < 0 or meta.count > 25 then minetest.chat_send_player( name, "Sorry buddy, you didn't win this time!" ) minetest.destroy_form( name ) elseif meta.score > 100 then minetest.chat_send_player( name, "CONGRATULATIONS! You just won a pot of virtual gold!" ) minetest.destroy_form( name ) elseif meta.score > 50 then minetest.chat_send_player( name, "You're a real winner. Keep going!" ) elseif meta.score > 10 then minetest.chat_send_player( name, "Now you're on a roll. Do it again!" ) else minetest.chat_send_player( name, "Don't give up. Try another round!" ) end elseif fields.quit == minetest.FORMSPEC_SIGEXIT then minetest.chat_send_player( name, "You scored " .. meta.score .. " after " .. meta.count .. "tries!" ) end end ) end } )
Code: Select all
minetest.register_chatcommand( "score", { func = function ( name, param ) local score = 0 local count = 0 local formspec = "size[4,4]" .. "label[0.2,1.0;How lucky do you feel today?]" .. "button[1,2;2,1;raise_score;Raise Score!]" minetest.create_form( nil, name, formspec, function ( meta, player, fields ) if fields.raise_score then count = count + 1 score = score + math.random( -5, 10 ) if score < 0 or count > 25 then minetest.chat_send_player( name, "Sorry buddy, you didn't win this time!" ) minetest.destroy_form( name ) elseif score > 100 then minetest.chat_send_player( name, "CONGRATULATIONS! You just won a pot of virtual gold!" ) minetest.destroy_form( name ) elseif score > 50 then minetest.chat_send_player( name, "You're a real winner. Keep going!" ) elseif score > 10 then minetest.chat_send_player( name, "Now you're on a roll. Do it again!" ) else minetest.chat_send_player( name, "Don't give up. Try another round!" ) end elseif fields.quit == minetest.FORMSPEC_SIGEXIT then minetest.chat_send_player( name, "You scored " .. score .. " after " .. count .. "tries!" ) end end ) end } )
Therefore, whenever possible, you should take advantage of upvalues. And indeed this is yet another benefit of form functions, as described in the previous Code Guide. Good luck and keep having fun with formspecs!