Jump to content

add variable to command in lua


Go to solution Solved by Syreldar,

Recommended Posts

 

hi there

i asked if there is any way to add variable from input to command in the quest

i tried to make it like that

 -- name is a input variable 
command("code "..name.." 1")
command("code ",name," 1")  
command("code "+name+" 1")

local test = command("code ")
chat(test..name.." ")

local test = command("code ") + name + " 1"
chat(test)

the code was not executed correctly in the game
any one know the solution ?

thnx alot

Link to comment
Share on other sites

  • Premium

It depends, what are you trying to do? What's the command supposed to send/receive?

command("code "..name.." 1");
command(string.format("code %s 1", name));

These are both written with valid syntax and do the same thing.

  • Love 1

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Link to comment
Share on other sites

2 minutes ago, Syreldar said:

It depends, what are you trying to do? What's the command supposed to send/receive?

command("code "..name.." 1");
command(string.format("code %s 1", name));

These are both written with valid syntax and do the same thing.

Thank you for caring

its a voice chat system the code syntax is

voice_chat [name] [id]

so i type quest in this way

 

quest private_voice_chat begin
	state start begin	
		when 40003.use begin
			say_event_title(item_name(40003))
				say()
				say_light_blue(" type the receiver name ")
				say()
				name = tonumber(input())
				say_event_title(item_name(40003))
					say()
					say_light_blue(" choose voice ")
					say()
					local voice = select(" voice 1 ", " voice 2 ", " cancel ")
					if voice == 1 then
						command(string.format("voice_chat %s 1", name));
					elseif voice == 2 then
						command(string.format("voice_chat %s 2", name));
					else
						return
				end
		end
	end
end

but it didn't work 😐
if i used code in game /voice_chat [name] [id]

it works
 

Link to comment
Share on other sites

  • Premium
  • Solution
name = tonumber(input())

Your input only accepts numerical values and you didn't make any input validation.

If "name" is supposed to be the name of a player this will never work because of that. It should be:

name = input()

and then add some validation to check if the name exist and the player is online, if necessary.

 

quest private_voice_chat begin
	state start begin	
		when 40003.use begin
 			local item_vnum = item.get_vnum();
			say_event_title(string.format("%s:[ENTER]", item_vnum))
			say_light_blue("Type the receiver's name:[ENTER]")
			local name = tonumber(input());

			say_event_title(string.format("%s:[ENTER]", item_vnum))
			if (not name) then
				return say_reward("Insert a name, please.[ENTER]");
  			end -- if
  
			say_light_blue("Choose the channel:[ENTER]")
			local voice_channels_list = {};
			local voice_channels_num = 2; -- Add as many as you want.
			for i = 1, voice_channels_num do
				table.insert(voice_channels_list, string.format("Voice n.%d", i));
  			end -- for
  			table.insert(voice_channels_list, "Abort");

  			local voice_selection = select_table(voice_channels_list);
  			if (voice_selection == table.getn(voice_channels_list)) then
				return; -- Abort
  			end -- if
			
			command(string.format("voice_chat %s %d", name, voice_selection));
		end -- when
	end -- state
end -- quest

 

Edited by Syreldar

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Link to comment
Share on other sites

1 minute ago, Syreldar said:
name = tonumber(input())

Your input only accepts numerical values and you didn't make any input validation.

If "name" is supposed to be the name of a player this will never work because of that. It should be:

name = input()

and then add some validation to check if the name exist and the player is online, if necessary.

🤦‍♂️🤦‍♂️
i didn't recognize it
it works Perfect now
thnx alot 🥰

can i get the target name as avariable to the command ?

Link to comment
Share on other sites

  • Premium
3 minutes ago, Ropen said:

🤦‍♂️🤦‍♂️
i didn't recognize it
it works Perfect now
thnx alot 🥰

can i get the target name as avariable to the command ?

What do you mean?

 

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Link to comment
Share on other sites

  • Premium
13 minutes ago, Ropen said:

if i select a plyer and cilick on the item that i use in quest could " the selected player " being my variable in command ?

I think this does it [ target.pc() ]

You need to 'pc.select' the vid of the actor in order to get its pc_name without manually typing it.

You need the npc instance to not be null in order to get its vid and apply it to 'pc.select'.

npc instance is null unless called by a trigger which links to an npc, like .chat or.take, and will just crash your core if called in other circumstances.

 

So it's not possible with basic Metin2 features to do this via quest. 'target.' functions are not related to your current target, but to a target instance, which you may recognize as the big arrow that can be on a location or on an NPC's head when doing quests.

Edited by Syreldar

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Link to comment
Share on other sites

  • Premium
17 hours ago, Ropen said:

It was a desperate attempt

You're free to use semicolons if you like them. Lua doesn't rly care.

They're used to write multiple statements in a single line, but I use them regardless cause they're one of my many signatures, they don't change the code flow anyway.

Edited by Syreldar
  • Metin2 Dev 1

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Link to comment
Share on other sites

1 minute ago, Syreldar said:

You're free to use semicolons if you like them. Lua doesn't rly care.

They're also used to write multiple statements in a single line, I do regardless cause they're one of my many signatures and don't change the code's flow.

yeah , i think  WeedHex means my previous attempts
this one
 

command("code ",name," 1")  

 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.