Jump to content

Recommended Posts

After gathering supplies (5 or more) I would like to have the player have an addaction.

It doesnt seem to work no matter what I do, it either doesnt work at all or spawns 100 addactions (keeps adding them)

Here is the code, hopefully someone can help:

 

init.sqf:

// By JShock case for EpicGoldenWarrior
//This is an alteration of my load effect snippet (in my signature)
JSHK_fnc_Loading =
{
    private ["_one", "_two", "_three", "_iter", "_final"];
 
    _one = composeText [parseText "<t color='#ffffff' size='1' align='center' >Searching body.</t>"];
    _two = composeText [parseText "<t color='#ffffff' size='1' align='center' >Searching body..</t>"];
    _three = composeText [parseText "<t color='#ffffff' size='1' align='center' >Searching body...</t>"];
    _final = composeText [parseText "<t color='#6cdd23' size='1' align='center' >Search Complete</t>"];
 
    _iter = 0;
    
    if !(player getVariable ["JSHK_use_LoadHint",false]) exitWith 
    {
        hintSilent _final; 
        sleep 5; 
        hintSilent "";
    };
 
    while {player getVariable ["JSHK_use_LoadHint",false]} do 
    {
        switch (_iter) do 
        {
            case 0: { hintSilent _one; };
            case 1: { hintSilent _two; };
            case 2: { hintSilent _three; };
            default { systemChat "Failed hint >> JSHK_fnc_Loading"; };
        };
 
        if !(player getVariable ["JSHK_use_LoadHint",false]) exitWith {};
 
        if (_iter >= 2) then {_iter = 0;} else {_iter = _iter + 1;};
 
        sleep 1;
 
    };
    
    hintSilent _final; 
    sleep 5; 
    hintSilent "";
};
 
//locally global variable to the player
supplies = 0;
 
//hint
_monitorStats = [] spawn {
   while {true} do {
        hintSilent format["Supplies: %1", supplies];};};
        sleep 10; //there is a small break in this loop to reduce the cost of this thread on the CPU.    // Also, the 
 
player doesn't need 100% accuracy anyway.
//THIS IS WHERE STUFF GOES WRONG, IM SURE. Generally this area at least.
_buildstatus = [] spawn {
while {true} do {       
if (supplies >= 5) then {
player addAction [format ["Build", localize "STR_FS_OpenBuilder"], {createDialog "BuildMenu";}, [], 8, 
 
false, true, "", ""];};};

Share this post


Link to post
Share on other sites

Use the condition parameter of addAction, no need for a loop to manage some addAction can do inherently:

player addAction ["My Action",{/*code*/},nil,6,true,true,"","supplies >= 5"];
I also recommend the use of get/set variable over a global variable instantiated in the init.sqf.

Share this post


Link to post
Share on other sites

The formatting is really bad so let's fix that first:

_monitorStats = [] spawn
{
	while {true} do
	{
		hintSilent format["Supplies: %1", supplies];
	};
		
};
sleep 10; //there is a small break in this loop to reduce the cost of this thread on the CPU.

// Also, the player doesn't need 100% accuracy anyway.
//THIS IS WHERE STUFF GOES WRONG, IM SURE. Generally this area at least.
_buildstatus = [] spawn
{
	while {true} do
	{
		if (supplies >= 5) then
		{
			player addAction [format ["Build", localize "STR_FS_OpenBuilder"], {createDialog "BuildMenu";}, [], 8, 
			false, true, "", ""];
		};
	};

You have several problems.

 

The first and most important is that if you actually do reach 5 supplies, that while loop just keep adding the action over and over, infinitely. The best thing to do would be to change the while loop condition to (supplies < 5) so it will terminate. You can also save the return value of spawn into a global variable, then use that to terminate with scriptDone. I don't see any place where you change the value of supplies so I'm just assuming that's done elsewhere.

 

The sleep command you have for the first monitoring loop is not inside the loop. Did you try waiting for 10 seconds to see if the next thread would spawn?

It won't anyway because you did not close the spawn. There is a closing bracket missing

 

You have used format incorrectly. Even though you localized the string, it will not be added to "Build"

 

Also the very last parameter of addAction is actually a condition check, so that whole thing can be simplified to just one use of the command:

player addAction [format ["Build", localize "STR_FS_OpenBuilder"],
{
	createDialog "BuildMenu";
}, [], 8, false, true, "", "supplies >= 5"];

Share this post


Link to post
Share on other sites

Awesome guys thanks, will give these a shot.

This mission is ridiculously old and I never published it, but I feel like it has a future, so I went back to it.

Kind of rusty on the code and what not :S

 

P.S. Hey Jshock :P

 

EDIT: This is also in initplayerlocal, I don't know if that changes anything...should be fine, I have it there for a reason, right? Lol, forgot xD

Share this post


Link to post
Share on other sites

Well, the action is "removed" whenever the player has less than 5 supplies, or at least, conditionally, it doesn't show up, but once you get 5 or more supplies again, it shows again.

Or are you saying once they use the action, you want to remove it, so they don't use it again?

Share this post


Link to post
Share on other sites

No, you're right.

 

I just have a lot of code to work with and I don't know where to put my [supplies = supplies - 5;] 

 

I'd put down the mission folder but there is A LOT of stuff in there. Most of the mission runs script based, which is why its out of my comfort zone.

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

×