Unity 5 Saving and Loading variables - save

i know i can save int,float,string with PlayerPrefs, but how can i save a boolean too, like the Player has money, and he is bought an upgrade, how can i save if the player bought the upgrade and load it next time?
I have my upgrades like this:
public void Computer()
{
if(tier1 == true && Click.money >= cost)
{
Click.money -= cost;
ItemNameInfo.text = "[TIER II]Computer";
UpgradeInfo.text = "Wooaah! Upgrade Time!\n(Gives +5 CPS)";
cost = 1000;
costInfo.text = "Cost: " + cost;
Click.moneyperclick += 1;
tier1 = false;
tier2 = true;
}
else if(tier2 == true && Click.money >= cost)
{
Click.money -= cost;
ItemNameInfo.text = "[TIER III]Computer";
UpgradeInfo.text = "It's still isn't good enough\n(Gives +10 CPS)";
cost = 20000;
costInfo.text = "Cost: " + cost;
Click.moneyperclick += 5;
tier2 = false;
tier3 = true;
}
}

You cannot save bools to Player Prefs.
I tend to just use an int.
For example, let's say we have an item "item1".
To check if the player has that item unlocked:
if(PlayerPrefs.GetInt("hasItem1",0) == 1){
//player has item 1
}
to unlock item1:
PlayerPrefs.SetInt("hasItem1",1);

Related

Is there any arithmetic formula that can test all given numbers are in row, like [ 3 5 4 ]

I m making a card game where 3 random numbers are generated..I need to check are these numbers Row numbers...
like 4 6 5 and 23,24,22. are row numbers
I have made method but I think there should be easy arithmetic formulas
I have tried this and working well, but I need simple arithmatic formula to avoid use of array and for
bool isAllInRow(int num1, int num2,int num3)
{
//subject : tinpati
List<int> numbers=[num1,num2,num3];
bool is_in_row=true;
numbers.sort();
if(numbers[0]==1 && numbers[1]==12 && numbers[2]==13)
return true;
for(int x=0;x<numbers.length-1;x++)
{
if(numbers[x]-numbers[x+1]!=-1)
{
is_in_row=false;
break;
}
}
return is_in_row;
}
So you want to know if the cards form a straight, with aces both low and high.
Is the "three cards" fixed, or would you want to generalize to more cards?
Sorting should be cheap for such a short list, so that's definitely a good start. Then you just need to check the resulting sequence is increasing adjacent values.
I'd do it as:
bool isStraight(List<int> cards) {
var n = cards.length;
if (n < 2) return true;
cards.sort();
var first = cards.first;
if (first == 1 && cards[1] != 2) {
// Pretend Ace is Jack if n == 3.
// Accepts if remaining cards form a straight up to the King.
first = 14 - n;
}
for (var i = 1; i < n; i++) {
if (cards[i] != first + i) return false;
}
return true;
}
This code rejects card sets that have duplicates, or do not form a straight.
I think you are looking for Arithmetic Progression.
bool checkForAP(List<int> numberArr) {
numberArr.sort();
int diff = numberArr[1] - numberArr[0];
if (numberArr[2] - numberArr[1] != diff) {
return false;
}
return true;
}
And modify your function like
bool isAllInRow(int num1, int num2,int num3) {
//subject : tinpati
List<int> numbers=[num1,num2,num3];
bool is_in_row=true;
numbers.sort();
if(numbers[0]==1 && numbers[1]==12 && numbers[2]==13)
return true;
return checkForAP(numbers);
}
Note: remove sort in AP method as it is of no use. Since your numbers
list length is 3 I directly compared numbers for AP, the same can also
be written for n numbers with for.
bool checkForAp(numberArr) {
numberArr.sort();
int diff = numberArr[1] - numberArr[0];
for(int i = 2; i< numberArr.length ;i++) {
if (numberArr[i] - numberArr[i - 1] != diff) {
return false;
}
}
return true;
}
You could do it like this:
bool isAllInRow(int num1, int num2,int num3) {
if (num1 == num2 || num2 == num3) return false;
var maxNum = max(num1, max(num2, num3));
var minNum = min(num1, min(num2, num3));
return (maxNum - minNum == 2) || (minNum == 1 && maxNum == 13 && num1 + num2 + num3 == 26);
}

Why Buy order is not placed when calculated Stoploss and Take profit are used

I have a simple BUY order that, when the Take Profit and Stoploss are in points (ie: Ask+10*_Point), operates correctly. But when I change the take profit and stop loss from points to my own calculated values (CL_OP), the BUY order is not placing trades. How can I solve the issue:
input int _Hour =16;
input int _Minute =30;
bool NewBar()
{
static datetime OldTime = 0;
if(OldTime < Time[0])
{
OldTime = Time[0];
return(true);
}
else
{
return(false);
}
}
void OnTick()
{
int Hour_Int = Hour();
int Minute_Int = Minute();
double CL_OP = (Close[1] - Open[1]);
bool LastBearHiLoEngulf = (Close[1] < Open[1] && High[1] > High[2] && Low[1] < Low[2] );
if(NewBar())
if(OrdersTotal()==0)
// Apply signals on Buy opportunities
if( Hour_Int == _Hour && Minute_Int == _Minute && LastBearHiLoEngulf == TRUE)
{
int buyticket = OrderSend
(
Symbol(), // all symbols
OP_BUY, // Buy without delay
1, // 1 Microlots
Ask, // for market price
3, // 3 pips slippage
Ask - CL_OP, // Stop Loss - 1000*_Point
Ask + CL_OP , // Take profitworks with 100*_Point
"Buy LastBearHiLoEngulf Target: ", // comment
144, // Magic number
0, // no experiation day
Green // draw green arrow
);
}
}
double CL_OP = (Close[1] - Open[1]);
CL_OP is not a normal price to open trade. Actually, it is the Size of the Candle 1!
It should be something like this:
double CL_OP = Close[1] + (Close[1] - Open[1]);

how to track take profit and stoploss in mql4

I have the following code in mql4 and it is partially working but having an issue with order tracking.
int ticket;
void OnTick(){
... not included code, setting variables, etc ...
if(tenkan_sen < kijun_sen){
comment += "\nSHORT!";
if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderType() == OP_BUY){
if(OrderClose(OrderTicket(),OrderLots(),Bid,1000,clrCrimson)){
ticket = 0;
}
}
if(ticket <= 0){
// need to set stoploss and takeprofit prices
double short_tp = current_close - (atr*6);
double short_sl = current_close + (atr*2);
ticket = OrderSend(_Symbol,OP_SELL,0.01,Bid,1000,short_sl,short_tp,"This is a sell",1,0,clrPink);
}
} else if(tenkan_sen > kijun_sen){
comment += "\nLONG!";
if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderType() == OP_SELL){
if(OrderClose(OrderTicket(),OrderLots(),Ask,1000,clrPink)){
ticket = 0;
}
}
if(ticket <= 0){
// need to set stoploss and take profit prices
double long_tp = current_close + (atr*6);
double long_sl = current_close - (atr*2);
ticket = OrderSend(_Symbol,OP_BUY,0.01,Ask,1000,long_sl,long_tp,"This is a buy",1,0,clrCrimson);
}
}
}
This was previously based on the logic of closing the previous position upon opening the last position, it was working as expected before I added the sl and tp values. I am not sure how I should reset the ticket variable to 0 in the event of a sl or tp, or if there is a different way I should be handling this.

TypeError: Indexed expression has to be a type, mapping or array (is function (uint16) returns (uint256))?

Full source:https://github.com/laronlineworld/bettingMatch/blob/main/bettingMatch.sol This is a Betting Smart Contract, trying to create a mapping structure that user/wallet_address can bet on multiple matches. The problem of this betting contract is every time the user/wallet_address place a bet, the data of single mapping overwrite, how to create a mapping of value so that user/wallet_address can bet on different matches.
Place bet function:
function bet(uint16 _matchSelected, uint16 _resultSelected) public payable {
require(matchBettingActive[_matchSelected], "Betting: match voting is disabled");
//Check if the player already exist
// require(!checkIfPlayerExists(msg.sender));
//Check if the value sended by the player is higher than the min value
require(msg.value >= minimumBet);
//Set the player informations : amount of the bet, match and result selected
playerInfo[msg.sender].amountBet = msg.value;
playerInfo[msg.sender].matchSelected = _matchSelected;
playerInfo[msg.sender].resultSelected = _resultSelected;
//Add the address of the player to the players array
players.push(msg.sender);
MatchID[] storage bets = matchDetails[_matchSelected];
bets.push(MatchID(msg.sender, _matchSelected, msg.value, _resultSelected))-1;
uint16[] storage userBets = userToBets[msg.sender];
userBets.push[_matchSelected];
//Finally increment the stakes of the team selected with the player bet
if ( _resultSelected == 1){
totalBetHome[_matchSelected] += msg.value;
}
else if( _resultSelected == 2){
totalBetAway[_matchSelected] += msg.value;
}
else{
totalBetDraw[_matchSelected] += msg.value;
}
also need to sync the data in reward distribution depending of matchID distribution function:
function distributePrizes(uint16 matchFinished, uint16 teamWinner) public onlyOwner {
address[1000] memory winners;
//Temporary in memory array with fixed size. Let's choose 1000
uint256 count = 0; // This is the count for the array of winners
uint256 loserBet = 0; //This will take the value of all losers bet
uint256 winnerBet = 0; //This will take the value of all winners bet
address add;
uint256 bets;
address playerAddress;
//Check who selected the winner team
for(uint256 i = 0; i < players.length; i++){
playerAddress = players[i];
//If the player selected the winner team, we add his address to the winners array
if(playerInfo[playerAddress].matchSelected == matchFinished &&
playerInfo[playerAddress].resultSelected == teamWinner){
winners[count] = playerAddress;
count++;
}
}
//We define which bet sum is the Loser one and which one is the winner
if ( teamWinner == 1){
loserBet = totalBetAway[matchFinished] + totalBetDraw[matchFinished];
winnerBet = totalBetHome[matchFinished];
}
else if ( teamWinner == 2){
loserBet = totalBetHome[matchFinished] + totalBetDraw[matchFinished];
winnerBet = totalBetAway[matchFinished];
}
else{
loserBet = totalBetHome[matchFinished] + totalBetAway[matchFinished];
winnerBet = totalBetDraw[matchFinished];
}
//We loop through the array of winners, to give ethers to the winners
for(uint256 j = 0; j < count; j++){
//Check that the address in this fixed array is not empty
if(winners[j] != address(0))
add = winners[j];
bets = playerInfo[add].amountBet;
uint256 amountToPlayer = (bets * (10000+(loserBet*devFee/winnerBet))) / 10000;
winners[j].transfer(amountToPlayer);
}
//Reset all variables
delete playerInfo[playerAddress];
players.length = 0;
loserBet = 0;
winnerBet = 0;
//10 will be the number of matches (To improve this)
for(uint256 k = 0; k < 10; k++){
totalBetHome[k] = 0;
totalBetAway[k] = 0;
totalBetDraw[k] = 0;
}
}

Only One EA works at a time. The others won't open as long as there's a trade

The Orderselect is used to control the number of trades the EA opens. I didnt use OrdersTotal because it creates the problem of blocking other EAs. Now it seems OrdersSelect also does the same thing. I need each EA to only open two trades and allow other EAs to open theirs too.
void OnTick()
{
double movingAverageSS = iMA(NULL,60,LowerMAS,0,MODE_SMA,PRICE_CLOSE,0);
double lastmovingAverageSS = iMA(NULL,60,LowerMAS,0,MODE_SMA,PRICE_CLOSE,1);
double movingAverageSB = iMA(NULL,60,LowerMAB,0,MODE_SMA,PRICE_CLOSE,0);
double lastmovingAverageSB = iMA(NULL,60,LowerMAB,0,MODE_SMA,PRICE_CLOSE,1);
double movingAverageFS = iMA(NULL,60,UpperMAS,0,MODE_SMA,PRICE_CLOSE,0);
double lastmovingAverageFS = iMA(NULL,60,UpperMAS,0,MODE_SMA,PRICE_CLOSE,1);
double movingAverageFB = iMA(NULL,60,UpperMAB,0,MODE_SMA,PRICE_CLOSE,0);
double lastmovingAverageFB = iMA(NULL,60,UpperMAB,0,MODE_SMA,PRICE_CLOSE,1);
for(int b=0;b<OrdersTotal();b++)
{
if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES) == true)
{
if (OrderMagicNumber() != MagicB)
{
if((lastmovingAverageFB<lastmovingAverageSB) && (movingAverageFB > movingAverageSB))
{
b = OrderSend (Symbol(),OP_BUY,lotSize,Ask,4,Ask - SLPB*_Point, Ask + TPB1*_Point,NULL,128,0,Green);
b = OrderSend (Symbol(),OP_BUY,lotSize,Ask,4,Ask - SLPB*_Point, Ask + TPB2*_Point,NULL,128,0,Green);
}
else if((lastmovingAverageFS>lastmovingAverageSS)&&(movingAverageFS<movingAverageSS))
{
b = OrderSend (Symbol(),OP_SELL,lotSize,Bid,4,Ask + SLPS*_Point,Ask - TPS1*_Point,NULL,128,0,Red);
b = OrderSend (Symbol(),OP_SELL,lotSize,Bid,4,Ask + SLPS*_Point,Ask - TPS2*_Point,NULL,128,0,Red);
}
}
}
}

Resources