View Full Version : The use of global variables
podvoxx
05-24-2012, 05:55 AM
Is there a way to use the same variable in the host-mission and submission without using a .txt-file(for save variables)?
For example:
In host mission script I have count of planes
int countPlanes = 5;
In submission script I need to change this:
int countPlanes -= 1;
It is necessary to divide the script menu and script statistics, but they used to share data.
Now I now about this variant - http://www.sukhoi.ru/forum/showthread.php?t=68629&p=1766444&viewfull=1#post1766444
There are other alternatives a more simple than this? Maybe new class on dll help on it?
FG28_Kodiak
05-24-2012, 06:53 AM
Yes a Singleton Pattern in a DLL.
http://en.wikipedia.org/wiki/Singleton_pattern
Example Singleton (threadsafe)
public sealed class Singleton
{
public int planeCount { get; set; }
private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());
public static Singleton GetInstance { get { return lazy.Value; } }
private Singleton()
{
// Initialisations
}
}
in Mis-C# you use for example
//$reference parts/core/MyDll.dll
using System;
using System.Collections.Generic;
using maddox.game;
using maddox.game.world;
using MyNameSpace;
public class Mission : AMission
{
Singleton s=Singleton.GetInstance; // get access to the singleton or create it if not exist.
public override void OnPlaceEnter(Player player, AiActor actor, int placeIndex)
{
base.OnPlaceEnter(player, actor, placeIndex);
s.planeCount += 1; // example
}
}
use Singleton s=Singleton.GetInstance; in every Scriptfile you need acces to the singleton. There is always only one Singleton present.
podvoxx
05-24-2012, 08:48 AM
Yes a Singleton Pattern in a DLL.
Thank you, I hope it's help me.
podvoxx
05-26-2012, 10:31 AM
Great, it's work. :)
podvoxx
05-26-2012, 11:22 AM
My dll-file:
using System;
namespace smpcore
{
public static class SMPcore
{
public static int addNum(int x, int y)
{
return x + y;
}
}
public sealed class Singleton
{
public int planeCount { get; set; }
// Global side score
public int redScore { get; set; }
public int blueScore { get; set; }
// Number dead units global statistic
public int numberDeadRedPlanes { get; set; } // aircraft unit
public int numberDeadBluePlanes { get; set; } // aircraft unit
public int numberDeadRedAAA { get; set; }
public int numberDeadBlueAAA { get; set; }
public int numberDeadRedTanks { get; set; } // Tank unit
public int numberDeadBlueTanks { get; set; } // Tank unit
public int numberDeadRedArtillery { get; set; } // Artillery unit
public int numberDeadBlueArtillery { get; set; } // Artillery unit
public int numberDeadRedCars { get; set; } // Cars unit
public int numberDeadBlueCars { get; set; } // Cars unit
public int numberDeadRedShips { get; set; } // Ships unit
public int numberDeadBlueShips { get; set; } // Ships unit
private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());
public static Singleton GetInstance { get { return lazy.Value; } }
private Singleton()
{
// Initialisations
planeCount = 1;
// Global side score
redScore = 0;
blueScore = 0;
// Number dead units global statistic
numberDeadRedPlanes = 0; // aircraft unit
numberDeadBluePlanes = 0; // aircraft unit
numberDeadRedAAA = 0;
numberDeadBlueAAA = 0;
numberDeadRedTanks = 0; // Tank unit
numberDeadBlueTanks = 0; // Tank unit
numberDeadRedArtillery = 0; // Artillery unit
numberDeadBlueArtillery = 0; // Artillery unit
numberDeadRedCars = 0; // Cars unit
numberDeadBlueCars = 0; // Cars unit
numberDeadRedShips = 0; // Ships unit
numberDeadBlueShips = 0; // Ships unit
}
}
}
My script:
//$reference "parts\core\smp.dll"
//$reference "parts\core\smpMessage.dll"
using System;
using System.Collections.Generic;
using maddox.game;
using maddox.game.world;
using smpcore;
using smpmessage;
// HOST-MISSION Script
public class Mission : AMission
{
// =========================HOST-MISSION PARAMETERS===========================
// Setup file and folder path
string setupMisMPath = Environment.GetFolderPath(Environment.SpecialFolde r.Personal) + "\\1C SoftClub\\il-2 sturmovik cliffs of dover\\missions\\SMP\\Friday on my mind\\system\\mission_setup.ini";
int unitPlanePrice = 10;
int unitAAAPrice = 3;
int unitTankPrice = 10;
int unitArtilleryPrice = 2;
int unitCarPrice = 2;
int unitShipPrice = 10;
// TESTTESTTESTTESTTESTTEST
Singleton stat = Singleton.GetInstance; // get access to the singleton or create it if not exist.
public override void OnBattleStarted()
{
base.OnBattleStarted();
MissionNumberListener = -1;
smpSendMSG.MessageToAll("BATTLE STARTED!", "Chat");
// LOAD SUBMISSIONS
GamePlay.gpPostMissionLoad("missions\\SMP\\Friday on my mind\\submissions\\Test1\\Test1.mis");
GamePlay.gpPostMissionLoad("missions\\SMP\\Friday on my mind\\submissions\\Test2\\Test2.mis");
}
// =========================OnActorDead============== ===============
public override void OnActorDead(int missionNumber, string shortName, AiActor actor, List<DamagerScore> damages)
{
base.OnActorDead(missionNumber, shortName, actor, damages);
if (actor != null)
{
// AiGroundActor STATISTIC
if (actor is AiGroundActor)
{
AiGroundActor groundActor = actor as AiGroundActor;
if (groundActor != null)
{
// AAArtillery points
if (groundActor.Type() == AiGroundActorType.AAGun)
{
if (actor.Army() == 1)
{
stat.numberDeadRedAAA ++;
stat.blueScore += unitAAAPrice;
}
else if (actor.Army() == 2)
{
stat.numberDeadBlueAAA++;
stat.redScore += unitAAAPrice;
}
smpSendMSG.MessageToAll("Уничтожена зенитка [ Red/Blue ]:..." + stat.numberDeadRedAAA.ToString() + " / " + stat.numberDeadBlueAAA.ToString(), "Chat");
}
} // null
}
} // null
}
}
stat.redScore, stat.blueScore - no error
other stat. have error
Second stat. element in smpSendMSG.MessageToAll - no error.
I have error on VS, but script work fine in game.
FG28_Kodiak
05-26-2012, 12:14 PM
No problem by me, do you use a static class with name stat?
if you rename "stat" (singleton) the problem also exist?
podvoxx
05-26-2012, 02:31 PM
No problem by me, do you use a static class with name stat?
if you rename "stat" (singleton) the problem also exist?
I don'n know why, but now I no have this problem. All work very good
vBulletin® v3.8.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.