Display and add varaibles

Post Reply
basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Display and add varaibles

by basil60 » Post

Hi
I'm a noob and trying to do some basic stuff.

My code looks like this:

Code: Select all

minetest.register_chatcommand("foo1", {
	privs = {
		interact = true
	},
	func = function(name, param)
		local a = param 
	end
})
minetest.register_chatcommand("foo2", {
	privs = {
		interact = true
	},
	func = function(name, param)
		local b = param
	end
})

result = a + b 
minetest.chat_send_all(result)
I'm trying to do this:

Code: Select all

local a = 2     -- Set 'a' to 2
local b = 2     -- Set 'b' to 2
local result = a + b -- Set 'result' to a + b, which is 4
a = a + 10
print("Sum is "..result)
, but instead of doing in the command line, want it to happen in the display.

Where did I go wrong? It doesn't display a result.
Is there a better way to do this?

User avatar
qwertymine3
Member
Posts: 202
Joined: Wed Jun 03, 2015 14:33
GitHub: Qwertymine
In-game: qwertymine3

Re: Display and add varaibles

by qwertymine3 » Post

There are a number of issues with what you have written.

In both of the chat commands you have written local a/b. This makes a/b local to the function, not the file, so they can't be accessed outside their respective function. You need to declare a local a/b outside of any function for it to be file-local.

You have then written the result calculation and chat_send_all call outside of any function. This means that it will be called while the mod is initialising, before either of the two values has been set. IDK why this isn't causing an error, but you need to call it later instead, such as in a third chat command.

Also param is a string, you need to convert it to a number if you want to do arithmetic on it later. This can be done with tonumber().
Avatar by :devnko-ennekappao:

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: Display and add varaibles

by basil60 » Post

Thanks
I see you do a lot of work in Minetest. I was trying to work on basic coding examples that use Lua and work within Minestest, to teach some basic programming to kids.

Got any good ideas for simple simple programmes for kids, who'll generally have no idea of coding?

User avatar
qwertymine3
Member
Posts: 202
Joined: Wed Jun 03, 2015 14:33
GitHub: Qwertymine
In-game: qwertymine3

Re: Display and add varaibles

by qwertymine3 » Post

Are you looking for demo ideas (so that complex stuff can be used behind the scenes to make the code look better) or are you looking for code that the kids can write?

It is quite hard to think of examples which don't go too deep into the minetest API and are interactive, but on_palce/on_rightclick maybe a good starting point.

Code: Select all

minetest.register_craftitem("teaching:conditionals",{
    description = "Change dirt to mese!",
    stack_max = 1,
    inventory_image = "default_mese_block.png",
    on_place = function(_,_,pointed_thing)
        local looking_at = minetest.get_node(pointed_thing.under)
        if looking_at.name == "default:dirt" then
            minetest.set_node(pointed_thing.under,{name = "default:mese"})
        end
    end,
})
Avatar by :devnko-ennekappao:

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: Display and add varaibles

by basil60 » Post

Code that kids can write or adapt would be the ideal.

So I've taken a LUA loop - write Hello World 10 times to lay 10 blocks side by side.
That's the kind of progression I had in mind. And I think all they can probably handle!
The API has always been a sticking point (not just in Minetest) for me. It's what separates the "men from the boys".

And I'm barely a lad!!!
Thanks for the example - if you've got others, it'd be great!

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: Display and add varaibles

by basil60 » Post

qwertymine3
I tried this, in anew mod folder called "teaching"

Code: Select all

minetest.register_craftitem("teaching:conditionals",{
    description = "Change dirt to water!",
    stack_max = 1,
    inventory_image = "default_water.png",
    on_place = function(_,_,pointed_thing)
        local looking_at = minetest.get_node(pointed_thing.under)
        if looking_at.name == "default:dirt" then
            minetest.set_node(pointed_thing.under,{name = "default:water"})
        end
    end,
})
Am I correct in assuming that if I place a dirt node, it will then change the node to water.
It doesn't seem to change anything.

Byakuren
Member
Posts: 816
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: Display and add varaibles

by Byakuren » Post

basil60 wrote:qwertymine3
I tried this, in anew mod folder called "teaching"

Code: Select all

minetest.register_craftitem("teaching:conditionals",{
    description = "Change dirt to water!",
    stack_max = 1,
    inventory_image = "default_water.png",
    on_place = function(_,_,pointed_thing)
        local looking_at = minetest.get_node(pointed_thing.under)
        if looking_at.name == "default:dirt" then
            minetest.set_node(pointed_thing.under,{name = "default:water"})
        end
    end,
})
Am I correct in assuming that if I place a dirt node, it will then change the node to water.
It doesn't seem to change anything.
It creates a "teaching:conditionals" item that you can use to turn dirt into water by right-clicking the dirt with it.
Every time a mod API is left undocumented, a koala dies.

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: Display and add varaibles

by basil60 » Post

Thanks
so how can I access that item. Does it put it into one's inventory? Do I need to manually add it to inventory.

User avatar
qwertymine3
Member
Posts: 202
Joined: Wed Jun 03, 2015 14:33
GitHub: Qwertymine
In-game: qwertymine3

Re: Display and add varaibles

by qwertymine3 » Post

You would have to use /giveme teaching:condtionals or the creative inventory menu

Also this may be a good example of these types of loops.

Code: Select all

  minetest.register_craftitem("teaching:checking_loop",{ 
         description = "Make a tower of mese!", 
         inventory_image = "default_mese_block.png", 
         stack_max = 1, 
          on_place = function(_,_,pointed_thing) 
                  local pos = pointed_thing.under 
                  local node = minetest.get_node(pos) 
                  while node.name == "default:mese" do 
                          pos.y = pos.y + 1 
                          node = minetest.get_node(pos) 
                  end 
                  minetest.set_node(pos,{name = "default:mese"}) 
          end, 
  }) 
   
  minetest.register_craftitem("teaching:checking_loop_b",{ 
          description = "Make another tower of mese!", 
          inventory_image = "default_mese_block.png", 
          stack_max = 1, 
          on_place = function(_,_,pointed_thing) 
                  local pos = pointed_thing.under 
                  repeat 
                          pos.y = pos.y + 1 
                          node = minetest.get_node(pos) 
                  until node.name ~= "default:mese" 
                  minetest.set_node(pos,{name = "default:mese"}) 
          end, 
  })      
Avatar by :devnko-ennekappao:

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: Display and add varaibles

by basil60 » Post

Cheers

I'll look at it...

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: Display and add varaibles

by basil60 » Post

So the /giveme worked OK.
I slightly changed the code you gave me:

Code: Select all

minetest.register_craftitem("teaching:conditionals",{
    description = "Change dirt to water!",
    stack_max = 1,
    inventory_image = "default_water.png",
    on_place = function(_,_,pointed_thing)
        local looking_at = minetest.get_node(pointed_thing.under)
        if looking_at.name == "default:dirt" then
            minetest.set_node(pointed_thing.under,{name = "default:water"})
        end
    end,
})
It now returns an error when I right click on a dirt block:

Code: Select all

2016-10-06 18:56:06: ERROR[ServerThread]: Map::setNode(): Not allowing to place
CONTENT_IGNORE while trying to replace "default:dirt" at (-25,3,-21) (block (-2,
0,-2))
Hope I'm not just being thick.

User avatar
qwertymine3
Member
Posts: 202
Joined: Wed Jun 03, 2015 14:33
GitHub: Qwertymine
In-game: qwertymine3

Re: Display and add varaibles

by qwertymine3 » Post

basil60 wrote:So the /giveme worked OK.
I slightly changed the code you gave me:

Code: Select all

            minetest.set_node(pointed_thing.under,{name = "default:water"})
Hope I'm not just being thick.
The name needs to be default:water_source
Avatar by :devnko-ennekappao:

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: Display and add varaibles

by basil60 » Post

Thanks
That's perfect

User avatar
qwertymine3
Member
Posts: 202
Joined: Wed Jun 03, 2015 14:33
GitHub: Qwertymine
In-game: qwertymine3

Re: Display and add varaibles

by qwertymine3 » Post

Here is an example for the basic for loop

Code: Select all

  minetest.register_craftitem("teaching:for_loop",{ 
          description = "Make a geysir!", 
          inventory_image = "default_water.png", 
          stack_max = 1, 
          on_place = function(_,_,pointed_thing) 
                  local pos = pointed_thing.above 
                  for i=1,10 do 
                          minetest.set_node({x=pos.x,y=pos.y+i,z=pos.z},{name="default:river_water_source"}) 
                  end     
          end,
  })
Avatar by :devnko-ennekappao:

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: Display and add varaibles

by basil60 » Post

Thank you

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: Display and add varaibles

by basil60 » Post

Qwertymine3 - another really noob question - how do you get rid of the geyser?

User avatar
qwertymine3
Member
Posts: 202
Joined: Wed Jun 03, 2015 14:33
GitHub: Qwertymine
In-game: qwertymine3

Re: Display and add varaibles

by qwertymine3 » Post

Place nodes through the center of the geyser to remove the source nodes
Avatar by :devnko-ennekappao:

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests