Page 1 of 1

Display and add varaibles

PostPosted: Sun Sep 25, 2016 10:07
by basil60
Hi
I'm a noob and trying to do some basic stuff.

My code looks like 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
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:
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
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?

Re: Display and add varaibles

PostPosted: Sun Sep 25, 2016 11:59
by qwertymine3
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().

Re: Display and add varaibles

PostPosted: Wed Sep 28, 2016 23:32
by basil60
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?

Re: Display and add varaibles

PostPosted: Thu Sep 29, 2016 22:47
by qwertymine3
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.

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_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,
})

Re: Display and add varaibles

PostPosted: Mon Oct 03, 2016 07:03
by basil60
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!

Re: Display and add varaibles

PostPosted: Tue Oct 04, 2016 09:10
by basil60
qwertymine3
I tried this, in anew mod folder called "teaching"
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_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.

Re: Display and add varaibles

PostPosted: Tue Oct 04, 2016 17:31
by Byakuren
basil60 wrote:qwertymine3
I tried this, in anew mod folder called "teaching"
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_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.

Re: Display and add varaibles

PostPosted: Tue Oct 04, 2016 20:53
by basil60
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.

Re: Display and add varaibles

PostPosted: Wed Oct 05, 2016 14:47
by qwertymine3
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.
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_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,
  })     

Re: Display and add varaibles

PostPosted: Wed Oct 05, 2016 20:16
by basil60
Cheers

I'll look at it...

Re: Display and add varaibles

PostPosted: Thu Oct 06, 2016 09:00
by basil60
So the /giveme worked OK.
I slightly changed the code you gave me:
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_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:
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
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.

Re: Display and add varaibles

PostPosted: Thu Oct 06, 2016 17:24
by qwertymine3
basil60 wrote:So the /giveme worked OK.
I slightly changed the code you gave me:
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.set_node(pointed_thing.under,{name = "default:water"})

Hope I'm not just being thick.


The name needs to be default:water_source

Re: Display and add varaibles

PostPosted: Thu Oct 06, 2016 20:49
by basil60
Thanks
That's perfect

Re: Display and add varaibles

PostPosted: Fri Oct 07, 2016 21:54
by qwertymine3
Here is an example for the basic for loop

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_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,
  })

Re: Display and add varaibles

PostPosted: Wed Oct 12, 2016 07:02
by basil60
Thank you

Re: Display and add varaibles

PostPosted: Tue Oct 18, 2016 23:31
by basil60
Qwertymine3 - another really noob question - how do you get rid of the geyser?

Re: Display and add varaibles

PostPosted: Wed Oct 19, 2016 15:12
by qwertymine3
Place nodes through the center of the geyser to remove the source nodes