Jump to content

Problem at creating a simply lua function C++


Go to solution Solved by Shang,

Recommended Posts

Hi dev

 

Actually im trying to add a new function but i don't know why this is working bad:

I have in player a new column called: rank and i want that this function returns the number that i have in rank but i don't know how to do it ._.

	int pc_get_rank(lua_State* L)
	{
		DBManager::instance().Query("SELECT rank FROM player.player WHERE name='%s' ", CQuestManager::instance().GetCurrentCharacterPtr()->GetName());
		
		lua_pushnumber(L, 0);
		return 1;
	}

 

Function returns me all time 0 in game

:D

Link to comment
Share on other sites

  • Solution
	int pc_get_rank(lua_State* L)
	{
		LPCHARACTER ch = CQuestManager::instance().GetCurrentCharacterPtr();
		if (!ch)
		{
			lua_pushnumber(L, 0);
			return 1;
		}
	
		char szQuery[512];
		snprintf(szQuery, sizeof(szQuery), "SELECT rank FROM player.player WHERE name='%s'", ch->GetName());
		std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery(szQuery));
		
		if (pMsg->Get()->uiNumRows > 0)
		{
			MYSQL_ROW row = mysql_fetch_row(pMsg->Get()->pSQLResult);

			int iRank = 0;
			str_to_number(iRank, row[0]);
			lua_pushnumber(L, iRank);
			return 1;
		}
		
		lua_pushnumber(L, 0);
		return 1;
	}

You are not pushing the query return value, you're pushing 0 all the time.

Not tested. Oh, and it is better to declare first the LPCHARACTER to avoid core crashes if it doesn't exists.

PS: Where did you get the Dev member? In a bet?

  • Love 3
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.