Quantcast

Jump to content

» «
Photo

ScriptHook menu VERY slow

No replies to this topic
Zolika1351
  • Zolika1351

    Ladies, hide your titties!

  • Members
  • Joined: 16 Jun 2014
  • Hungary

#1

Posted A week ago Edited by Zolika1351, A week ago.

I've been making a menu, and it seems to be VERY, VERY slow at recognizing keypresses, and I have to hold down the next option key for at least a second before it actually does anything.

A bit of my menu code:

void Updatemenu()
{
	if (menu1 == 3)
	{
		menu1 = 0;
	}
	if (menu1 == -1)
	{
		menu1 = 2;
	}
	//Wait(1);
	if (menu1 == 0)
	{
		//print("Action: Teleport To");
		TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
		DisplayTextWithLiteralString((pos_x + 0.1500), (pos_y), "STRING", "Teleports");
		TEXT_DRAW(0.3, 0.3, 127, 127, 127, 255);
		DisplayTextWithLiteralString((pos_x + 0.1500), (pos_y + 0.032), "STRING", "Spawn");
		TEXT_DRAW(0.3, 0.3, 127, 127, 127, 255);
		DisplayTextWithLiteralString((pos_x + 0.1500), (pos_y + 0.032 + 0.032), "STRING", "Car Options");
		if (SubMenuNumber == 1)
		{
			if (menu2 == 0)
			{
				TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
				DisplayTextWithLiteralString((pos_x + 0.3000), (pos_y), "STRING", "Player Teleports");
				TEXT_DRAW(0.3, 0.3, 127, 127, 127, 255);
				DisplayTextWithLiteralString((pos_x + 0.3000), (pos_y + 0.032), "STRING", "World Teleports");
			}
			if (menu2 == 1)
			{
				TEXT_DRAW(0.3, 0.3, 127, 127, 127, 255);
				DisplayTextWithLiteralString((pos_x + 0.3000), (pos_y), "STRING", "Player Teleports");
				TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
				DisplayTextWithLiteralString((pos_x + 0.3000), (pos_y + 0.032), "STRING", "World Teleports");
			}
			if (menu2 == 2)
			{
				menu2 = 0;
			}
			if (menu2 == -1)
			{
				menu2 = 1;
			}
		}
		if (SubMenuNumber == 2)
		{
			if (menu2 == 0)
			{
				TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
				DisplayTextWithLiteralString((pos_x + 0.3000), (pos_y), "STRING", "Player Teleports");
				TEXT_DRAW(0.3, 0.3, 127, 127, 127, 255);
				DisplayTextWithLiteralString((pos_x + 0.3000), (pos_y + 0.032), "STRING", "World Teleports");
				if (submenumode == 0)
				{
					TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
					DisplayTextWithLiteralString((pos_x + 0.4500), (pos_y), "STRING", "To Player");
					TEXT_DRAW(0.3, 0.3, 127, 127, 127, 255);
					DisplayTextWithLiteralString((pos_x + 0.4500), (pos_y + 0.032), "STRING", "Into Car");
					mode = 0;
				}
				if (submenumode == 1)
				{
					TEXT_DRAW(0.3, 0.3, 127, 127, 127, 255);
					DisplayTextWithLiteralString((pos_x + 0.4500), (pos_y), "STRING", "To Player");
					TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
					DisplayTextWithLiteralString((pos_x + 0.4500), (pos_y + 0.032), "STRING", "Into Car");
					mode = 1;
				}
				if (submenumode == 2)
				{
					submenumode = 0;
				}
				if (submenumode == -1)
				{
					submenumode = 1;
				}
			}
			if (menu2 == 1)
			{
				TEXT_DRAW(0.3, 0.3, 127, 127, 127, 255);
				DisplayTextWithLiteralString((pos_x + 0.3000), (pos_y), "STRING", "Player Teleports");
				TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
				DisplayTextWithLiteralString((pos_x + 0.3000), (pos_y + 0.032), "STRING", "World Teleports");
			}
			if (menu2 == 2)
			{
				menu2 = 0;
			}
			if (menu2 == -1)
			{
				menu2 = 1;
			}
		}
	}

Execution code:

// The real script
void CustomFiberThread::ExecuteOption()
{
			if (mode == 0) // Teleport To
			{
				Ped hisplayermodel;
				f32 hisX;
				f32 hisY;
				f32 hisZ;
				GetPlayerChar(i, &hisplayermodel);
				GetCharCoordinates(hisplayermodel, &hisX, &hisY, &hisZ); 
				SetCharCoordinates(GetPlayerPed(), hisX, hisY, hisZ);
			}
			if (mode == 1) // Teleport Into Car
			{
				int seats;
				Ped hisplayermodel;
				Vehicle hiscar;
				f32 hisX;
				f32 hisY;
				f32 hisZ;
				Ped driver;
				GetPlayerChar(i, &hisplayermodel);
				if(IsCharInAnyCar(hisplayermodel))
				{
					GetCarCharIsUsing(hisplayermodel, &hiscar);
					GetDriverOfCar(hiscar, &driver);
					if(!DoesCharExist(driver)) WarpCharIntoCar(GetPlayerPed(),hiscar);
					else for(seats = 0;seats <= 2;seats++)if(IsCarPassengerSeatFree(hiscar, seats)) WarpCharIntoCarAsPassenger(GetPlayerPed(),hiscar,seats);
				}
			}

Keypress code:

	while(IsThreadAlive())
	{
		
		if ((GetAsyncKeyState(VK_F7) & 1) != 0)
		{
			if(MenuYey)
			{
				MenuYey = false;
			}
			else if(!MenuYey)
			{
				MenuYey = true;
			}
		}
		// Call Wait() so we can process other scripts/game code
		// You must call Wait(...) in your loop code for a fiber thread!
		if(MenuYey)
		{
			if ((GetAsyncKeyState(VK_NUMPAD2) & 1) != 0)
			{
				LogInfo("NUM 2 Pressed");
				if(SubMenuNumber == 0)
				{
					menu1++;
				}
				else if(SubMenuNumber == 1)
				{
					menu2++;
				}
				else if(SubMenuNumber == 2)
				{
					submenumode++;
				}
				Updatemenu();
			}
			if ((GetAsyncKeyState(VK_NUMPAD8) & 1) != 0)
			{
				LogInfo("NUM 8 Pressed");
				if(SubMenuNumber == 0)
				{
					menu1--;
					Updatemenu();
				}
				else if(SubMenuNumber == 1)
				{
					menu2--;
					Updatemenu();
				}
				else if(SubMenuNumber == 2)
				{
					submenumode--;
					Updatemenu();
				}
				Updatemenu();
			}
			if ((GetAsyncKeyState(VK_NUMPAD5) & 1) != 0)
			{
				LogInfo("NUM 5 Pressed");
				if(SubMenuNumber == 0)
				{
					SubMenuNumber = 1;
				}
				else if(SubMenuNumber == 1)
				{
					SubMenuNumber = 2;
				}
				else if(SubMenuNumber == 2)
				{
					ExecuteOption();
				}
			}
			if ((GetAsyncKeyState(VK_NUMPAD0) & 1) != 0)
			{
				LogInfo("NUM 0 Pressed");
				if(SubMenuNumber == 1)
				{
					SubMenuNumber = 0;
				}
				else if(SubMenuNumber == 2)
				{
					SubMenuNumber = 1;
				}
			}
			if ((GetAsyncKeyState(VK_NUMPAD4) & 1) != 0)
			{
				LogInfo("NUM 4 Pressed");
				i--;
				UpdateISub();
			}
			if ((GetAsyncKeyState(VK_NUMPAD6) & 1) != 0)
			{
				LogInfo("NUM 6 Pressed");
				i++;
				UpdateIAdd();
			}
			if(IsNetworkSession())
			{
				NativeInvoke::Invoke<ScriptVoid>("GET_PLAYER_RGB_COLOUR", i, &gr, &gg, &gb);
				TEXT_DRAW(0.3, 0.3, gr, gg, gb, 255);
				DisplayTextWithLiteralString((pos_x), pos_y, "STRING", GetPlayerName(i));
				if(NativeInvoke::Invoke<ScriptAny>("GET_HOST_ID") == i)
				{
					TEXT_DRAW(0.3, 0.3, gr, gg, gb, 255);
					DisplayTextWithLiteralString((pos_x), (pos_y - 0.032), "STRING", "HOST");
				}
				if(NativeInvoke::Invoke<ScriptAny>("GET_HOST_ID") == GetPlayerId())
				{
					NativeInvoke::Invoke<ScriptVoid>("GET_PLAYER_RGB_COLOUR", GetPlayerId(), &gr, &gg, &gb);
					TEXT_DRAW(0.3, 0.3, gr, gg, gb, 255);
					DisplayTextWithLiteralString((pos_x), (pos_y + 0.032), "STRING", "YOU ARE HOST");
				}
			}
			else
			{
				if(GetCurrentEpisode() == 0)
				{
					TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
					DisplayTextWithLiteralString((pos_x), pos_y, "STRING", "Niko Bellic");
				}
				if(GetCurrentEpisode() == 1)
				{
					TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
					DisplayTextWithLiteralString((pos_x), pos_y, "STRING", "Johnny Klebitz");
				}
				if(GetCurrentEpisode() == 2)
				{
					TEXT_DRAW(0.3, 0.3, 255, 255, 255, 255);
					DisplayTextWithLiteralString((pos_x), pos_y, "STRING", "Luis Lopez");
				}
			}
			Updatemenu();
		}
		else if ((GetAsyncKeyState(VK_NUMPAD4) & 1) != 0)
		{
			LogInfo("num4 Pressed");
		}
		else if ((GetAsyncKeyState(VK_NUMPAD6) & 1) != 0)
		{
			LogInfo("num6 Pressed");
		}
		else if ((GetAsyncKeyState(VK_NUMPAD5) & 1) != 0)
		{
			LogInfo("num5 Pressed");
		}
		else if ((GetAsyncKeyState(VK_NUMPAD8) & 1) != 0)
		{
			LogInfo("num8 Pressed");
		}
		else if ((GetAsyncKeyState(VK_NUMPAD2) & 1) != 0)
		{
			LogInfo("num2 Pressed");
		}
		Wait(0);
	}

I know that the code itself is VERY unoptimized, but I have no idea how to optimize it. Any ideas? Selecting options/submenus seems to be instantaneous, but scrolling and going backwards is the slow part.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users