Jump to content

[LUA/.quest] how to select and return to number field with mysql.query¿?


Recommended Posts

hi master devs! i was trying to get an field from an mysql table, in the query builder from the mysql client it works, but when im gonan try it out on a quest, it doesnt works...

here is what ive done:

i put it into a function cause i took it from another reference... noob alert! xD

function itemidputo(item_id)
    local item_id = item.get_id()
    return mysql.query(string.format("SELECT duration FROM %s.new_petsystem WHERE id = %.0f;", auction.PLAYER_DATABASE, item_id)).duration[1]
end

and im calling it for a test with just an local in a say fucntion, but when i call the function, nothin appears, and the error log doesnt says anything... compilling it works fine.. any idea?


i try to select the field from the column DURATION from the row ID, im trying to get some new knowledge from this kind of lua using, but i doesnt find anything from mysql references with my requiriments...

well if anybody has any idea, i appreciate it! gonna keep trying! thanks in advance! :D

Link to comment
Share on other sites

  • Premium

Hi.

First of all, what the hell is that ".duration[1]" at the end of the mysql.query function?

2. By calling a function named item_id, you are overriding the function arg which is also a var named item_id. Which means that regardless of what you insert inside the function arg, it will return a static value, item.get_id() which is what you put inside "local item_id".

Check the function i've just written: http://pastebin.com/1E0gJgnu

This is for you; http://pastebin.com/nvRAXySX

  • Love 2

 

"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

thanks for your anwer, but, i dont could make it works... 

in your example;

 

function GetDuration(item_id)
    return mysql.query(string.format("SELECT duration FROM %s.new_petsystem WHERE id = %d LIMIT 1;", player, item_id));
end 

yo use "item_id" as the argument you want to declare later right? so in the example it shopuld be

 

when xxx.take begin
	local item_id = item.get_id()
	say("item duration: "..GetDuration(item_id))
end

right? cause that doesnt wokr either... i dunno why :/

i have tryend to comment it with that argument and without... and one more questio! :3 when you say: 

WHERE id = %d 

that %d is the argument named "item_id" right? so i have to put later an "local item_id = item.get_id()" to get that "%d" or where is the mistake instead?

im tired with this one hahah, thansk for your help! i will continue later on it, have a nice day!

Link to comment
Share on other sites

  • Premium

there is a difference between inserting that var in the quest and inserting it inside the function. XD

by inserting it inside the function and naming it the same as the function arg you make a mistake since you override it, by inserting it inside the quest you save the item id inside a local variable which you can use for the quest, and you can do it.

no you don't have to put item_id inside the function since the function already takes an argument named item_id.

This is basic programing, i shouldn't be there teaching you what does that mean cause understanding how variables work is simple, almost like doing 1+1.

local LOL = item.get_id() -> GetDuration(LOL) -> mysql.query(string.format("SELECT duration FROM %s.new_petsystem WHERE id = %d LIMIT 1;", player, LOL))

get it?

 

 

  • 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

  • Premium
4 minutes ago, PACI said:

It should return a lua table, that's why.

Then why just not [1]? He said he wants a single vnum: the one which fullfills his condition. Specifying the column name when you already check for the column to be a determined one is rather useless, LIMIT 1 also does the job (and i prefer it over the [1] after the mysql.query).

  • 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

  • Premium
5 minutes ago, PACI said:

Because the returning table has as key the selected column's name.

tbl = { [columnName] = {values} }

Yes, but specifying the key when you already check for the column to be a determined one in the query is rather useless, LIMIT 1 also does the job (and i prefer it over the [1] after the mysql.query).

Three ways of doing the same thing are:

.["duration"][1] (I dislike it.)

[1] after the mysql.function (since you already select a column, in this case duration, and you just want one result)

LIMIT 1 at the end of the query.

  • 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

  • Premium
8 minutes ago, PACI said:

I'm not sure, I don't usually use this function on my quests, however, I think it still returns the same table type even if you provide a record limit at your query.

Be right back, i'm gonna test for science.

  • 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

  • Premium
32 minutes ago, PACI said:

I'm not sure, I don't usually use this function on my quests, however, I think it still returns the same table type even if you provide a record limit at your query.

http://pastebin.com/9N4LRxTu

if i use ["vnum"][1] or .vnum[1] it doesn't recognize the key vnum and instead says that it doesn't exist, so i suppose the returned table is

{[number] = value}

and not

{[column] = value}

  • Love 2

 

"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

uh shalala what a level of knowledge, well, masters of disasters, thanks for your advices! i have an new goal now! check out as mny courses as i can from basic prorgammit to get into it. in some years i want to study anyways informatics so it will be necessary anyways!

@Syreldar; about your last comment, isnt it right that named variables have to be saved inside "", maybe its that the reason it doesnt recognize it... xDD! well i dont want to be a smatass, you two seem to know all better than me xDD gonna to stop my local metin2 project, im tired of copieng thinks, and trying to make their work, with just realing the half of what i wrote in a code xD! i think python and lua is very interesting, and c++ too, just needs english knowledge to understand it hahah, and time of course,  well, gonna search some lessons for it xD!

by the way... does anyone of you two, know some good lesson stuff, or website where it could be?

thanks and, keep it going guys! take care! :D

im gonna press that like button on you all hahaha my way to appreciate it +_+


signed in my first course on
https://courses.edx.org/
:3

  • Love 1
Link to comment
Share on other sites

  • Developer

Well you can look at their official sites (Lua, Python, C++). But you can actually learn just by taking a look to some other quests, or python files from the game itself.
Can't say the same for C++, although you can easily get the logic just by reading a couple of code lines. As you said, you only need a bit of english knowledge to start with it, and then you just keep going, and trying different things.
Your mistakes will also increase your knowledge, so go for it. :)

  • Love 2

when you return 0 and server doesn't boot:

unknown.png

Link to comment
Share on other sites

I dont read all what you say but maybe this can be help you, idk. xD

Here is better version via c++ :ph34r:

d19751f6294743eb8304f277b46faf77.png

//@ //server//locale/country/test.lua:
quest get_duration begin
    state start begin
        when 70001.use begin
		local item_id = item.get_id()
		syschat("<Debug Mode> QUEST get_duration result : "..pc.get_duration(item_id).." xD")
        end
    end
end
//@ //server//locale/country/questfunction:
//1.) Add where you want:
pc.get_duration

//@ /svn/Server/game/src/questlua_pc.cpp
//1.) Search:
	{ "reset_status",		pc_reset_status	},
//2.) Add bellow:
	{ "get_duration",        pc_get_duration },
//3.) Search:
	int pc_get_hp(lua_State * L)  
	{
		lua_pushnumber(L, CQuestManager::instance().GetCurrentCharacterPtr()->GetHP());
		return 1;
	}
//4.) Add bellow:
int pc_get_duration(lua_State * L)
{
	if (!lua_isnumber(L, 1))
	{
		sys_err("QUEST get_duration call error : wrong argument");
		return 0;
	}
		
	int duration = lua_tonumber(L, 1);
	LPCHARACTER ch = CQuestManager::instance().GetCurrentCharacterPtr();

	if (ch)
		lua_pushnumber(L, ch->GetDuration(duration));
	return 1;
}

//@ /svn/Server/game/src/char.h
//1.) Search:
		void			ClearItem();
//2.) Add bellow:
		int			GetDuration(int dwItem);
//@ /svn/Server/game/src/char.cpp
//1.) Add in end line:		
int CHARACTER::GetDuration(int dwItem)
{
	char szQuery[CHAT_MAX_LEN + 1];
	snprintf(szQuery, sizeof(szQuery), "SELECT duration FROM player.new_petsystem WHERE id = %d LIMIT 1", dwItem);
	std::auto_ptr<SQLMsg> msg(DBManager::instance().DirectQuery(szQuery));

	int duration = 0;
	
	if (msg->Get()->uiNumRows == 0)
	{
		return duration;
	}
	else
	{
		MYSQL_ROW row = mysql_fetch_row(msg->Get()->pSQLResult);
		str_to_number(duration, row[0]); /* Poker face */
		return duration;
	}
}

 

Edited by Metin2 Dev
Core X - External 2 Internal
  • Love 3
Link to comment
Share on other sites

uooo!!!

3 hardcore masters answered to my question! im very glad to read you all! +_+
well, masters, ive just introduced with c++ im seeing that is one of the main programmig so i decided to start with it first of all

https://metin2.download/picture/Kd6K09046YNYb3t8TBKSv6B7rreBjWLB/.jpg

thanks to anyone! hope you have all a nice day! maybe ai give some beers for free! hahahha
and double thanks for vegas master for just write an explicit tutorial, yo have done an greta wok for the community and i read many of your how to's just wanted to say it directly, that kind of things make ud hobby noobs grow a bit more in this area! well, thansk again to all! :D



PS: i readed the code you wrote and im trying to understand it all, and yes, i got the main logic, but im wondering why the hell you wrote so much just for help xDD thanks again a lot! than even shows me how much i have to learn, gonna keep on it! i will surpass you all.........! someday! XDD!! <3

Edited by Metin2 Dev
Core X - External 2 Internal
  • Love 1
Link to comment
Share on other sites

You're welcome, we try to help as much as time allows.
But nobody here is master, everyone is good at what he do and in different areas.
Some of the world's more famous "programmers" are famous not so much for their programming skills (though they may be excellent) but for their innovations in certain aspects of this process - is the personality of that person.

  • Love 2
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



  • Similar Content

  • Activity

    1. 11

      Metin2 Closed Beta Content (2003-2004)

    2. 11

      Metin2 Closed Beta Content (2003-2004)

    3. 0

      Football Ground

    4. 11

      Metin2 Closed Beta Content (2003-2004)

    5. 0

      Error compile binary Zentoria2

    6. 11

      Metin2 Closed Beta Content (2003-2004)

  • Recently Browsing

    • No registered users viewing this page.
×
×
  • 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.