Jump to content

Release/Tutorial - Vending Machine


Recommended Posts

Hi all! Thought I would share a simple little script I put together. I am by no means a professional and this probably could be cleaner and better written but it works and is a cool little addition. This script is also a simple example of what can be accomplished with your server. Start with small scripting projects and have fun learning. So here it is. I studied the code from a few others and threw this together..

 

Add this code to your fn_selfactions.sqf at the very bottom...

//------------------------------------------------ Vending Machine --------------------------------//
private["_playerPos","_nearVend"];

_playerPos = getPosATL player;
_nearVend = count nearestObjects [_playerPos, ["MAP_vending_machine"], 4] > 0;
 
if (_nearVend) then {
        if (s_player_buySoda < 0) then {
            s_player_buySoda = player addaction[("<t color=""#00C732"">" + ("Buy Soda") +"</t>"),"Vending\venddrink.sqf"];
        };
    } else {
        player removeAction s_player_buySoda;
        s_player_buySoda = -1;
    };
 
//--------------------------------------------- Vending Machine End ---------------------------//

Make a folder in your root of your mission file called "Vending" and make a empty file named "venddrink.sqf", then copy and paste this...

//by ZeroK00L
private["_playerPos","_canVend"];

call gear_ui_init;
_playerPos = getPosATL player;
_canVend = count nearestObjects [_playerPos, ["MAP_vending_machine"], 4] > 0;
_soda = ["ItemSodaRabbit","ItemSodaOrangeSherbet","ItemSodaLvg","ItemSodaClays","ItemSodaSmasht","ItemSodaPepsi","ItemSodaMdew","ItemSodaCoke","ItemSodaLemonade"];

if (_canVend) then {
			
			playsound "vendingmachine";
			sleep 2;
			player playActionNow "PutDown";
			player addMagazine 'ItemSodaRabbit';
			cutText ["You received a Soda from the Vending Machine!", "PLAIN DOWN"];
};

OPTIONAL IF YOU WANT THE SOUND!

 

Open up description.ext and add this to your sounds class. If you dont already have one you can make one..

class CfgSounds
{
    sounds[] ={vendingmachine};
 
class vendingmachine
    {
    name="vendingmachine";
    sound[]={\Vending\vendingmachine.ogg,0.9,1};
    titles[] = {};
    };
	
};

Then add the attached sound file to your "Vending" folder. Voila!

 

If you notice, in the venddrink.sqf I put an array for all the sodas & beer the game can use.

_soda = ["ItemSodaRabbit","ItemSodaOrangeSherbet","ItemSodaLvg","ItemSodaClays","ItemSodaSmasht","ItemSodaPepsi","ItemSodaMdew","ItemSodaCoke","ItemSodaLemonade"];

Maybe if I have time I will create a random chance for different sodas to come out. Also I might make it so it takes money from the player. If anyone wants to tackle those items and share please do. Have fun with it :) This script can even be modified and used on the Refrigerators to give the player canned foods.

 

P.S You will need to place a vending machine somewhere on the map. Classname is "MAP_vending_machine". I have tried to use Raymix's Emerald tools but the script doesn't recognize the vending machine placed by that tool. Would be cool if it did...so use the editor for now. :)

 

Zero

Link to comment
Share on other sites

P.S You will need to place a vending machine somewhere on the map. Classname is "MAP_vending_machine". I have tried to use Raymix's Emerald tools but the script doesn't recognize the vending machine placed by that tool. Would be cool if it did...so use the editor for now. :)

 

Zero

 

So you can only get soda from the vending machines that are placed and not the ones built into the map?

Link to comment
Share on other sites

Great idea and I have this on my sever already, but I use the isKindof command in fn_selfactions.sqf for the vending action. Try this as a vend.sqf:

private ["_hasCash"];

_hasCash = "ItemTinBar" in magazines player;

if (!_hasCash) exitWith {cutText [format["You have no TinBars"], "PLAIN DOWN"];};

if (_hasCash) then
    {
    player playActionNow "PutDown";
    sleep 3;
    player removeMagazine "ItemTinBar";
    player addMagazine "ItemSodaOrangeSherbet";
    sleep 0.01;
    titleText ["Soda dispensed, you drop it into your pocket", "PLAIN DOWN"];titleFadeOut 5;
};

This will take 1 tin bar to get a can of orange sherbet.

Link to comment
Share on other sites

Here's an updated one for random drink. Haven't tested but pretty sure it will work. Added it to the one requiring tin bar.

 

private ["_hasCash","_random"];

_hasCash = "ItemTinBar" in magazines player;
_random = round(random(8));

if (!_hasCash) exitWith {cutText [format["You have no TinBars"], "PLAIN DOWN"];};

if (_hasCash) then
    {
    player playActionNow "PutDown";
    sleep 3;
    player removeMagazine "ItemTinBar";
    switch (_random) do {    
        case 0 :{
            player addMagazine "ItemSodaOrangeSherbet";
        };
        case 1 :{
            player addMagazine "ItemSodaLvg";
        };
        case 2 :{
            player addMagazine "ItemSodaClays";
        };
        case 3 :{
            player addMagazine "ItemSodaSmasht";
        };
        case 4 :{
            player addMagazine "ItemSodaPepsi";
        };
        case 5 :{
            player addMagazine "ItemSodaMdew";
        };
        case 6 :{
            player addMagazine "ItemSodaCoke";
        };
        case 7 :{
            player addMagazine "ItemSodaLemonade";
        };
        case 8 :{
            player addMagazine "ItemSodaRabbit";
        };
    };
    sleep 0.01;
    titleText ["Soda dispensed, you drop it into your pocket", "PLAIN DOWN"];titleFadeOut 5;
};
Link to comment
Share on other sites

 

Best way would be BIS_fnc_selectRandom.

_soda = ["ItemSodaRabbit","ItemSodaOrangeSherbet","ItemSodaLvg","ItemSodaClays","ItemSodaSmasht","ItemSodaPepsi","ItemSodaMdew","ItemSodaCoke","ItemSodaLemonade"] call BIS_fnc_selectRandom;
player addMagazine _soda;

Thanks for that, didn't know it could be simplified so easy. Will come in handy for other work.

Link to comment
Share on other sites

make a scroll menu and let the player choose himself, what drink he wants not to mention set price individually ... afaik some of these sodas cost a lot more than a tinbar at a trader

 

Add a risk to malfunction the vending machine to offset the price, 50/50 chance. And increase the base price.

Link to comment
Share on other sites

Great idea and I have this on my sever already, but I use the isKindof command in fn_selfactions.sqf for the vending action. Try this as a vend.sqf:

private ["_hasCash"];

_hasCash = "ItemTinBar" in magazines player;

if (!_hasCash) exitWith {cutText [format["You have no TinBars"], "PLAIN DOWN"];};

if (_hasCash) then
    {
    player playActionNow "PutDown";
    sleep 3;
    player removeMagazine "ItemTinBar";
    player addMagazine "ItemSodaOrangeSherbet";
    sleep 0.01;
    titleText ["Soda dispensed, you drop it into your pocket", "PLAIN DOWN"];titleFadeOut 5;
};

This will take 1 tin bar to get a can of orange sherbet.

 

 

 

Here's an updated one for random drink. Haven't tested but pretty sure it will work. Added it to the one requiring tin bar.

private ["_hasCash","_random"];

_hasCash = "ItemTinBar" in magazines player;
_random = round(random(8));

if (!_hasCash) exitWith {cutText [format["You have no TinBars"], "PLAIN DOWN"];};

if (_hasCash) then
    {
    player playActionNow "PutDown";
    sleep 3;
    player removeMagazine "ItemTinBar";
    switch (_random) do {    
        case 0 :{
            player addMagazine "ItemSodaOrangeSherbet";
        };
        case 1 :{
            player addMagazine "ItemSodaLvg";
        };
        case 2 :{
            player addMagazine "ItemSodaClays";
        };
        case 3 :{
            player addMagazine "ItemSodaSmasht";
        };
        case 4 :{
            player addMagazine "ItemSodaPepsi";
        };
        case 5 :{
            player addMagazine "ItemSodaMdew";
        };
        case 6 :{
            player addMagazine "ItemSodaCoke";
        };
        case 7 :{
            player addMagazine "ItemSodaLemonade";
        };
        case 8 :{
            player addMagazine "ItemSodaRabbit";
        };
    };
    sleep 0.01;
    titleText ["Soda dispensed, you drop it into your pocket", "PLAIN DOWN"];titleFadeOut 5;
};

 

Nice! Thanks for contributing!

 

Zero

Link to comment
Share on other sites

make a scroll menu and let the player choose himself, what drink he wants not to mention set price individually ... afaik some of these sodas cost a lot more than a tinbar at a trader

 

Feel free too make it however you like! :) Could also just make a trader out of the vending machine instead of scripting.

 

Zero

Link to comment
Share on other sites

  • 3 months later...

I have a rare food and drink vendor on my server, so I prefer not to have all those rare drinks available though this method, so I did this line:

_soda = [["ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaOrangeSherbet"] call BIS_fnc_selectRandom;

That gives a 50% chance for Coke, 40% chance for Pepsi and a 10% chance for OrangeSherbert. This is untested, but it should wor

 

I also changed the cost to 50oz of silver:

_hasCash = "ItemSilverBar50oz" in magazines player;
Link to comment
Share on other sites

Ok, so I put a vending machine in. I get the Buy Soda menu option, but when I select it, nothing happens. Here's the venddrink.sqf I'm using:

private ["_hasCash","_soda"];


_hasCash = "ItemSilverBar10oz" in magazines player;
_soda = [["ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaOrangeSherbet"] call BIS_fnc_selectRandom;


if (!_hasCash) exitWith {cutText [format["You need a 10oz silver bar to buy soda"], "PLAIN DOWN"];};


if (_hasCash) then
    {
    player playActionNow "PutDown";
    sleep 3;
    player removeMagazine "ItemSilverBar10oz";
player addMagazine _soda;
    sleep 0.01;
    titleText ["Soda has been added to your inventory", "PLAIN DOWN"];titleFadeOut 5;
};
Link to comment
Share on other sites

NO

_soda = [["ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaOrangeSherbet"] call BIS_fnc_selectRandom;

YES :)

_soda = ["ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaCoke","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaPepsi","ItemSodaOrangeSherbet"] call BIS_fnc_selectRandom;
Link to comment
Share on other sites

Modified this a bit ;)

Only for SingleCurrency

 

SelfActions:

private["_playerPos","_nearVend"];

_playerPos = getPosATL player;
_nearVend = count nearestObjects [_playerPos, ["MAP_vending_machine"], 4] > 0;
 
if (_nearVend) then {
        if (s_player_buySoda < 0) then {
            s_player_buySoda = player addaction[("<t color=""#00C732"">" + ("Buy Drinks") +"</t>"),"Vending\vending_drinks.sqf"];
        };
		if (s_player_buyFood < 0) then {
			s_player_buyFood = player addaction[("<t color=""#00C732"">" + ("Buy Food") +"</t>"),"Vending\vending_foods.sqf"];
        };
    } else {
        player removeAction s_player_buySoda;
        s_player_buySoda = -1;
		player removeAction s_player_buyFood;
        s_player_buyFood = -1;
    };

Vending\vending_foods.sqf:

private["_playerPos","_canVend","_costs"];

call gear_ui_init;
_playerPos = getPosATL player;
_canVend = count nearestObjects [_playerPos, ["MAP_vending_machine"], 4] > 0;
_food = ["FoodNutmix","FoodPistachio","FoodSteakCooked","FoodMRE"] call BIS_fnc_selectRandom;
_costs = 5;

if (_canVend) then {
	if !([ player,_costs] call SC_fnc_removeCoins) then {
		titleText [format["You need %1 %2 to buy food.",_costs,CurrencyName] , "PLAIN DOWN", 1];
	} else {
		sleep 1;
		player playActionNow "PutDown";
		player addMagazine _food;
		titleText [format["Bought a %1 for %2 %3",_food,_costs,CurrencyName] , "PLAIN DOWN", 1];
	};
};

Vending\vending_drinks.sqf:

private["_playerPos","_canVend","_costs"];

call gear_ui_init;
_playerPos = getPosATL player;
_canVend = count nearestObjects [_playerPos, ["MAP_vending_machine"], 4] > 0;
_soda = ["ItemSodaOrangeSherbet","ItemSodaPepsi","ItemSodaMdew","ItemSodaCoke","ItemSodaLemonade"] call BIS_fnc_selectRandom;
_costs = 2;

if (_canVend) then {
	if !([ player,_costs] call SC_fnc_removeCoins) then {
		titleText [format["You need %1 %2 to buy a soda.",_costs,CurrencyName] , "PLAIN DOWN", 1];
	} else {
		sleep 1;
		player playActionNow "PutDown";
		player addMagazine _soda;
		titleText [format["Bought %1 for %2 %3",_soda,_costs,CurrencyName] , "PLAIN DOWN", 1];
	};
};

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...