Jump to content

Loop Issue


Recommended Posts

Hello

So this has been my issue for the past 6 hours and I cannot crack it so I have given in and come to where I know someone will know the answer. 

Anyway, basically I am using this section of code:

Spoiler

                    while (true)
                    {
                        if (Game.IsKeyDown(Keys.L))
                            LKeyPressed();

                        if (Game.IsKeyDown(Keys.K))
                            KKeyPressed();
                    }

However, when this code is carried out it freezes the game or takes the game to around 1 frame every 12-15 seconds (no exaggeration). This section of code isn't being carried out in a loop (hence that's why I made my own). But any help is much appreciated, thanks guys.

Edited by ScottehBoyy
Typo

ScottyTooHotty

Link to comment
Share on other sites

First of all, make sure you run any looping code in a new fiber. Second, yield the fiber on every loop. 

GameFiber.StartNew(delegate{
	while (true)
	{
		GameFiber.Yield();

		if (Game.IsKeyDown(Keys.L))
			LKeyPressed();

		if (Game.IsKeyDown(Keys.K))
			KKeyPressed();
	}
});

 

[REL] Coastal Callouts: An action-packed mod with new vehicles, maps, capabilities, and callouts in and around the waters of Los Santos

[REL] Police Tape: Make your scenes more realistic while stopping peds and traffic

[REL] Better EMS: Realistic and dynamic EMS response

Join the Parks Benefactor Program to support my work and get early beta access!

Link to comment
Share on other sites

If you're not already, wrap it in a GameFiber. If you are, then add a call to GameFiber.Yield(). Something like this should work:

 

GameFiber.StartNew(delegate {
	while(true) {
		if (Game.IsKeyDown(Keys.L))
			LKeyPressed();
		if (Game.IsKeyDown(Keys.K))
			KKeyPressed();
		GameFiber.Yield();
	}
});

 

My YouTube Channel: Darkmyre Gaming (Australian LSPDFR patrols, plugins in development, and other games)

My Discord Server | AusGamer Network

 

Please do not PM me for technical support or bug reports, use the appropriate forum or plugin's comments instead.

Link to comment
Share on other sites

10 hours ago, Darkmyre said:

If you're not already, wrap it in a GameFiber. If you are, then add a call to GameFiber.Yield(). Something like this should work:

 


GameFiber.StartNew(delegate {
	while(true) {
		if (Game.IsKeyDown(Keys.L))
			LKeyPressed();
		if (Game.IsKeyDown(Keys.K))
			KKeyPressed();
		GameFiber.Yield();
	}
});

 

Thanks guys, it was already in a GameFiber but there was no yield, thanks. @Darkmyre @PNWParksFan

ScottyTooHotty

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...