PDA

View Full Version : What's wrong with this?



SoW Reddog
Sep-27-2013, 16:22
Playing around with Lists for the first time, all I'm trying to do is build a list of Red players as they enter the game, and then check whether they're in friendly territory or not. (This will eventually morph into the anti vulch code to protect French airfields in my mission)


using System.Text;
using System;
using System.Collections;
using maddox.game;
using maddox.game.world;
using maddox.GP;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;

public class Mission : AMission
{

public List<Player> Players = new List<Player>();

public override void OnPlaceEnter(Player player, AiActor actor, int placeIndex)
{
base.OnPlaceEnter(player, actor, placeIndex);
if (GamePlay.gpPlayer() != null)
{
if (GamePlay.gpPlayer().Army() == 1)
Players.Add(GamePlay.gpPlayer());
} // Multiplayer
if (GamePlay.gpRemotePlayers() != null || GamePlay.gpRemotePlayers().Length > 0)
{
foreach (Player p in GamePlay.gpRemotePlayers())
{
if (p.Army() == 1)
Players.Add(p);
}
}
}

public override void OnTickGame()
{
if (Time.tickCounter() % 2000 == 1800)
{
foreach (Player pl in Players())
{
if (GamePlay.gpFrontArmy(pl.Pos().x, pl.Pos().y) != pl.Army())
{
GamePlay.gpHUDLogCenter("Front and BirthPlace DO NOT match!");
}
else
{
GamePlay.gpHUDLogCenter("Front and BirthPlace DO match!");
}
}
}

}

}

I'm getting an error on foreach (Player pl in Players()) which is Players does not exist in the current context. What have I done wrong??

SoW Reddog
Sep-27-2013, 19:55
OK, fixed that issue with
foreach (Player pl in Players)
{
if (GamePlay.gpFrontArmy(pl.Place().Pos().x, pl.Place().Pos().y) != pl.Army())

I've now got an issue referencing the players aircraft. I'd hoped that
pl.aircraft would work, but it doesn't and the following
pl.place() generates a "cannot convert from aiactor to aiaircraft" error in compile.

Any ideas?

SoW Reddog
Sep-27-2013, 20:21
Never mind, figured a way around it.

Salmo
Sep-28-2013, 19:29
Playing around with Lists for the first time, all I'm trying to do is build a list of Red players as they enter the game, and then check whether they're in friendly territory or not. (This will eventually morph into the anti vulch code to protect French airfields in my mission)


using System.Text;
using System;
using System.Collections;
using maddox.game;
using maddox.game.world;
using maddox.GP;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;

public class Mission : AMission
{

public List<Player> Players = new List<Player>();

public override void OnPlaceEnter(Player player, AiActor actor, int placeIndex)
{
base.OnPlaceEnter(player, actor, placeIndex);
if (GamePlay.gpPlayer() != null)
{
if (GamePlay.gpPlayer().Army() == 1)
Players.Add(GamePlay.gpPlayer());
} // Multiplayer
if (GamePlay.gpRemotePlayers() != null || GamePlay.gpRemotePlayers().Length > 0)
{
foreach (Player p in GamePlay.gpRemotePlayers())
{
if (p.Army() == 1)
Players.Add(p);
}
}
}

public override void OnTickGame()
{
if (Time.tickCounter() % 2000 == 1800)
{
foreach (Player pl in Players())
{
if (GamePlay.gpFrontArmy(pl.Pos().x, pl.Pos().y) != pl.Army())
{
GamePlay.gpHUDLogCenter("Front and BirthPlace DO NOT match!");
}
else
{
GamePlay.gpHUDLogCenter("Front and BirthPlace DO match!");
}
}
}

}

}

I'm getting an error on foreach (Player pl in Players()) which is Players does not exist in the current context. What have I done wrong??

A few suggestions with your methodology:

1. You use OnPlaceEnter to add to the list of red players, but will need to remove red players from your list if they leave the game or change to the blue side. A better method may be to construct your red player list each time the OnTick event is called. That way you won't need to track red player movements (changing sides etc) during gameplay.

2. You may get some "null" errors using the 'foreach' call. A good practice would be to enclose the foreach statement in a Try-catch format. ie.


try
{
foreach (Player pl in Players())
{
if (GamePlay.gpFrontArmy(pl.Pos().x, pl.Pos().y) != pl.Army())
{
GamePlay.gpHUDLogCenter("Front and BirthPlace DO NOT match!");
}
else
{
GamePlay.gpHUDLogCenter("Front and BirthPlace DO match!");
}
}
}
catch {}



Alternatively, where possile, use a 'for' statement with an integer counter as this does not throw a 'null' error if the list or the elemnets your looping through is empty. ie. for (int 1=0; i<list.count; i++)

ATAG_Deacon
Sep-28-2013, 21:04
(This will eventually morph into the anti vulch code to protect French airfields in my mission)


Just curious why you would single out one side when both sides 'vulch'? I'm not making this Red vs. Blue, but
why a bias in a mission you're creating?

Salmo
Sep-28-2013, 23:14
Just curious why you would single out one side when both sides 'vulch'? I'm not making this Red vs. Blue, but
why a bias in a mission you're creating?

I've considered this strategy for missions where I want to 'encourage' red to defend over England.

Combat Wombat
Sep-28-2013, 23:24
so it happens from time to time :stunned:

92 Sqn. Philstyle (QJ-P)
Sep-29-2013, 07:24
Just curious why you would single out one side when both sides 'vulch'? I'm not making this Red vs. Blue, but
why a bias in a mission you're creating?

Because one side, historically, did not attack/strafe enemy airfields during this period of the war. The other did.

SoW Reddog
Sep-29-2013, 08:11
Just curious why you would single out one side when both sides 'vulch'? I'm not making this Red vs. Blue, but
why a bias in a mission you're creating?

Because the Battle of Britain was not characterised by Hurricane and Spitfire pilots flying their crates across the channel on the off chance of racking up some easy kills knowing full well that their return to England was unlikely given flak and enemy CAPs.

I'm making no statement about vulching as such, just that in my mission (and only this one) I want to encourage the Red's to defend in a semi historical way, ie, over France and the Channel. 2 of the mission targets are fully achievable by AI bombers, so the map will roll easily enough if the Reds swan off. The script kicks in when a Red player nears the French coast, they've still got loads of Channel to play with.

Besides, it means I don't need to place a load of Flak objects near the French bases, allowing me to place them in England and keeping the object count the same.

If there's a huge backlash against the idea then fine. We can stick with the same 3-4 missions that are on the server. :D