npmproductions13 14 Posted October 16, 2017 Hi everyone i have an issue making a simple money script / function in my mission. I'm finding the syntax of SQF and SQS difficult to understand or comprehend in comparison to languages like java and C#. I used this sample below as a baseline but the guy who responded didn't tell the OP of that forum how edit the value of your "Bank" right now I've created a object of money with an add action that adds money to my Currency variable, but now i want to extend that to the death of 'X' unit and i came up with this. To some this code might be laughable but i don't understand why this doesn't work when executed via a units init. Any input is appreciated. _man = _this select 0; while {alive _this} do { if (!alive _this) then { //GoldCurrency = GoldCurrency + 1000; hint"DEAD"; }; }; Many Thanks -Irish Share this post Link to post Share on other sites
HazJ 1289 Posted October 16, 2017 Don't use SQS, it is obsolete. To define starting money (bank balance), do this in the init.sqf or somewhere else. GoldCurrency = 0; // assuming that is the name of the actual variable That while loop won't work in a player's init, not like that, unless you are using execVM, spawn or call to that file. Which you would also need to pass the player to. I don't think you need a while-loop for what you want. Something like this instead maybe? Player init: this setVariable ["money", 100, true]; // default balance: 100 Take Money addAction: this addAction ["Take Money", { _myMoney = (_this select 0) getVariable ["money", 100]; _victimMoney = (_this select 0) getVariable ["money", 100]; (_this select 1) setVariable ["money", (_myMoney + _victimMoney), true]; }]; NOT TESTED. Share this post Link to post Share on other sites
npmproductions13 14 Posted October 16, 2017 Thanks for your response but i don't think this is what i was looking for. I essentially wanted money added to the myMoney variable if you kill someone. Somewhat like a assassination mission that you get paid for. I look forward to your response. Share this post Link to post Share on other sites
npmproductions13 14 Posted October 16, 2017 I cant seem to be able to add code to my previous post but i tried this in an AI units init to no avail. if (!alive this) then { _myMoney = (_this select 0) getVariable ["money", 100]; _victimMoney = (_this select 0) getVariable ["money", 100]; (_this select 1) setVariable ["money", (_myMoney + _victimMoney), true]; hint format["New Currency %1",_myMoney]; }; Share this post Link to post Share on other sites
wozzsta 16 Posted October 16, 2017 I have a generic money system: make a initServer.sqf and put this in it: addMissionEventHandler ["EntityKilled", { params ["_killed", "_killer", "_killerID"]; if ((isPlayer _killer) && (alive _killer)) then { _killerID = owner _killer; [_killed] remoteExec ["fnc_addmoneyMP", _killerID, false]; }; }]; make a description.ext and put this in it: class CfgNotifications { class GotAward { title = "AWARD RECEIVED"; iconPicture = "\A3\Ui_f\data\GUI\Cfg\Ranks\general_gs.paa"; description = "<t color='#ffff33'>AWARD RECEIVED<br />%1</t>"; color[] = {0.3,0.6,0.3,1}; sound = ""; duration = 2; priority = 0; }; }; make a folder called functions and inside there a file called global.sqf and put this in it: fnc_addmoneyMP = { params ["_killed", "_award", "_myName"]; _award = 0; switch (true) do { case (_killed isKindOf "Man") : { _award = 1000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Static") : { _award = 2000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Car") : { _award = 3000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Ship") : { _award = 4000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "APC") : { _award = 5000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Tank") : { _award = 10000; _myName = profileName; player setVariable ["HG_Cash",0,false]; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Air") : { _award = 20000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; }; }; then in your .init.sqf add this: call compileFinal preprocessFileLineNumbers "functions\global.sqf"; This will give the play a notification when they kill something and add the corresponding amount of money to the variable HG_Cash Hope that helps. Share this post Link to post Share on other sites
Grumpy Old Man 3546 Posted October 16, 2017 33 minutes ago, npmproductions13 said: I cant seem to be able to add code to my previous post but i tried this in an AI units init to no avail. if (!alive this) then { _myMoney = (_this select 0) getVariable ["money", 100]; _victimMoney = (_this select 0) getVariable ["money", 100]; (_this select 1) setVariable ["money", (_myMoney + _victimMoney), true]; hint format["New Currency %1",_myMoney]; }; If you come from a java/c# background you should have no trouble getting into sqf. The only things that could throw you off might be logical issues, like your if statement above. Init fields get executed very early, before mission start and even before init.sqf. At the point of execution the object is still marked as alive, so your !alive _this check will return false. You could use eventHandlers which will get called upon certain events, have a read here. Also your snippet is not doing what you want it to do (probably an oversight by @HazJ), since you set the money of the action caller to 2x the money of the action holder. Try this: if (!alive this) then { params ["_killed","_killer","_actionID"];//declare variables for readability _killed removeAction _actionID;//get rid of the action so it can't be activated again _myMoney = _killer getVariable ["money", 100]; _victimMoney = _killed getVariable ["money", 100]; _killer setVariable ["money", (_myMoney + _victimMoney), true]; hint format["New Currency %1",_myMoney + _victimMoney]; }; Cheers 1 Share this post Link to post Share on other sites
npmproductions13 14 Posted October 16, 2017 Damn that was some great information there Grumpy. Id consider myself excellent with Java but somewhat ok with C#. Anyways I came up with this, can you tell me if it's wrong in some ways or all . Init.sqf GoldCurrency = 500;//Set the start amount of gold to 500 Targets Init Field this addEventHandler ["killed", {_this execVM "onDeath.sqf"}]; onDeath.sqf GoldCurrency = GoldCurrency + 500; hint format["New Currency %1",GoldCurrency]; This works so now when the target is killed my currency updates but just curious is there more efficient ways of doing this i'm open to your critisim Many thanks -Irish Share this post Link to post Share on other sites
wozzsta 16 Posted October 16, 2017 8 minutes ago, npmproductions13 said: Damn that was some great information there Grumpy. Id consider myself excellent with Java but somewhat ok with C#. Anyways I came up with this, can you tell me if it's wrong in some ways or all . Init.sqf GoldCurrency = 500;//Set the start amount of gold to 500 Targets Init Field this addEventHandler ["killed", {_this execVM "onDeath.sqf"}]; onDeath.sqf GoldCurrency = GoldCurrency + 500; hint format["New Currency %1",GoldCurrency]; This works so now when the target is killed my currency updates but just curious is there more efficient ways of doing this i'm open to your critisim Many thanks -Irish Rather than using the targets init, which means you have to add that code to every unit u want a reward for, i would add an event handler to you initServer.sqf addMissionEventHandler ["EntityKilled", { params ["_killed", "_killer", "_killerID"]; if ((isPlayer _killer) && (alive _killer)) then { GoldCurrency = GoldCurrency + 500; hint format[New Currency %1, GoldCurrency] }; }]; Share this post Link to post Share on other sites
npmproductions13 14 Posted October 16, 2017 8 minutes ago, wozzsta said: Rather than using the targets init, which means you have to add that code to every unit u want a reward for, i would add an event handler to you initServer.sqf addMissionEventHandler ["EntityKilled", { params ["_killed", "_killer", "_killerID"]; if ((isPlayer _killer) && (alive _killer)) then { GoldCurrency = GoldCurrency + 500; hint format[New Currency %1, GoldCurrency] }; }]; I get an error when i put that into my initServer.sqf. It says the _killed generic error in expression. Share this post Link to post Share on other sites
npmproductions13 14 Posted October 16, 2017 Never mind i found the error it was related to the hint in the if statement you sent works now but how do i limit the eventHandler to certain units. Share this post Link to post Share on other sites
wozzsta 16 Posted October 16, 2017 2 minutes ago, npmproductions13 said: Never mind i found the error it was related to the hint in the if statement you sent works now but how do i limit the eventHandler to certain units. hmmm you could do a nested if statement. if ((isPlayer _killer) && (alive _killer)) then { if(_killed isKindof "man") then{ GoldCurrency = GoldCurrency + 500; hint format[New Currency %1, GoldCurrency] }; }; }]; i havnt tested that but that should only give money if you kill a soldier, to limit it to specific units i'm not sure when i implemented this on my mission i wanted it to be a global system. Cheers. 1 Share this post Link to post Share on other sites
npmproductions13 14 Posted October 16, 2017 Ill try this later on, nice one mate appreciate the responses your a sound guy have a good day. Everyones help has been great keep it coming. Share this post Link to post Share on other sites
wozzsta 16 Posted October 16, 2017 no worries mate, i come from a C# background myself, it's a steep learning curving not having intelesense to rely on. 1 Share this post Link to post Share on other sites
npmproductions13 14 Posted October 16, 2017 So can someone help me further with limiting the event handler to desired units, for example i want an officer to give money but no general infantry to effect the balance variable. Share this post Link to post Share on other sites
wozzsta 16 Posted October 16, 2017 (edited) maybe try this? again not tested but it "should work" :) https://community.bistudio.com/wiki/rank if ((isPlayer _killer) && (alive _killer)) then { _rankformoney = "COLONEL" if(_killed isKindof "man") && (_rankformoney = rank _killed)then{ GoldCurrency = GoldCurrency + 500; hint format[New Currency %1, GoldCurrency]; }; }; }]; Cheers. Edited October 16, 2017 by wozzsta logic wrong way around Share this post Link to post Share on other sites
npmproductions13 14 Posted October 16, 2017 I get errors with the above code Should the end of that have a semi colon ? 36 minutes ago, wozzsta said: _rankformoney = "COLONEL" Quote hint format[New Currency %1, GoldCurrency]; Should this be hint format["New Currecny %1",GoldCurrency]; Quote if(_killed isKindof "man") && (_rankformoney = rank _killed)then{ Should this be ? if((_killed isKindof "man") && (_rankformoney = rank _killed))then{}; Share this post Link to post Share on other sites
wozzsta 16 Posted October 16, 2017 14 minutes ago, npmproductions13 said: I get errors with the above code Should the end of that have a semi colon ? Should this be hint format["New Currecny %1",GoldCurrency]; Should this be ? if((_killed isKindof "man") && (_rankformoney = rank _killed))then{}; Yeah all that looks about right, give it a go and have a play around, like i said without an IDE to code in it's easy to get lost :) Share this post Link to post Share on other sites
npmproductions13 14 Posted October 16, 2017 Yea i appreciate the patience mate still trying to grasp it to be honest. The code works if i comment out the latest stuff like rank so the issue lies there ill try fix it then again thanks mate. Share this post Link to post Share on other sites
IGD Big Steve 0 Posted February 10, 2019 Old thread, but I have implemented a system similar to this, but am unable to stop the variable value from going below 0.I am doing everything through addaction commands in the editor, nothing fancy. Say I have 500$ and a gun costs 1000$, I can purchase it and my cash will be -500$. Need something like: if pointsPlayer >= *cost* then give weapon; remove cash; else dont Share this post Link to post Share on other sites
Grumpy Old Man 3546 Posted February 10, 2019 6 hours ago, IGD Big Steve said: Old thread, but I have implemented a system similar to this, but am unable to stop the variable value from going below 0.I am doing everything through addaction commands in the editor, nothing fancy. Say I have 500$ and a gun costs 1000$, I can purchase it and my cash will be -500$. Need something like: if pointsPlayer >= *cost* then give weapon; remove cash; else dont Might find this useful: Already checks if the transaction drops below 0 and if the player can actually carry the item, if any of those fails the transaction won't happen. Pretty basic and easy to build upon. Cheers Share this post Link to post Share on other sites
ethrendil 11 Posted November 2, 2020 On 10/16/2017 at 8:43 AM, wozzsta said: I have a generic money system: make a initServer.sqf and put this in it: addMissionEventHandler ["EntityKilled", { params ["_killed", "_killer", "_killerID"]; if ((isPlayer _killer) && (alive _killer)) then { _killerID = owner _killer; [_killed] remoteExec ["fnc_addmoneyMP", _killerID, false]; }; }]; make a description.ext and put this in it: class CfgNotifications { class GotAward { title = "AWARD RECEIVED"; iconPicture = "\A3\Ui_f\data\GUI\Cfg\Ranks\general_gs.paa"; description = "<t color='#ffff33'>AWARD RECEIVED<br />%1</t>"; color[] = {0.3,0.6,0.3,1}; sound = ""; duration = 2; priority = 0; }; }; make a folder called functions and inside there a file called global.sqf and put this in it: fnc_addmoneyMP = { params ["_killed", "_award", "_myName"]; _award = 0; switch (true) do { case (_killed isKindOf "Man") : { _award = 1000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Static") : { _award = 2000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Car") : { _award = 3000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Ship") : { _award = 4000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "APC") : { _award = 5000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Tank") : { _award = 10000; _myName = profileName; player setVariable ["HG_Cash",0,false]; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; case (_killed isKindOf "Air") : { _award = 20000; _myName = profileName; player setVariable ["HG_Cash",(player getVariable "HG_Cash") + _award, false]; ["GotAward",[_award]] call BIS_fnc_showNotification; titleText [format[(localize "STR_HG_DLG_MC"),[player getvariable 'HG_Cash'] call BIS_fnc_numberText],"PLAIN DOWN",1]; }; }; }; then in your .init.sqf add this: call compileFinal preprocessFileLineNumbers "functions\global.sqf"; This will give the play a notification when they kill something and add the corresponding amount of money to the variable HG_Cash Hope that helps. I know this is an old thread but I was hoping someone could help me with wozzsta's script. I'm using it to supplement a Warlords mission where it adds bonus money for kills, the problem is it awards the bonus regardless if you kill teammates or enemies. I'm terrible at understanding scripting, but that hasn't stopped me from messing around with it for the past 6 years. I've tried adding checks under the cases like "&& _killed == !playerside" but like I said I don't really know what I'm doing. Just thought I'd see if anyone could help me out. Ideally it would be a script that could cater to the player being Blufor or Opfor so simply saying "side does not equal Blufor" wouldn't work. Share this post Link to post Share on other sites
ethrendil 11 Posted November 2, 2020 I think I figured it out. I added "&& _sideOfKilled != playerSide" and defined the variable _sideOfKilled as _sideOfKilled = [_this select 0, true] call BIS_fnc_objectSide; It seems to work so far, not sure if it works in multiplayer though. Share this post Link to post Share on other sites
wogz187 1086 Posted November 3, 2020 (edited) @ethrendil, Check this out,you_accountDemo (drive) It's simple, Spoiler addMissionEventHandler ["EntityKilled", { params ["_unit", "_killer"]; if (!((faction _unit) == (faction _killer)) && !((faction _unit) == "CIV_f")) exitWith { private _val=_unit getVariable ["you_account", 0]; [_killer, _val] call you_fnc_account }; }]; any time a unit is killed their account or value is paid to the killer. This function handles balances, you_fnc_account = { params [["_caller", objNull, [objNull]], ["_amount", 0, [0]]]; private _account = _caller getvariable ["you_account", 0]; _caller setvariable ["you_account", _account+ _amount]; if (_amount < 0) then { systemChat format ["You spend $%1", _amount] } else { if (_amount > 0) then { systemChat format ["You earn $%1", _amount] }; }; systemChat format ["Balance $%1", _account + _amount]; }; Load the above anywhere script will stick and be sure to assign the values directly to your units/vehicles. I'd suggest something like, { _x setVariable ["you_account", 100] } forEach allUnits; or similar depending on the exact scenario. Or even more simple, Spoiler you_fnc_account= { params [["_caller", player, [objNull]], ["_amount", 0, [0]]]; private _account = _caller getvariable ["you_account", 0]; _caller setvariable ["you_account", _account+ _amount]; if (_caller== player) exitWith {format ["Balance $%1", _account +_amount] remoteExec ["hint", 0]}; }; because systemChats are ugly... Have fun! Edited November 3, 2020 by wogz187 updated 3 Share this post Link to post Share on other sites