Whats wrong with this code?

Post Reply
User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Whats wrong with this code?

by Joseph16 » Post

Code: Select all

minetest.override_item("default:sword_mese", {
	description = "Mese Sword",
	inventory_image = "default_tool_mesesword.png",
	tool_capabilities = {
		full_punch_interval = 0.1,
		max_drop_level=1,
		groupcaps={
			snappy={times={[1]=2.0, [2]=1.00, [3]=0.35}, uses=30, maxlevel=3},
		},
		damage_groups = {fleshy=7},
	},
	sound = {breaks = "default_tool_breaks"},
	on_place = function(itemstack, placer, pointed_thing)
		my_players[placer:get_player_name()] = true
	end
minetest.register_globalstep(function(dtime)
	for name, what in pairs(my_players) do
		local player = minetest.get_player_by_name(name)
		if player then
			my_players[name] = false -- player left
		else
			if !player:get_player_control().RMB then
	minetest.chat_send_all(math.random())
				my_players[name] = false
			end
		end
	end
end)
})
The error I get when running:

Code: Select all

2019-05-30 15:08:34: ERROR[Main]: ModError: Failed to load and run script from D:\Joseph\Minetest\minetest-5.1.0-38b94f2-win64\bin\..\mods\pvpengine\sword_override\init.lua:
2019-05-30 15:08:34: ERROR[Main]: ...94f2-win64\bin\..\mods\pvpengine\sword_override\init.lua:38: '}' expected (to close '{' at line 23) near 'minetest'
I have tried to fix this error but I don't know lua that well and I'm probably missing something really simple. Can someone help?
Testin' mines since 1989

User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: Whats wrong with this code?

by Joseph16 » Post

The line numbers in the error code are off because I didn't paste the entire init.lua. The entire file can be found here: https://pastebin.com/1QFNACKh
Testin' mines since 1989

Eran
Member
Posts: 123
Joined: Fri May 03, 2019 16:46

Re: Whats wrong with this code?

by Eran » Post

In my experience '} expected' in lua usually means there's a comma missing in a table. In this case it's after the on_place function.

User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: Whats wrong with this code?

by Joseph16 » Post

I tried tha but now it says that the "!" in the code: "!player:get_player_control().RMB then" is an unexpected character.
Testin' mines since 1989

User avatar
Hugues Ross
Member
Posts: 60
Joined: Mon May 06, 2019 22:52
GitHub: Df458
Contact:

Re: Whats wrong with this code?

by Hugues Ross » Post

ah... shouldn't !player:get_player_control().RMB be not player:get_player_control().RMB? It's been a while, but I don't think lua uses ! as an operator like many other languages do.

User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: Whats wrong with this code?

by Joseph16 » Post

rubenwardy gave me this code but he said it was untested, I don’t know enough about this coding language to properly debug it. Thanks.
Testin' mines since 1989

An0n3m0us
Member
Posts: 44
Joined: Thu Dec 01, 2016 19:15
GitHub: An0n3m0us

Re: Whats wrong with this code?

by An0n3m0us » Post

I've managed to get something working. I used the player api. Not sure where to go from here. Maybe you can work from here. (I've removed some of your code so don't replace your own with this one):

Code: Select all

minetest.override_item("default:sword_mese", {
	description = "Mese Sword",
	inventory_image = "default_tool_mesesword.png",
	tool_capabilities = {
	  full_punch_interval = 0.1,
	  max_drop_level=1,
	  groupcaps={
		 snappy={times={[1]=2.0, [2]=1.00, [3]=0.35}, uses=30, maxlevel=3},
	  },
	  damage_groups = {fleshy=7},
	},
})

minetest.register_globalstep(function(dtime)
	for _, player in pairs(minetest.get_connected_players()) do
	  local name = player:get_player_name()
	  if player then
		 -- ???
	  else
		 if not player:get_player_control().RMB then
			minetest.chat_send_all(math.random())
		    players[name] = false
		 end
	  end
	end
end)

User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: Whats wrong with this code?

by Joseph16 » Post

Thanks! I will try this!
Testin' mines since 1989

User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: Whats wrong with this code?

by Joseph16 » Post

An0n3m0us wrote:I've managed to get something working. I used the player api. Not sure where to go from here. Maybe you can work from here. (I've removed some of your code so don't replace your own with this one):

Code: Select all

minetest.override_item("default:sword_mese", {
	description = "Mese Sword",
	inventory_image = "default_tool_mesesword.png",
	tool_capabilities = {
	  full_punch_interval = 0.1,
	  max_drop_level=1,
	  groupcaps={
		 snappy={times={[1]=2.0, [2]=1.00, [3]=0.35}, uses=30, maxlevel=3},
	  },
	  damage_groups = {fleshy=7},
	},
})

minetest.register_globalstep(function(dtime)
	for _, player in pairs(minetest.get_connected_players()) do
	  local name = player:get_player_name()
	  if player then
		 -- ???
	  else
		 if not player:get_player_control().RMB then
			minetest.chat_send_all(math.random())
		    players[name] = false
		 end
	  end
	end
end)
Now I get this error:

Code: Select all

Undeclared global variable "itemstack" accessed at ...94f2-win64\bin\..\mods\pvpengine\sword_override\init.lua:40
2019-05-30 19:04:15: ERROR[Main]: ServerError: AsyncErr: environment_Step: Runtime error from mod 'sword_override' in callback environment_Step(): ...94f2-win64\bin\..\mods\pvpengine\sword_override\init.lua:40: attempt to index global 'itemstack' (a nil value)
Testin' mines since 1989

User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: Whats wrong with this code?

by Joseph16 » Post

I added itemstack to the globalstep function list

Code: Select all

minetest.register_globalstep(function(itemstack, dtime)
I am using this:

Code: Select all

minetest.register_globalstep(function(itemstack, dtime)
       for _, player in pairs(minetest.get_connected_players()) do
         local name = player:get_player_name()
         if player then
         itemstack:take_item(); return ("default:sword_diamond")
         else
           if not player:get_player_control().RMB then
             minetest.chat_send_all(math.random())
              players[name] = false
           end
         end
       end
    end)
Any ideas?
Testin' mines since 1989

User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: Whats wrong with this code?

by Joseph16 » Post

I want it to give me a diamond sword on RMB release.
Testin' mines since 1989

An0n3m0us
Member
Posts: 44
Joined: Thu Dec 01, 2016 19:15
GitHub: An0n3m0us

Re: Whats wrong with this code?

by An0n3m0us » Post

I'll give it a try.

An0n3m0us
Member
Posts: 44
Joined: Thu Dec 01, 2016 19:15
GitHub: An0n3m0us

Re: Whats wrong with this code?

by An0n3m0us » Post

Almost there!:

Code: Select all

function test(player)
	local inv = player:get_inventory()
	local stack = ItemStack("default:sword_mese")
	inv:add_item("main", stack)

	print(inv)
end

minetest.register_globalstep(function(itemstack, dtime)
	x = minetest.register_on_joinplayer(test)
    for _, player in pairs(minetest.get_connected_players()) do
        local name = player:get_player_name()
        local inv = player:get_inventory()
        if player then
		    print('test')
        else
            if not player:get_player_control().RMB then
                minetest.chat_send_all(math.random())
                players[name] = false
            end
        end
    end
end)

An0n3m0us
Member
Posts: 44
Joined: Thu Dec 01, 2016 19:15
GitHub: An0n3m0us

Re: Whats wrong with this code?

by An0n3m0us » Post

Done! It's kinda hacky (using x and named the function test), so you might wanna clean it up:

Code: Select all

x = 0

function test(player)
	local inv = player:get_inventory()
	local stack = ItemStack("default:sword_diamond")
	inv:add_item("main", stack)
	x = x + 1
end

minetest.register_globalstep(function(itemstack, dtime)
    for _, player in pairs(minetest.get_connected_players()) do
        local name = player:get_player_name()
        local inv = player:get_inventory()
		local controls = player:get_player_control()
		if player:get_player_control().RMB and x == 0 then
        	test(player)
		end
    end
end)
Here's the process:
1. Join the game
2. Press right click (diamond sword added)
3. Press right click again (nothing happens)

User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: Whats wrong with this code?

by Joseph16 » Post

Thanks!
Testin' mines since 1989

An0n3m0us
Member
Posts: 44
Joined: Thu Dec 01, 2016 19:15
GitHub: An0n3m0us

Re: Whats wrong with this code?

by An0n3m0us » Post

You're welcome.

User avatar
Krock
Developer
Posts: 4577
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: Whats wrong with this code?

by Krock » Post

Joseph13 wrote:rubenwardy gave me this code but he said it was untested, I don’t know enough about this coding language to properly debug it. Thanks.
Well then, I guess that's my new identity now. ¯\_(ツ)_/¯
http://irc.minetest.net/minetest/2019-05-30#i_5549623

Indeed, that should be not player:. It's a mistake I did due to some C programming earlier that day.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Joseph16
Member
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: Whats wrong with this code?

by Joseph16 » Post

Oh it was Krock..... I could swear Rubenwardy did this... ok thanks Krock
Testin' mines since 1989

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests