Enable a self action with a variable? Looking for help.

PezDax

Member
Hi

I have a simple trading script I'm working on. Players trade items for "credits" which are not in-game items, but a value held in a global variable.

The "sell" section works fine. Players sell items and the playerCredits global variable increments fine. The credit total is displayed ...

titleText [format["Hello. You have this many credits to spend: %1",(playerCredits)], "PLAIN DOWN"]; titleFadeOut 15;

so I know the variable playerCredits is working.

The trouble I have is when I try to use the variable to enable the "sell" self actions ...

if (playerCredits >= 5) then {
hasB1credits = true;
} else {
hasB1credits = false;
};
if (hasB1credits) then {
if (buy1 < 0) then {
buy1 = player addAction [("<t color=""#00c362"">" + ("Buy Scrap Metal for 5 Credits") +"</t>"),"Market\Buy\buy1.sqf","",5,false,true,"",""];
};
} else {
player removeAction buy1;
buy1 = -1;
};

In this example, I know that the value of playerCredits is greater than 5, but the self action never comes up as an option.

Can anyone tell me why the "if (playerCredits >= 5) then {" condition doesn't work?

Thank You
 
Where is this code located? I mean how is this code called in the first place?

also, sqf can be a bitch run a little cuttext debug for yourself, something along the lines of:
cuttext [format["AddAction returns: %1",buy1],"PLAIN"];
will suffice, put it right after you call the addAction and see what buy1 returns, if the value is "any" or less than zero then your addaction failed, if the cuttext never shows up then that also says alot?

also where are you setting buy1 initially to be less than zero? in variables.sqf? is it public?

You have to initialize the var buy1 to be less than ZERO to begin with or the IF block will fail. you'll also see this easily if that cuttext debug line i gave you never shows up, that will also tell you alot.

Cheers.
 
Thanks for the response.

I'm using virtually the same code for my "sell" section which works just fine.

For example ...

if ("ItemJerrycan" in magazines player) then {
hasS2parts = true;
} else {
hasS2parts = false;
};
if (hasS2parts) then {
if (sell2 < 0) then {
sell2 = player addAction [("<t color=""#00c362"">" + ("Trade Jerry Can for 7 Credits") +"</t>"),"Market\Sell\sell2.sqf","",5,false,true,"",""];
};
} else {
player removeAction sell2;
sell2 = -1;

This works perfectly, so the only difference is, I'm looking for an item in the player's inventory to activate this sell action, while I'm using the value of a global variable (playerCredits) to activate the buy action.

As I say, the value for playerCredits is displaying fine and is accurate. That's why I'm so confused as to why I can't activate the buy action by simply checking its value.

Oh, and this part of the buy and sell code is in the self actions sqf
 
Thanks AlienX.

When I saw your post I thought that might be my problem.

The global variable playerCredits is initially defined as a numeric variable, but I thought that using it in the line ...

titleText [format["Hello. You have this many credits to spend: %1",(playerCredits)], "PLAIN DOWN"]; titleFadeOut 15;

... might have changed it to a string somehow.

In the wiki example, it shows a string in quotation marks to be converted, not a variable.

_number = parseNumber "0.125"

So I tried ...

playerCredits = parseNumber playerCredits;
playerCredits = parseNumber (playerCredits);
numCredits = parseNumber playerCredits;
numCredits = parseNumber (playerCredits);

The tries without playerCredits in brackets faulted out all player self actions.

The tries with the brackets didn't cause any faults, but didn't solve my problem. The buy option still doesn't come up.

Perhaps I'm formatting the parseNumber code incorrectly, or maybe the parseNumber command doesn't work with variables ... or perhaps that's not my problem.

Hoping to figure it out ... eventually ;)
 
Darn it haha.

Could we please have a copy (perhaps a pastebin) of the whole file, perhaps there is an error elsewhere that is causing it to not work correctly.
 
Okay :) Here's how it works ...

In my init.sqf, I initialize the global variable playerCredits ...

playerCredits = 0; // Credits Initialize

I have a Buy and a Sell area on my map. In my mission.sqm I have two "Domes" defined that will detect if a player enters or exits either area.

When a player enters the Buy area, the dome runs this ...

expActiv="dome3 = [] execVM ""Market\Buy\buy.sqf"";";

This runs the buy.sqf script ...

titleText [format["Hello. You have this many credits to spend: %1",(playerCredits)], "PLAIN DOWN"]; titleFadeOut 15;
canBuy = true;

This works and when the player enters the area, the amount of credits they have (playerCredits) is displayed properly.
All the self actions for Buying in fn_selfAction.sqf are inside a loop ...

if (canBuy) then {
self actions are here
};

So the Buy self actions only come up when the player is in the Buy area (dome).

All that's left to do is test to enable possible Buy actions based on the number of credits the player has (playerCredits).

As I show in my first post, I'm trying to do it by testing the value of playerCredits.

if (playerCredits >= 5) then {
self action here
};

I'm using exactly the same method for the Sell area and it works great. So only difference is that Sell looks at inventory ...

if ("ItemJerrycan" in magazines player) then {

and the Buy area looks at the value of playerCredits ...

if (playerCredits >= 5) then {


Not sure if that explanation helps. Still looking for a solution. I should have time to try a couple more things this week-end. :)
 
Well ... I finally got it to work :)

Turns out if I assign the value of playerCredits to another variable in the self actions script, and work with the new variable instead of playerCredits, it works like a charm.

I guess this is like re-initializing or refreshing the variable value ... or something.

Thanks for the suggestions.
 
Back
Top