Jump to content
  • Join-Us

    https://metin2.dev is the 1st international community on Metin2 development. Join us to develop your skills and share your knowledge with the community, respectful and ethical forum.

  • 0

True, False or 0, 1, -1?


ReFresh

Question

  • Premium
Posted (edited)

Hey guys,

I'm wondering what should I use in this function, because everywhere in LUA .cpp functions are 0 or 1 or -1, can someone explain me what does it mean and should I use number or boolean return in LUA .cpp functions?

Spoiler
ALUA(game_open_safebox)
	{
		CQuestManager& q = CQuestManager::instance();
		LPCHARACTER ch = q.GetCurrentCharacterPtr();

		if (ch->GetExchange())
		{
			ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("´Ů¸Ą°Ĺ·ˇĂ˘ŔĚ ż­¸°»óĹÂżˇĽ­´Â â°í¸¦ ż­Ľö°ˇ ľř˝Ŕ´Ď´Ů."));
			/// return false; or return 0, 1, -1;???
		}
		else
		{
			ch->SetSafeboxOpenPosition();
			ch->ChatPacket(CHAT_TYPE_COMMAND, "ShowMeSafeboxPassword");
		}

		return 0;
	}

 

Thanks for possible answers!

Sincerely,

ReFresh

Edited by ReFresh

I'll be always helpful! 👊 

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0
  • Honorable Member

return value: number of results (https://www.lua.org/pil/26.1.html)

static int l_sin (lua_State *L) {
      double d = luaL_checknumber(L, 1);
      lua_pushnumber(L, sin(d));
      return 1;  /* number of results */
    }

Example from m2:

Spoiler
	int forked_sungzi_start_pos( lua_State* L )
	{
		LPCHARACTER ch = CQuestManager::instance().GetCurrentCharacterPtr();

		const ForkedSungziMapInfo& info = CThreeWayWar::instance().GetEventSungZiMapInfo();

		lua_pushnumber( L, info.m_iForkedSungziStartPosition[ch->GetEmpire()-1][0] );
		lua_pushnumber( L, info.m_iForkedSungziStartPosition[ch->GetEmpire()-1][1] );

		return 2; // 2 result
	}

	int game_get_event_flag(lua_State* L)
	{
		CQuestManager& q = CQuestManager::instance();

		if (lua_isstring(L,1))
			lua_pushnumber(L, q.GetEventFlag(lua_tostring(L,1)));
		else
			lua_pushnumber(L, 0);

		return 1; // 1 result
	}
  
  	int game_open_safebox(lua_State* /*L*/)
	{
		CQuestManager& q = CQuestManager::instance();
		LPCHARACTER ch = q.GetCurrentCharacterPtr();
		ch->SetSafeboxOpenPosition();
		ch->ChatPacket(CHAT_TYPE_COMMAND, "ShowMeSafeboxPassword");
		return 0; // no result
	}
  	
  

 

Other examples:

Spoiler
static int c_swap (lua_State *L) {
    //check and fetch the arguments
    double arg1 = luaL_checknumber (L, 1);
    double arg2 = luaL_checknumber (L, 2);

    //push the results
    lua_pushnumber(L, arg2);
    lua_pushnumber(L, arg1);
  
    return 2; // number of results
}

 

 

  • Metin2 Dev 1
  • Good 1
Link to comment
Share on other sites

  • 0
  • Premium

Honestly, I've never used return -1. However, if you give 0 or 1 to the return command, it will directly interpret it as a boolean

return 0; /*or*/ return false; //they are equal
return 1; /*or*/ return true; //they are equal

btw, the result of return has type int

  • Metin2 Dev 1

My signature (yes, I'm a creative guy)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • 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.