Jump to content
  • 0

Global variables


ViktorReznov

Question

So i have quite the conundrum, this is in relation to the script tweak i put out, and the question is:

What all can I do with globals? Are they static once they have been set? Can they be altered after server start? Can I modify a global variable anytime I want during server uptime?

I have defined three variables (global) in a total of 7 files in order to get my code to function. Ill go ahead and break it down here so that I can get an accurate answer and possibly figure this out. The last gentlemen to help me with this helped me blindly without fully understanding what I modified so I never discoverd if it is possible to do what Id like to. (i appreciate all of the good sir's help and hopefully this post here will get the answers needed!)

The three variables are DZE_ecoMult, DZE_buyMult and DZE_sellMult. I initialize the ecoMult in init.sqf with = (round(random 12)) to give a value between 0 and 11. This value then uses a switch case to modify the buy/sellMults which are then coded to modify the prices of the traders. What Id like to do, and havent been able to succeed in doing, is modify the buy/sellMults by modifying the ecoMult say every hour of server up time. I however, couldnt get it to do this. Would I have to de-globalize my globals in order to do this? OR can i keep them global and modify them? I do not quite understand the nature of the global....

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0
1 hour ago, ViktorReznov said:

So i have quite the conundrum, this is in relation to the script tweak i put out, and the question is:

What all can I do with globals? Are they static once they have been set? Can they be altered after server start? Can I modify a global variable anytime I want during server uptime?

I have defined three variables (global) in a total of 7 files in order to get my code to function. Ill go ahead and break it down here so that I can get an accurate answer and possibly figure this out. The last gentlemen to help me with this helped me blindly without fully understanding what I modified so I never discoverd if it is possible to do what Id like to. (i appreciate all of the good sir's help and hopefully this post here will get the answers needed!)

The three variables are DZE_ecoMult, DZE_buyMult and DZE_sellMult. I initialize the ecoMult in init.sqf with = (round(random 12)) to give a value between 0 and 11. This value then uses a switch case to modify the buy/sellMults which are then coded to modify the prices of the traders. What Id like to do, and havent been able to succeed in doing, is modify the buy/sellMults by modifying the ecoMult say every hour of server up time. I however, couldnt get it to do this. Would I have to de-globalize my globals in order to do this? OR can i keep them global and modify them? I do not quite understand the nature of the global....

global variables are global variables, they can be overwritten by any script at any time.

for instance:

in init.sqf:

DZE_ecoMult = round(random 12);

Any script can now use that variable to do whatever it likes, i.e:

if (DZE_ecoMult >= 5 && ((count playableUnits) >= 20)) then {DZE_echoMult = 2}; // Reduce the economy multiplier when > 20 players are online

If you need to run that loop every hour, you would need to do a while loop inside a script, for an example of this, see this loyalty reward script I helped @Pattohwrite: 

It contains a script that runs in the background and has a sleep in it, easily modifiable to do what you want to do.

Also, discord is a really good way to talk about things like this, I am pretty much always available on there as are a few of the other coders.

https://discord.gg/AVSQbN6

Link to comment
Share on other sites

  • 0
42 minutes ago, seelenapparat said:

from what he wrote, I suspect he needs to use the public variable instead? as a global variable per se is not synchronized between clients/ the server.

Correct, if he wants to synchronize the variable across all clients on the server instead of the variable being changed based on the time that the client has been in the server, he would need to do a loop server side that sends a public variable to all the clients. However, if he is merely wanting to change the price on a single client say for being logged in for a certain amount of time, then client side would be fine.

Link to comment
Share on other sites

  • 0

Another way would be simply running a loop on the client (better than running one on the server) that checks serverTime (like most debug monitors).

 

Example:

waitUntil {uiSleep 300; floor(serverTime/60/60) == 1};

/*
	RUN THIS CODE
*/

 

Would wait until an hour into the server restart and then run the code which could be changing your global variable for example.

Link to comment
Share on other sites

  • 0

So to clarify a little further on my issue. I would set the variable in my init.sqf 

if (isNil "DZE_ecoMult") then {
	DZE_ecoMult = (round(random 12));
};

This way, it would only run once ( a work around I was using to stop the desync between client and server).

Then in my if (isServer) block

_adv_economyFluc = [] execVM "scripts\adv_economyFluc.sqf";

which would then call this script

Spoiler

fn_fluctuating = {  
    DZE_ecoMult = (round(random 12));
    switch (DZE_ecoMult) do {
        case 0:        {DZE_buyMult = 2;    DZE_sellMult = 0.5;};
        case 1:        {DZE_buyMult = 1.9;    DZE_sellMult = 0.6;};
        case 2:        {DZE_buyMult = 1.8;    DZE_sellMult = 0.7;};
        case 3:        {DZE_buyMult = 1.6;    DZE_sellMult = 0.8;};
        case 4:        {DZE_buyMult = 1.5;    DZE_sellMult = 0.9;};
        case 5:        {DZE_buyMult = 1.4;    DZE_sellMult = 1.1;};
        case 6:        {DZE_buyMult = 1.2;    DZE_sellMult = 1.2;};
        case 7:        {DZE_buyMult = 0.9;    DZE_sellMult = 1.3;};
        case 8:        {DZE_buyMult = 0.8;    DZE_sellMult = 1.4;};
        case 9:        {DZE_buyMult = 0.7;    DZE_sellMult = 1.5;};
        case 10:    {DZE_buyMult = 2.5;    DZE_sellMult = 1;}; //wildcard! bad day to be buying....
        case 11:    {DZE_buyMult = 0.7;    DZE_sellMult = 2;}; //wildcard! MO MONEY!
    };
    [nil,nil,rTitleText,"Prices have changed! Check out the sweet deals NOW!", "PLAIN",10] call RE;
};
   
   waituntil {(round(time)) > 300}; 
   call fn_fluctuating;
   
   waituntil {(round(time)) > 400}; 
   call fn_fluctuating;
   
   waituntil {(round(time)) > 500}; 
   call fn_fluctuating;
   
   waituntil {(round(time)) > 600}; 
   call fn_fluctuating;

however, my diag logs would show the change to the mults (diag_log Format["[ECONOMY] You Buy %1 Percent, You Sell %2 Percent",(DZE_buyMult * 100), (DZE_sellMult * 100)];) but the prices on my traders would not change.

If more information is needed, please let me know, ill post the changes I made if needed (and when I have time, im a soldier and currently on break)

Thanks for the input epochFamily!

Will take a quick moment:

Changes: fn_selfActions redirect advanced trading call to new advancedtrading_init.sqf

modified advancedtrading\init.sqf with

if (isNil "DZE_ecoMult") then {
    DZE_buyMult = 1; 
    DZE_sellMult = 1;
};

switch (DZE_ecoMult) do {
    case 0:    {DZE_buyMult = 2;    DZE_sellMult = 0.5;};
    case 1:    {DZE_buyMult = 1.9;    DZE_sellMult = 0.6;};
    case 2:    {DZE_buyMult = 1.8;    DZE_sellMult = 0.7;};
    case 3:    {DZE_buyMult = 1.6;    DZE_sellMult = 0.8;};
    case 4:    {DZE_buyMult = 1.5;    DZE_sellMult = 0.9;};
    case 5:    {DZE_buyMult = 1.4;    DZE_sellMult = 1.1;};
    case 6:    {DZE_buyMult = 1.2;    DZE_sellMult = 1.2;};
    case 7:    {DZE_buyMult = 0.9;    DZE_sellMult = 1.3;};
    case 8:    {DZE_buyMult = 0.8;    DZE_sellMult = 1.4;};
    case 9:    {DZE_buyMult = 0.7;    DZE_sellMult = 1.5;};
    case 10:{DZE_buyMult = 2.5;    DZE_sellMult = 1;}; //wildcard!
    case 11:{DZE_buyMult = 0.9;    DZE_sellMult = 2;}; //wildcard!
};

diag_log Format["[ECONOMY] You Buy %1 Percent, You Sell %2 Percent",(DZE_buyMult * 100), (DZE_sellMult * 100)];

modified 7 files in custom dayz_code\actions\advancedTrading\functions\: 

z_at_buyItems, z_at_calcPrice, z_at_displayBackpackInfo, z_at_displayItemInfo, z_at_displayVehicleInfo, z_at_displayWeaponInfo, z_at_sellItems

Those 7 files only include modification to the prices with DZE_buyMult or DZE_sellMult respectively.

Link to comment
Share on other sites

  • 0

no, this does not work. on each client there will be a different value for this code:

if (isNil "DZE_ecoMult") then {
	DZE_ecoMult = (round(random 12));
};

as it is initialized on each client. if executed from init.sqf.

global means only global to this client, not to all clients. public variable is the way to go.

you need to use a public variable serverside like this:

DZE_ecoMult = (round(random 12));

publicVariable "DZE_ecoMult";

this way it will be sent to all clients and its the same random amount on each client.

now with a serverside while do loop you could change it every xx seconds if you want to and broadcast the value to each clients again (pv's are join in progress compatible).

clientside you would just need to add this code to the top of your script:

waituntil {sleep xx; !isNil "DZE_ecoMult";};

...clientside code...

 

 

Link to comment
Share on other sites

  • 0

So running into trouble understanding how to execute this properly. If Im understanding properly, im executing this function

diag_log format["[ECONOMY] Using Economy, pre initialization of Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];

fn_fluctuating = {  
    DZE_ecoMult = (round(random 12));
	publicVariable "DZE_ecoMult";
    switch (DZE_ecoMult) do {
        case 0:        	{DZE_buyMult = 2;      DZE_sellMult = 0.5;};
        case 1:        	{DZE_buyMult = 1.9;    DZE_sellMult = 0.6;};
        case 2:        	{DZE_buyMult = 1.8;    DZE_sellMult = 0.7;};
        case 3:        	{DZE_buyMult = 1.6;    DZE_sellMult = 0.8;};
        case 4:        	{DZE_buyMult = 1.5;    DZE_sellMult = 0.9;};
        case 5:        	{DZE_buyMult = 1.4;    DZE_sellMult = 1.1;};
        case 6:        	{DZE_buyMult = 1.2;    DZE_sellMult = 1.2;};
        case 7:        	{DZE_buyMult = 0.9;    DZE_sellMult = 1.3;};
        case 8:        	{DZE_buyMult = 0.8;    DZE_sellMult = 1.4;};
        case 9:       	{DZE_buyMult = 0.7;    DZE_sellMult = 1.5;};
        case 10:    	{DZE_buyMult = 2.5;    DZE_sellMult = 1;}; //wildcard! bad day to be buying....
        case 11:    	{DZE_buyMult = 0.7;    DZE_sellMult = 2;}; //wildcard! MO MONEY!
    };
    [nil,nil,rTitleText,"Prices have changed! Check out the sweet deals NOW!", "PLAIN",10] call RE;
	diag_log format["[ECONOMY] Price Change Using Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];
};

waituntil {(round(time)) > 300}; 
call fn_fluctuating;

waituntil {(round(time)) > 400}; 
call fn_fluctuating;

waituntil {(round(time)) > 500}; 
call fn_fluctuating;

waituntil {(round(time)) > 600}; 
call fn_fluctuating;

from its install location in \z\addons\dayz_server\init\ and calling for it like so

_adv_economyFluc = [] execVM \z\addons\dayz_server\init\adv_economyFluc.sqf";

which is placed at the bottom of server_function.sqf

Meanwhile, ive removed all other calls for the mults from all other files with exception to the files that actually call for the mults.

I then added call fn_fluctuating near the top of advancedTrading\init.sqf hoping it would execute and call and set the variables.

I wont lie, i am very lost here however, I am very appreciative of the support and input!

Link to comment
Share on other sites

  • 0

Well, we are getting there, this was my latest error free report

"[ECONOMY] Using Economy, pre initialization of Economy! ~You Buy 100 Percent, You Sell 100 Percent"
"[ECONOMY] Using Economy, pre initialization of Economy! ~You Buy 100 Percent, You Sell 100 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 90 Percent, You Sell 130 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 250 Percent, You Sell 100 Percent"
"[ECONOMY] Using Economy, pre initialization of Economy! ~You Buy 250 Percent, You Sell 100 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 160 Percent, You Sell 80 Percent"
"[ECONOMY] Using Economy, pre initialization of Economy! ~You Buy 160 Percent, You Sell 80 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 120 Percent, You Sell 120 Percent"
"[ECONOMY] Using Economy, pre initialization of Economy! ~You Buy 120 Percent, You Sell 120 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 80 Percent, You Sell 140 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 80 Percent, You Sell 140 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 70 Percent, You Sell 200 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 140 Percent, You Sell 110 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 140 Percent, You Sell 110 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 140 Percent, You Sell 110 Percent"
"[ECONOMY] Using Economy, pre initialization of Economy! ~You Buy 140 Percent, You Sell 110 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 140 Percent, You Sell 110 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 190 Percent, You Sell 60 Percent"
"[ECONOMY] Using Economy, pre initialization of Economy! ~You Buy 190 Percent, You Sell 60 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 90 Percent, You Sell 130 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 120 Percent, You Sell 120 Percent"
"[ECONOMY] Using Economy, pre initialization of Economy! ~You Buy 120 Percent, You Sell 120 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 250 Percent, You Sell 100 Percent"
"[ECONOMY] Price Change Using Economy! ~You Buy 180 Percent, You Sell 70 Percent"

every time you see preInitialization, its my function calling and firing, then it changes the mults. Everytime i was executing the trader script it would do this. I currently have this in my advancedTrading\init.sqf
    _adv_economyFluc = [] execVM "dayz_code\actions\AdvancedTrading\functions\adv_econFluc.sqf";
 

i just changed it to this

if (isNile "DZE_ecoMult") then {
    _adv_economyFluc = [] execVM "dayz_code\actions\AdvancedTrading\functions\adv_econFluc.sqf";
};

in hopes i can stop the fluctuation from firing everytime a player activates the trader. It may fire for each player individually, it may not, im not too sure and i dont have a player base to test it. however, it was successfully modifying the mults and prices reflected this so its getting closer.

EDIT: Yep, that stopped it from firing everytime the player used the trader. If I move it to init.sqf, and place it in the if (isServer) block, it should start with server launch correct? and with the publicVariable it should be synced to all clients? if i am understanding this correctly?

Link to comment
Share on other sites

  • 0

@BigEgg @seelenapparat  @ViktorReznov  whats about if viktor execute it with  epoch event format.. something like:

fluctuating_event.sqf

Spoiler

private ["_remote_msg"];
_remote_msg =false;//true if u have remote_messages

if (isNil "EPOCH_EVENT_RUNNING") then {
EPOCH_EVENT_RUNNING = false;
};
 

if (EPOCH_EVENT_RUNNING) exitWith {
diag_log("Event already running");
};

EPOCH_EVENT_RUNNING = true;


if (_remote_msg)then {
_hint = parseText format["<t align='center' color='#2fee11' shadow='2' size='1'>Fluctuating Prices</t>"];
RemoteMessage = ['hint', _hint];
publicVariable "RemoteMessage";
}else{
[nil,nil,rTitleText,"Fluctuating Prices", "PLAIN",10] call RE;
};

  DZE_ecoMult = (round(random 12));
    switch (DZE_ecoMult) do {
        case 0:        {DZE_buyMult = 2;    DZE_sellMult = 0.5;};
        case 1:        {DZE_buyMult = 1.9;    DZE_sellMult = 0.6;};
        case 2:        {DZE_buyMult = 1.8;    DZE_sellMult = 0.7;};
        case 3:        {DZE_buyMult = 1.6;    DZE_sellMult = 0.8;};
        case 4:        {DZE_buyMult = 1.5;    DZE_sellMult = 0.9;};
        case 5:        {DZE_buyMult = 1.4;    DZE_sellMult = 1.1;};
        case 6:        {DZE_buyMult = 1.2;    DZE_sellMult = 1.2;};
        case 7:        {DZE_buyMult = 0.9;    DZE_sellMult = 1.3;};
        case 8:        {DZE_buyMult = 0.8;    DZE_sellMult = 1.4;};
        case 9:        {DZE_buyMult = 0.7;    DZE_sellMult = 1.5;};
        case 10:    {DZE_buyMult = 2.5;    DZE_sellMult = 1;}; //wildcard! bad day to be buying....
        case 11:    {DZE_buyMult = 0.7;    DZE_sellMult = 2;}; //wildcard! MO MONEY!
    };
diag_log format["[ECONOMY] Price Change Using Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];

publicVariable "DZE_ecoMult"; 
EPOCH_EVENT_RUNNING = false;

 

or:

Spoiler

private ["_remote_msg"];
_remote_msg =false;//true if u have remote_messages

if (isNil "EPOCH_EVENT_RUNNING") then {
EPOCH_EVENT_RUNNING = false;
};
 

if (EPOCH_EVENT_RUNNING) exitWith {
diag_log("Event already running");
};

EPOCH_EVENT_RUNNING = true;


if (_remote_msg)then {
_hint = parseText format["<t align='center' color='#2fee11' shadow='2' size='1'>Fluctuating Prices</t>"];
RemoteMessage = ['hint', _hint];
publicVariable "RemoteMessage";
}else{
[nil,nil,rTitleText,"Fluctuating Prices", "PLAIN",10] call RE;
};

  DZE_ecoMult = (round(random 12));
    switch (DZE_ecoMult) do {
        case 0:        {DZE_buyMult = 2;    DZE_sellMult = 0.5;};
        case 1:        {DZE_buyMult = 1.9;    DZE_sellMult = 0.6;};
        case 2:        {DZE_buyMult = 1.8;    DZE_sellMult = 0.7;};
        case 3:        {DZE_buyMult = 1.6;    DZE_sellMult = 0.8;};
        case 4:        {DZE_buyMult = 1.5;    DZE_sellMult = 0.9;};
        case 5:        {DZE_buyMult = 1.4;    DZE_sellMult = 1.1;};
        case 6:        {DZE_buyMult = 1.2;    DZE_sellMult = 1.2;};
        case 7:        {DZE_buyMult = 0.9;    DZE_sellMult = 1.3;};
        case 8:        {DZE_buyMult = 0.8;    DZE_sellMult = 1.4;};
        case 9:        {DZE_buyMult = 0.7;    DZE_sellMult = 1.5;};
        case 10:    {DZE_buyMult = 2.5;    DZE_sellMult = 1;}; //wildcard! bad day to be buying....
        case 11:    {DZE_buyMult = 0.7;    DZE_sellMult = 2;}; //wildcard! MO MONEY!
    };

{
DZE_buyMult=DZE_buyMult;
DZE_sellMult =DZE_sellMult;
} forEach playableUnits;

 

Link to comment
Share on other sites

  • 0

Although I don't have time to look at your code I'll try to help you get a grasp on the global variable and publicVar.

A Script can run client side or server side or both. An easy example is anything ran from the init.sqf gets ran by the server on startup and then by each client when they load into the server.

Commands exist to control this; isDedicated, isServer, hasInterface, etc.

Variables prefaced with an underscore are local to the scope the script is running in. If I use execvm to run a script nothing outside of the script will be able to see that variable.

Global variables have no underscore and are global to the side they are on, client or server. A client can have a global with a different value from every other client and the server. Any script running on that machine can change the global var at any time but cannot change the global variable on any of the other machines.

A public variable is when a global variable is broadcasted to other machines. A public variable must be rebroadcast each time you want to update a value on another machine.

There are 3 ways to broadcast a public variable; publicVariable, publicVariableServer, and publicVariableClient. These can be used on any machine and do what their name suggests. PublicVariable broadcasts to all machines, publicVariableServer broadcasts to server, and publicVariableClient broadcasts to a specific client.

When using publicVariableClient you must specify the client by netid. The easiest way is for the server to also provide the player object and then grab the owner of the object. You can use publicVariableClient on the same machine as the target. Same with server.

So an example, if you want each player to carry a random value but to update a value on the server on-command you can use publicVariableServer to only update the server variable while each client retains a unique value.

Another is if you want to track zombie kills in a var on each player you can then use publicVariableClient to update each specific player with their new stats when they kill a zombie from the serverside.

Things get more interesting when you start looking at publicVariableEventHandlers.

Hope this helps.

Link to comment
Share on other sites

  • 0

Got it, the final bit of secret..... when someone suggests to use a bit of code you are unfamiliar with.... GO READ IT! Read up on publicVariables and learned that you have to set it as a publicVariable AFTER you change it each time otherwise it doesnt update

completed code (has to be a way to write this better but for now, this works)

Spoiler

if (isNil "DZE_ecoMult") then {
	DZE_buyMult = 1;	publicVariable "DZE_buyMult";	DZE_sellMult = 1;	publicVariable "DZE_sellMult";//stock prices
};

diag_log format["[ECONOMY] Initialization of Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];

fn_fluctuating = {
    DZE_ecoMult = (round(random 12));
	publicVariable "DZE_ecoMult";
    switch (DZE_ecoMult) do {
        case 0:			{DZE_buyMult = 2.0;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 0.5;	publicVariable "DZE_sellMult";};
        case 1:			{DZE_buyMult = 1.9;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 0.6;	publicVariable "DZE_sellMult";};
        case 2:			{DZE_buyMult = 1.8;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 0.7;	publicVariable "DZE_sellMult";};
        case 3:			{DZE_buyMult = 1.6;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 0.8;	publicVariable "DZE_sellMult";};
        case 4:			{DZE_buyMult = 1.5;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 0.9;	publicVariable "DZE_sellMult";};
        case 5:			{DZE_buyMult = 1.4;		publicVariable "DZE_buyMult";	
						 DZE_sellMult = 1.1;	publicVariable "DZE_sellMult";};
        case 6:			{DZE_buyMult = 1.2;		publicVariable "DZE_buyMult";	
						 DZE_sellMult = 1.2;	publicVariable "DZE_sellMult";};
        case 7:			{DZE_buyMult = 0.9;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 1.3;	publicVariable "DZE_sellMult";};
        case 8:			{DZE_buyMult = 0.8;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 1.4;	publicVariable "DZE_sellMult";};
        case 9:			{DZE_buyMult = 0.7;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 1.5;	publicVariable "DZE_sellMult";};
        case 10:		{DZE_buyMult = 2.5;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 1.0;	publicVariable "DZE_sellMult";}; //wildcard! bad day to be buying....
        case 11:		{DZE_buyMult = 0.7;		publicVariable "DZE_buyMult";
						 DZE_sellMult = 2.0;	publicVariable "DZE_sellMult";}; //wildcard! MO MONEY!
    };
    [nil,nil,rTitleText,"Prices have changed! Check out the sweet deals NOW!", "PLAIN",10] call RE;
	diag_log format["[ECONOMY] Price Change Using Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];
};

waituntil {(round(time)) > 60}; //reset after one minute, should have fluctuating prices before any player gets there short of admins
call fn_fluctuating;

waituntil {(round(time)) > 1800}; //every 30 minutes, reset
call fn_fluctuating;

waituntil {(round(time)) > 3600}; //every 30 minutes, reset
call fn_fluctuating;

waituntil {(round(time)) > 5400}; //every 30 minutes, reset
call fn_fluctuating;

waituntil {(round(time)) > 7200}; //every 30 minutes, reset
call fn_fluctuating;

waituntil {(round(time)) > 9000}; //every 30 minutes, reset
call fn_fluctuating;

waituntil {(round(time)) > 10800}; //every 30 minutes, reset
call fn_fluctuating;

 

 

Link to comment
Share on other sites

  • 0

Reading your code this is really over-complicated for what it does. Part of the age old "if you wrote it twice you wrote it too much". You should never need to use random and then a switch unless you are randomizing code to run.

A call like that is also poor as I believe each call would create a new thread. The only downside to my code is it is likely biased to one side, only your testing will tell. Your minimums and maxes may need adjusting.

Here's my take on it.

 

/*
	Author: Vampire (vampuricgaming.net)
	Desc: Randomize Market Multiplier
											*/

if !(isDedicated) exitWith { /* Not a Server */ };

// stock prices
DZE_buyMult = 1;
DZE_sellMult = 1;
publicVariable "DZE_buyMult";
publicVariable "DZE_sellMult";

while {true} do {
	// Randomize the Market
	_buyMax = 2.5; // Max Buying Mult
	_buyMin = 0.7; // Min Buying Mult
	_sellMax = 2; // Max Sell Mult
	_sellMin = 0.5; // Min Sell Mult
	_sleep = 1800; // Time to Sleep
	_time = diag_tickTime; // Current Time (in seconds)
	
	// Randomize -> trim to one decimal -> make sure in min/max -> broadcast
	DZE_buyMult = [(random _buyMax),1] call BIS_fnc_cutDecimals;
	if (DZE_buyMult < _buyMin) then { DZE_buyMult = _buyMin; };
	if (DZE_buyMult > _buyMax) then { DZE_buyMult = _buyMax; };
	publicVariable "DZE_buyMult";
	
	DZE_sellMult = [(random _sellMax),1] call BIS_fnc_cutDecimals;
	if (DZE_sellMult < _sellMin) then { DZE_sellMult = _sellMin; };
	if (DZE_sellMult > _sellMax) then { DZE_sellMult = _sellMax; };
	publicVariable "DZE_sellMult";
	
	// Only show if not startup run
	if !(isNil "MULT_startDone") then {
		[nil,nil,rTitleText,"Prices have changed! Check out the sweet deals NOW!", "PLAIN",10] call RE;
	} else {
		MULT_startDone = true;
	};
	
	diag_log format["[ECONOMY] Price Change Using Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];
	
	// Sleep Time
	waitUntil{sleep 1;diag_tickTime >= (_time + _sleep)};
};

 

Much cleaner in less lines and it should be better performance. You also don't need to include it inside (isServer) since I have an exitWith at the top if it isn't a server.

Link to comment
Share on other sites

  • 0

alright, trying something new inspired by @TheVampire. I love the code you showed but kinda broke my idea of a scaling price of supply vs demand sort of thing. However, using some ideas from your code to try something new and Im curious about if it will work (im going to test anyways but im looking for the logic behind the reasons for the ensuing answers) 

Thanks again so much everybody!

Spoiler

if !(isDedicated) exitWith {}; //if not a server then EFF this code

// stock prices
DZE_buyMult = 1; //initialize at stock
DZE_sellMult = 1; //initialize at stock
publicVariable "DZE_buyMult"; //sync variable with client and server
publicVariable "DZE_sellMult"; //sync variable with client and server

diag_log format["[ECONOMY] Initialization of Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];

while {true} do { //while true so basically == always run script loop except for that sleep down there
	_rand = random 12; //just generate our random var which will generate a number from 0 to 11
	_multArray = [ //array to hold values and keep the scaling of the market
					["2.0","0.5"], //select 0 [BuyMult,SellMult] = select 0, select 1
					["1.9","0.6"], //select 1
					["1.8","0.7"], //select 2
					["1.6","0.8"], //select 3
					["1.5","0.9"], //select 4
					["1.4","1.1"], //select 5
					["1.2","1.2"], //select 6
					["0.9","1.3"], //select 7
					["0.8","1.4"], //select 8
					["0.7","1.5"], //select 9
					["2.5","1.0"], //select 10
					["0.7","2.0"]  //select 11
				];
	_buymult = (_multArray select _rand) select 0; //in theory will select array value of _rand, buyMult
	_sellMult = (_multArray select _rand) select 1; //in theory will select array value of _rand, sellMult
	_sleep = 180; //3 min sleep for testing
	_time = diag_tickTime; //track server time
	
	DZE_buyMult = _buyMult; //equals the new variable set by the array
	DZE_sellMult = _sellMult; //equals the new variable set by the array
	publicVariable "DZE_buyMult"; //sync variable with client and server
	publicVariable "DZE_sellMult"; //sync variable with client and server
	
	diag_log format["[ECONOMY] Price Change Using Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];
	
	waitUntil{sleep 1;diag_tickTime >= (_time + _sleep)}; //stop script until new time reached
};

 

EDIT: This works, ofcourse only after i changed my strings to integers *facepalm*

Link to comment
Share on other sites

  • 0

@ViktorReznov you also can try randomize with this code:

Spoiler

DZE_ecoMult = [
    ["2.5","1"],
	["2","0.5"],
	["1.9","0.6"],
	["1.8","0.7"],
	["1.6","0.8"],
	["1.5","0.9"],
	["1.4","1.1"],
	["1.2","1.2"],
	["0.9","1.3"],	
	["0.8","1.4"],
	["0.7","1.5"],
	["0.7","2"]
];
_fluct = floor random (count DZE_ecoMult);
DZE_buyMult = DZE_ecoMult select _fluct select 0;
DZE_sellMult = DZE_ecoMult select _fluct select 1;

 

 

Link to comment
Share on other sites

  • 0
34 minutes ago, juandayz said:

_fluct = floor random (count DZE_ecoMult);

when you use a count, it counts in whole numbers and starts with 1 correct? so would we have to modify the count by -1 in order for it to return the proper array value which starts with 0?

Using arrays is still really new to me (at least understanding this whole select and stuff)

EDIT: Disregard, yes, arrays start at zero and count just counts. but the random of the count will still produce ((count) -1) and random will be a value 0 to ((count) -1)

Now this I am happy with

if !(isDedicated) exitWith {}; //if not a server then EFF this code

// stock prices
DZE_buyMult = 1; //initialize at stock
DZE_sellMult = 1; //initialize at stock
publicVariable "DZE_buyMult"; //sync variable with client and server
publicVariable "DZE_sellMult"; //sync variable with client and server

diag_log format["[ECONOMY] Initialization of Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];

while {true} do { //while true so basically == always run script loop except for that sleep down there
    _multArray = [ //array to hold values and keep the scaling of the market
                    [2.0,0.5], //select 0 [BuyMult,SellMult] = select 0, select 1
                    [1.9,0.6], //select 1
                    [1.8,0.7], //select 2
                    [1.6,0.8], //select 3
                    [1.5,0.9], //select 4
                    [1.4,1.1], //select 5
                    [1.2,1.2], //select 6
                    [0.9,1.3], //select 7
                    [0.8,1.4], //select 8
                    [0.7,1.5], //select 9
                    [2.5,1.0], //select 10
                    [0.7,2.0]  //select 11
                ];
    _rand = random(count _multArray); //just generate our random var which will count the number of arrays in _multArray and pull a random from it
    _sleep = 1800; //30 min sleep cycle
    _time = diag_tickTime; //track server time
    
    DZE_buyMult = (_multArray select _rand) select 0; //in theory will select array value of _rand, buyMult
    DZE_sellMult = (_multArray select _rand) select 1; //in theory will select array value of _rand, sellMult
    publicVariable "DZE_buyMult"; //sync variable with client and server
    publicVariable "DZE_sellMult"; //sync variable with client and server
    
    [nil,nil,rTitleText,"Prices have changed! Check out the sweet deals NOW!", "PLAIN",10] call RE;
    diag_log format["[ECONOMY] Price Change Using Economy! ~You Buy %1 Percent, You Sell %2 Percent", (DZE_buyMult * 100), (DZE_sellMult * 100)];
    
    waitUntil{sleep 1;diag_tickTime >= (_time + _sleep)}; //stop script until new time reached
};

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...