Jump to content
Sign in to follow this  
anthonyfromtheuk

Generic error, syntax problem?

Recommended Posts

I am trying to use this bit of code works fine until I get to "5" and then I get a generic error is it a syntax error or can somebody see whats wrong here?

roundcount = 5;
MY_KEYDOWN_FNC = {
   switch (_this) do {


               //Key U
       case 22: {
           roundcount = roundcount - 1;
           hint format ["%1",roundcount];
           if (roundcount < 1) then
               {hint "Reloading";
               Sleep 5;
               roundcount = 5;
               hint "Loaded"}
           else {
               nul = [] execVM "mousebomb.sqf";};
           };
   };
};

Edited by Anthonyfromtheuk

Share this post


Link to post
Share on other sites
shotcount = 0;
MY_KEYDOWN_FNC = {
   switch (_this) do {
               //Key U
       case 22: {
           shotcount = shotcount + 1;
           hint format ["%1",shotcount];
           if (shotcount > 4) then
               {hint "reloading";
               Sleep 5;
               shotcount = 0;
               hint "ready";}//<<<<<<Needed ";"
           else {
               nul = [] execVM "mousebomb.sqf";};
           };
   };
};

Share this post


Link to post
Share on other sites

Thanks i didn't notice that, still getting an error though. it is skipping the reloading hint and sleep and jumping straight to the ready hint while spitting a generic error.

error.jpg

Edited by Anthonyfromtheuk

Share this post


Link to post
Share on other sites
Thanks i didn't notice that, still getting an error though. it is skipping the reloading hint and sleep and jumping straight to the ready hint while spitting a generic error

May be due to the capitalization of the "Sleep" command, because I just pulled out the first part of the if statement and made the global variable roundcount = 0 in the init.sqf, and then had it run through that portion of the script, and it worked just fine, but other than that I'm not sure of any other issues with it.

Share this post


Link to post
Share on other sites

Defiantly is the sleep I have been moving it around and every time the error shows its on the same line as the sleep but I had already made it all lowercase. It runs through everything without the sleep in there, but the sleep is the point of the whole thing, to simulate a reload time. maybe because the whole thing is running in the init file, will have to try moving it all to a different script.

Share this post


Link to post
Share on other sites

Your formatting made me sad.

shotcount = 0; 
MY_KEYDOWN_FNC =
{ 
switch (_this) do
{
	case 22:
	{
		shotcount = shotcount + 1; 
		hintSilent format ["%1",shotcount]; 
		if (shotcount > 4) then 
		{
			hintSilent "reloading"; 
			sleep 5; 
			shotcount = 0; 
			hintSilent "ready";
		} else
		{
			nul = [] execVM "mousebomb.sqf";
		};
	};
};
};

Anyway, since you have no syntax errors in this code, it seems your error comes from the way you're trying to access the function, using call MY_KEYDOWN_FNC

"To execute a sleep function in the called code, execute it with spawn instead."

Instead execute this function like, "[] spawn MY_KEYDOWN_FNC;"

MattAka Horner explains in a note at the bottom of the "call" documentation: "A called function may only use suspension (sleep, uiSleep, waitUntil) if it originates in a scheduled environment. If the called function originates in a non-scheduled environment it will return a generic error. "

Share this post


Link to post
Share on other sites
Your formatting made me sad.

lol ;) Thanks for the information, Much appreciated!

---------- Post added at 20:46 ---------- Previous post was at 20:37 ----------

Instead execute this function like, "[] spawn MY_KEYDOWN_FNC;"

I was using this to call MY_KEYDOWN_FNC

waituntil {!isnull (finddisplay 46)}; (findDisplay 46) displayAddEventHandler ["KeyDown","_this select 1 call MY_KEYDOWN_FNC;false;"];

But I cannot just replace

call MY_KEYDOWN_FNC;

with

[] spawn MY_KEYDOWN_FNC;

it doesn't work, any idea how to do this properly?

Share this post


Link to post
Share on other sites

(findDisplay 46) displayAddEventHandler ["KeyDown","[_this select 1] spawn MY_KEYDOWN_FNC;false;"];

?? Maybe ??

And what is the false; at the end for?

Share this post


Link to post
Share on other sites

Returning false tells the engine to run the default keyboard event, true will override it

Share this post


Link to post
Share on other sites

Remember:

"A called function may only use suspension (sleep, uiSleep, waitUntil) if it originates in a scheduled environment. If the called function originates in a non-scheduled environment it will return a generic error."

Apparently, as long as the higher scope is in a scheduled enviornment, you can "call" a sleep statement and it will work out okay (Using the chart MattAka Horner made for reference). Maybe try:

[] spawn
{
waituntil {!isnull (finddisplay 46)};
(findDisplay 46) displayAddEventHandler ["KeyDown", {_this select 1 call MY_KEYDOWN_FNC;false;}];
};

@JShock

displayAddEventHandler can take code for it's second argument. I prefer to use squigglies ( { } not sure the real name) rather than quotes "".

EDIT: I forgot to mention, I changed your hints to hintSilent because for some reason playing the sound in hint has a significant effect on performance.

Edited by DreadedEntity

Share this post


Link to post
Share on other sites
Returning false tells the engine to run the default keyboard event, true will override it

Thanks Cuel, haven't used this eventhandler and key bindings before if you can't tell :p.

Share this post


Link to post
Share on other sites

Its the code that runs from the EH that is non-scheduled.

Either

waituntil {!isnull (finddisplay 46)};
(findDisplay 46) displayAddEventHandler ["KeyDown", "_this select 1 spawn MY_KEYDOWN_FNC; false;"];

MY_KEYDOWN_FNC = { 
switch (_this) do {
	case 22: {
		shotcount = shotcount + 1; 
		hintSilent format ["%1",shotcount]; 
		if (shotcount > 4) then {
			hintSilent "reloading"; 
			sleep 5; 
			shotcount = 0; 
			hintSilent "ready";
		}else{
			nul = [] execVM "mousebomb.sqf";
		};
	};
};
}; 

Spawn a new scheduled thread from the EH code

or

waituntil {!isnull (finddisplay 46)};
(findDisplay 46) displayAddEventHandler ["KeyDown", "_this select 1 call MY_KEYDOWN_FNC; false;"];

MY_KEYDOWN_FNC = { 
switch (_this) do {
	case 22: {
		shotcount = shotcount + 1; 
		hintSilent format ["%1",shotcount]; 
		if (shotcount > 4) then {
			_newThread = [] spawn {
				hintSilent "reloading"; 
				sleep 5; 
				shotcount = 0; 
				hintSilent "ready";
			};
		}else{
			nul = [] execVM "mousebomb.sqf";
		};
	};
};
};

Spawn a new scheduled thread from the case

Share this post


Link to post
Share on other sites

First one worked a treat, thanks for everyone's help :D

Its the code that runs from the EH that is non-scheduled.

Either

waituntil {!isnull (finddisplay 46)};
(findDisplay 46) displayAddEventHandler ["KeyDown", "_this select 1 spawn MY_KEYDOWN_FNC; false;"];

MY_KEYDOWN_FNC = { 
switch (_this) do {
	case 22: {
		shotcount = shotcount + 1; 
		hintSilent format ["%1",shotcount]; 
		if (shotcount > 4) then {
			hintSilent "reloading"; 
			sleep 5; 
			shotcount = 0; 
			hintSilent "ready";
		}else{
			nul = [] execVM "mousebomb.sqf";
		};
	};
};
}; 

Spawn a new scheduled thread from the EH code

or

waituntil {!isnull (finddisplay 46)};
(findDisplay 46) displayAddEventHandler ["KeyDown", "_this select 1 call MY_KEYDOWN_FNC; false;"];

MY_KEYDOWN_FNC = { 
switch (_this) do {
	case 22: {
		shotcount = shotcount + 1; 
		hintSilent format ["%1",shotcount]; 
		if (shotcount > 4) then {
			_newThread = [] spawn {
				hintSilent "reloading"; 
				sleep 5; 
				shotcount = 0; 
				hintSilent "ready";
			};
		}else{
			nul = [] execVM "mousebomb.sqf";
		};
	};
};
};

Spawn a new scheduled thread from the case

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×