I am gridding around an object but I need the grid to align in the local space of the pivot object
the code that i have
private void createColum()
{
for (int i =0; i<gridGame.Count;i++)
{
if(gridGame[i] == gridGame[0])
{
gridGame[i].localPosition = gridGame[0].localPosition;
}
if(gridGame[i]!= gridGame[0])
{
gridGame[i].localPosition = new Vector3(gridGame[i-1].localPosition.x + distX,gridGame[0].localPosition.y, gridGame[0].localPosition.z);
}
if(i +1 > limiteFila)
{
if(i%limiteFila==0)
{
gridGame[i].localPosition = new Vector3(gridGame[0].position.x, gridGame[0].position.y , gridGame[i-Mathf.RoundToInt(limiteFila)].position.z + distY);
}else
{
gridGame[i].position = new Vector3(gridGame[i-1].position.x + distX, gridGame[0].position.y , gridGame[i-1].position.z);
}
}
gridGame[i].rotation = gridGame[0].rotation;
}
}
with this the grid of objects is formed but only in taking the world space
picture of how you are at this moment
I'm not sure if this will help, but you can turn the grid into a child object and leave the worldPositionStays as false, to do so use the SetParent(Transform parent, bool worldPositionStays). For example as a reference for your script.
private void createColum()
{
for (int i =0; i<gridGame.Count;i++)
{
if(gridGame[i] == gridGame[0])
{
gridGame[i].localPosition = gridGame[0].localPosition;
gridGame[i].transform.SetParent(this.transform, false);
}
if(gridGame[i]!= gridGame[0])
{
gridGame[i].localPosition = new Vector3(gridGame[i-1].localPosition.x + distX,gridGame[0].localPosition.y, gridGame[0].localPosition.z);
}
if(i +1 > limiteFila)
{
if(i%limiteFila==0)
{
gridGame[i].localPosition = new Vector3(gridGame[0].position.x, gridGame[0].position.y , gridGame[i-Mathf.RoundToInt(limiteFila)].position.z + distY);
}else
{
gridGame[i].position = new Vector3(gridGame[i-1].position.x + distX, gridGame[0].position.y , gridGame[i-1].position.z);
}
}
gridGame[i].rotation = gridGame[0].rotation;
}
}
Related
I'm creating a shop inside my game to buy virtual goods.
Main Shop Page
Each group has its own items. I've created a view where only one item is visible each time, with buttons to go to the previous and next item.
Specific group selection Menu - Teams
The question is that I don't know how to show the list where I have the items, in that last window and make that touching the buttons previous/next the list shows the different items.
Each item inside the list is called inventory[x] (x = the number in the loop) and has the properties itemName and itemDescription.
How can I make the code communicate with the graphical user interface?
I've created 6 inventory lists (each one for one type of unlockable virtual good) and I use GameSparks API for online features such a player profile.
The lists are declared at the beginning of the script:
public static List<Inventario> inventarioEquipos = new List<Inventario>(); //Inventory for Teams
public static List<Inventario> inventarioPelotas = new List<Inventario>(); //Inventory for Balls
public static List<Inventario> inventarioModos = new List<Inventario>(); //Inventory for Modes
public static List<Inventario> inventarioVitores = new List<Inventario>(); //Inventory for Cheers
public static List<Inventario> inventarioCampos = new List<Inventario>(); //Inventory for Fields
public static List<Inventario> inventarioFondos = new List<Inventario>(); //Inventory for Backgrounds
In the Awake method I call:
Info_Botones ("Equipos"); //I call this function for each group, to load its items and show how many items of a total of X the player owns. If you look at the first picture, will see below TEAMS: 1/2. That's what this function is for (besides to load all the unlockables in separated lists).
Info_Botones ("Pelotas");
Info_Botones ("Modos");
Info_Botones ("Vitores");
Info_Botones ("Campos");
Info_Botones ("Fondos");
The code of the main function is as follows:
private void Info_Botones(string tipoDeInventario) { //"tipoDeInventario" means InventoryType and I use it to pass to the function which type of inventory I want to load (Teams, Balls, Modes, Cheers, Fields or Backgrounds)
int numero = 0;
new LogEventRequest()
.SetEventKey("INVENTARIO")
.SetEventAttribute("TAG_TYPE", tipoDeInventario)
.Send((response) =>
{
if (!response.HasErrors)
{
List<object> entryList = response.ScriptData.GetObjectList("result") as List<object>;
for (int i = 0; i < entryList.Count; i++)
{
Dictionary<string, object> entry = entryList[i] as Dictionary<string, object>;
int itemId = i;
string itemName = (entry["name"]).ToString();
string itemDescription = (entry["description"]).ToString();
int itemPrice = int.Parse((entry["currency1Cost"].ToString()));
string itemInteractable = (entry["interactable"]).ToString();
Inventario inv = new Inventario(itemId, itemName, itemDescription, itemPrice, itemInteractable);
if(tipoDeInventario == "Equipos") {
inventarioEquipos.Add(inv);
if (inventarioEquipos[i].itemInteractable == "False") {
numero++;
}
} else if(tipoDeInventario == "Pelotas") {
inventarioPelotas.Add(inv);
if (inventarioPelotas[i].itemInteractable == "False") {
numero++;
}
} else if(tipoDeInventario == "Modos") {
inventarioModos.Add(inv);
if (inventarioModos[i].itemInteractable == "False") {
numero++;
}
} else if(tipoDeInventario == "Vitores") {
inventarioVitores.Add(inv);
if (inventarioVitores[i].itemInteractable == "False") {
numero++;
}
} else if(tipoDeInventario == "Campos") {
inventarioCampos.Add(inv);
if (inventarioCampos[i].itemInteractable == "False") {
numero++;
}
} else if(tipoDeInventario == "Fondos") {
inventarioFondos.Add(inv);
if (inventarioFondos[i].itemInteractable == "False") {
numero++;
}
} else {
}
}
if(tipoDeInventario == "Equipos") {
txtBtnCantidadEquipos.text = numero.ToString() + "/" + inventarioEquipos.Count;
} else if(tipoDeInventario == "Pelotas") {
txtBtnCantidadPelotas.text = numero.ToString() + "/" + inventarioPelotas.Count;
} else if(tipoDeInventario == "Modos") {
txtBtnCantidadModos.text = numero.ToString() + "/" + inventarioModos.Count;
} else if(tipoDeInventario == "Vitores") {
txtBtnCantidadVitores.text = numero.ToString() + "/" + inventarioVitores.Count;
} else if(tipoDeInventario == "Campos") {
txtBtnCantidadCampos.text = numero.ToString() + "/" + inventarioCampos.Count;
} else if(tipoDeInventario == "Fondos") {
txtBtnCantidadFondos.text = numero.ToString() + "/" + inventarioFondos.Count;
} else {
}
}
else
{
Debug.Log("ERROR AL OBTENER VG DEL JUGADOR: " + response.Errors.JSON);
}
});
}
I've achieved it using IF condictions. If anyone needs the code, ask for it and I will share it (I'm not at home right now).
Thanks!
I don't know, if StackOverflow is the right place to ask about Performance issues, but I haven't found any better community for this issue yet.
Basically we have two sample programs, one is an addin and one is a winforms program referencing the Word interop.
Both have implemented the same method called GetTabsFromParagraph:
public class SlowExample
{
public static void GetTabsFromParagraph(Paragraph para, Style style, List<Tabulator> tabList, bool getTabsForCase = false)
{
foreach (TabStop tab in para.TabStops)
{
if (tab.CustomTab)
{
bool showTab = true;
foreach (TabStop ts in style.ParagraphFormat.TabStops)
{
if (Math.Abs(ts.Position - tab.Position) < 0.001 &&
ts.Alignment == tab.Alignment)
{
showTab = false;
break;
}
}
if (showTab || getTabsForCase)
{
Tabulator t = new Tabulator
{
Tabulatorausrichtung =
tab.Alignment == WdTabAlignment.wdAlignTabLeft
? TabulatorAusrichtung.Links
: TabulatorAusrichtung.Rechts,
Tabulatorart = TabulatorArt.Tabulator,
Position = tab.Position
};
tabList.Add(t);
}
}
}
if (!getTabsForCase)
{
foreach (TabStop ts in style.ParagraphFormat.TabStops)
{
if (ts.CustomTab)
{
bool showTab = true;
foreach (TabStop tab in para.TabStops)
{
if (Math.Abs(tab.Position - ts.Position) > 0.0001 || tab.Alignment != ts.Alignment)
{
showTab = false;
break;
}
}
if (showTab)
{
Tabulator t = new Tabulator
{
Tabulatorausrichtung = TabulatorAusrichtung.Geloescht,
Tabulatorart = TabulatorArt.Tabulator,
Position = ts.Position
};
tabList.Add(t);
}
}
}
}
if (Math.Abs(para.LeftIndent - style.ParagraphFormat.LeftIndent) > 0.001 || getTabsForCase)
{
Tabulator t = new Tabulator
{
Tabulatorausrichtung = TabulatorAusrichtung.Links,
Tabulatorart = TabulatorArt.Einzug,
Position = para.LeftIndent
};
tabList.Add(t);
}
if (Math.Abs(para.RightIndent - style.ParagraphFormat.RightIndent) > 0.001 || getTabsForCase)
{
Tabulator t = new Tabulator
{
Tabulatorausrichtung = TabulatorAusrichtung.Rechts,
Tabulatorart = TabulatorArt.Einzug,
Position = para.RightIndent
};
tabList.Add(t);
}
if (Math.Abs(para.FirstLineIndent - style.ParagraphFormat.FirstLineIndent) > 0.001 || getTabsForCase)
{
Tabulator t = new Tabulator
{
Tabulatorausrichtung = TabulatorAusrichtung.ErstzeilenEinzug,
Tabulatorart = TabulatorArt.Einzug,
Position = para.FirstLineIndent
};
tabList.Add(t);
}
}
public class Tabulator
{
private TabulatorArt m_Tabulatorart;
private TabulatorAusrichtung m_Tabulatorausrichtung;
private float m_Position;
private bool m_UebernahmeInFolgedokument = false;
public float Position
{
get { return m_Position; }
set { m_Position = value; }
}
public float PositionOrg
{
get;
set;
}
public float PositionInCm
{
get
{
return (m_Position / 28.35F);
}
set
{
m_Position = value * 28.35F;
}
}
public TabulatorArt Tabulatorart
{
get { return m_Tabulatorart; }
set { m_Tabulatorart = value; }
}
public TabulatorAusrichtung Tabulatorausrichtung
{
get { return m_Tabulatorausrichtung; }
set { m_Tabulatorausrichtung = value; }
}
public TabulatorAusrichtung TabulatorausrichtungOrg
{
get;
set;
}
public bool UebernahmeInFolgedokument
{
get { return m_UebernahmeInFolgedokument; }
set { m_UebernahmeInFolgedokument = value; }
}
}
public enum TabulatorArt
{
Invalid = 0,
Tabulator = 1,
Einzug = 2
}
public enum TabulatorAusrichtung
{
Invalid = 0,
Links = 1,
Rechts = 2,
ErstzeilenEinzug = 3,
Geloescht = 4,
}
}
In each of the both programs, I load up a Application, open up a document with a few paragraphs and tabs and run this method for each paragraph like this:
private void TestSlowMethod(Word.Document document)
{
Word.Paragraphs documentParagraphs = document.Paragraphs;
List<Tabulator> tabList = new List<Tabulator>();
long swElapsedMilliseconds = 0;
foreach (Word.Paragraph documentParagraph in documentParagraphs)
{
Word.Style style = documentParagraph.get_Style();
Stopwatch sw = new Stopwatch();
sw.Start();
SlowExample.GetTabsFromParagraph(documentParagraph, style, tabList, true);
sw.Stop();
swElapsedMilliseconds += sw.ElapsedMilliseconds;
Debug.WriteLine(sw.ElapsedMilliseconds + "\r\n");
}
MessageBox.Show("Total ms: " + swElapsedMilliseconds);
Debug.WriteLine("Done...");
}
What I found out is the addin is running throw all of this 10-20 times faster.
Addin: 20-30 ms per call
WinForms tool: 200-300 ms per call
Why is that? My assumption would be, that the addin runs in the same context / process than the word application. But is that the real reason?
And can we fix that? Our software moved from addin to WPF + Interop-Word.
Thanks in advance!
Some performance fixes:
Check getTabsForCase sooner here:
if (tab.CustomTab)
{
bool showTab = true;
if (getTabsForCase) //insert this here, no need to run if getTabsForCase.
foreach (TabStop ts in style.ParagraphFormat.TabStops)
{
if (Math.Abs(ts.Position - tab.Position) < 0.001 &&
ts.Alignment == tab.Alignment)
{
showTab = false;
break;
}
}
if (showTab || getTabsForCase)
{
Tabulator t = new Tabulator
{
Tabulatorausrichtung =
tab.Alignment == WdTabAlignment.wdAlignTabLeft
? TabulatorAusrichtung.Links
: TabulatorAusrichtung.Rechts,
Tabulatorart = TabulatorArt.Tabulator,
Position = tab.Position
};
tabList.Add(t);
}
}
Similarly, put the check on getTabsForCase before all calculations in if statements:
//see getTabsForCase goes first
if (getTabsForCase || Math.Abs(para.LeftIndent - style.ParagraphFormat.LeftIndent) > 0.001)
Fix all of those conditionals to have getTabsForCase first - then the rest of the statement won't need to evaluate.
I am importing the same report 3 times. For each iteration, I am passing a different value, but the problem is that only the first imported report gets the value, the other two are blank. Here is my code:
foreach (var current in reportParameterList)
{
if (string.IsNullOrEmpty(current.SubreportName))
{
document.SetParameterValue(current.Name, current.Value);
}
else
{
document.SetParameterValue(current.Name, current.Value, current.SubreportName);
}
}
So, for the where is subreport name blank, that is for the main report, the else case is for my subreports. So, the same report, imported 3 times doesn't show the value on the second and third.
UPDATE:
The method for filling values for subreports:
for (int i = 0; i < _reportLayoutConfiguration.ReportLayout.Count; i++)
{
if (_reportLayoutConfiguration.ReportLayout[i].SubreportName == "SectionReportTest1.rpt")
{
SetSectionOneReportParameters(reportParameterList);
}
if (_reportLayoutConfiguration.ReportLayout[i].SubreportName == "SectionReportTest2.rpt")
{
SetSectionTwoReportParameters(reportParameterList);
}
if (_reportLayoutConfiguration.ReportLayout[i].SubreportName == "SectionReportTest3.rpt")
{
SetSectionThreeReportParameters(reportParameterList);
}
}
private void SetSectionOneReportParameters(List<ReportParameter> reportParameterList)
{
reportParameterList.Add(new ReportParameter() { Name = "SectionParameterID", Value = "ParameterOne_" + DateTime.Now.ToLongTimeString() + "", SubreportName = "SectionReportTest1.rpt" });
}
private void SetSectionTwoReportParameters(List<ReportParameter> reportParameterList)
{
reportParameterList.Add(new ReportParameter() { Name = "SectionParameterID", Value = "ParameterTwo_" + DateTime.Now.ToLongTimeString() + "", SubreportName = "SectionReportTest2.rpt" });
}
private void SetSectionThreeReportParameters(List<ReportParameter> reportParameterList)
{
reportParameterList.Add(new ReportParameter() { Name = "SectionParameterID", Value = "ParameterThree_" + DateTime.Now.ToLongTimeString() + "", SubreportName = "SectionReportTest3.rpt" });
}
Of course, this is for testing only.
Hey guys I'm currently working on a bank program for a class project. The idea the user will need to make an account if not done so already but if they already do they can just login using account number and pin. However. instead of my program constantly adding data to an array that's size is 100 it just replaces the data in slot [0] just wondering why.
public partial class Form1 : MetroForm
{
//For Creating new account
string newAccountType;
Accounts[] customers = new Accounts[99999];
int temp;
string VerifyPin = ("");
private void openAccount_Click(object sender, EventArgs e)
{
for (int index=0;index < customers.Length; ++index)
{
var R1 = new Random();
var R2 = new Random();
customers[index] = new Accounts();
customers[index].Name = newName.Text;
customers[index].accountType = newAccountType;
customers[index].accountNumber = (R1.Next(1000000,9000000))+(R2.Next(100,9000));
customers[index].accountPin = createPin.Text;
customers[index].accountBalance = 100.00;
temp = index;
}
MetroMessageBox.Show(this, "Thank you member "+customers[temp].Name+"\nYour member number is: "+customers[temp].accountNumber, "You are now a memeber", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
metroTabControl1.SelectedTab = metroTabPage2;
}
private void checkBalance_Click(object sender, EventArgs e)
{
int veri=0;
bool isfound = false;
for (int count = 0; count < customers.Length; ++count)
{
if (Convert.ToInt32(userName.Text) == customers[count].accountNumber)
{
veri = count;
isfound = true;
}
else
isfound = false;
accountnotfound.Text = "Account Not Found";
}
if (isfound && (customers[veri].accountPin == pinText.Text))
{
MetroMessageBox.Show(this, "account found", "account found");
}
else
{
MetroMessageBox.Show(this, "account not found or wrong pin", "account not found");
pinText.Text = "";
}
accountBalance.Visible = true;
userWithdraw.Visible = true;
userDeposite.Visible = true;
accountBalance.Text = "Welcome, "+customers[veri].Name+"\nYour current balance is: "+customers[veri].accountBalance;
}
public class Accounts
{
private string name, AccountType, AccountPin;
private int AccountNumber;
private double AccountBalance;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int accountNumber
{
get
{
return AccountNumber;
}
set
{
AccountNumber = value;
}
}
public string accountPin
{
get
{
return AccountPin;
}
set
{
AccountPin = value;
}
}
public string accountType
{
get
{
return AccountType;
}
set
{
AccountType = value;
}
}
public double accountBalance
{
get
{
return AccountBalance;
}
set
{
AccountBalance = value;
}
}
}
I think you just need to keep track of which element in the array is available (i.e. has a null value). Once no elements in the array are null, this means that the customers array is full, so you can't add any more customers at that point.
Make these following changes. In the part of the code just where the code starts the loop:
bool added = false;
for (int index=0;index < customers.Length; ++index)
{
if (customers[index] != null) continue;
...
Also, after you actually add a customer, do this (right after the loop ends):
if (!added)
{
// show error message here!
}
else
{
MetroMessageBox.Show(this, "Thank you member "+customers[temp].Name+"\nYour member number is: "+customers[temp].accountNumber, "You are now a memeber", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
metroTabControl1.SelectedTab = metroTabPage2;
}
for (int index=0;index < customers.Length; ++index)
customers[index] = new Accounts();
Index will be 0 every time your array loops, thus replacing customers[0] every time you openAccount_Click is called.
You don't really need to loop to add. You just need to make sure you are writing the next possible index. Maybe use your Temp variable and increment it + 1 for every user added and then without a loop do like do.
customers[Temp] = new Accounts();
customers[Temp].Name = newName.Text;
customers[Temp].accountType = newAccountType;
customers[Temp].accountNumber = (R1.Next(1000000,9000000))+ (R2.Next(100,9000));
customers[Temp].accountPin = createPin.Text;
customers[Temp].accountBalance = 100.00;
Temp += 1;
You would be better off using a List of Accounts for your customers global instead.
List<Accounts> customers = new List<Accounts>();
Then inside openAccount_Click you can add a new Accounts like so.
customers.Add(new Accounts {
Name = newName.Text,
accountType = newAccountType,
accountNumber = (R1.Next(1000000,9000000))+(R2.Next(100,9000)),
accountPin = createPin.Text,
accountBalance = 100.00
});
bool added = false;
for (int x = 0; x <= temp; x++)
{
if (customers[temp] != null) continue;
{
for (int indexies = 0; indexies < customers.Length; indexies++)
{
var R1 = new Random();
var R2 = new Random();
Convert.ToInt32(createPin.Text);
customers[temp] = new Accounts();
customers[temp].Name = newName.Text;
customers[temp].accountType = newAccountType;
customers[temp].accountNumber = 1;//(R1.Next(1000000, 9000000)) + (R2.Next(100, 9000));
customers[temp].accountPin = Convert.ToInt32(createPin.Text);
customers[temp].accountBalance = 100.00;
added = true;
}
}
}
I am currently working/experimenting with lists. I am able to determine with function GetNextTree the position of each item in my list such as: First, Next and Last. I have an already made list from an array ax but now I am trying to implement an insert button that will take values tree_type, tree_height, tree_price, tree_instock and create the item. Since I can point anywhere in my list, the insert will be intended to add an item after the currently pointed to item. That is where my question is: How can I add a new item after the currently pointed to item?
public class fruit_trees
{
private string tree_type = " ";
private int tree_height = 0;
public double tree_price = 0;
private int tree_instock = 0;
public fruit_trees next_tree;
public fruit_trees(string newtree, int newheight, double newprice, int newinstock)
{
tree_type = newtree;
tree_height = newheight;
tree_price = newprice;
tree_instock = newinstock;
next_tree = null;
}
public string GetTreeType
{
get { return tree_type;
}
}
public override string ToString()
{
return tree_type + " " + tree_height + " " + tree_price + " " + tree_instock;
}
}
public class ListForTrees
{
private fruit_trees first_tree;
public fruit_trees First_tree
{
get
{
return first_tree;
}
}
public fruit_trees last_tree;
public int current;
public int count = 0;
public ListForTrees(fruit_trees new_tree)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
current = 0;
}
public ListForTrees(IEnumerable trees)
{
current = 0;
foreach (fruit_trees t in trees)
{
this.AddTree(t);
}
}
public fruit_trees GetNextTree()
{
//current = 0;
fruit_trees ft = first_tree;
if (current == count)
{
current = 0;
}
int i = 0;
while (i != current)
{
ft = ft.next_tree;
i++;
}
return ft;
}
}
ListForTrees mainlist = new ListForTrees();
private void BtnInsertTree_Click(object sender, EventArgs e)
{
try
{
int height = Convert.ToInt32(TxtTreeHeight.Text);
int stock = Convert.ToInt32(TxtTreeStock.Text);
double price = Convert.ToDouble(TxtTreePrice.Text);
fruit_trees treeadd = new fruit_trees(TxtTreeName.Text, height, price, stock);
mainlist.AddTree(treeadd);
}
catch
{
MessageBox.Show("Please check intput fields");
}
}
private void BtnGo_Click(object sender, EventArgs e)
{
fruit_trees[] ax = { new fruit_trees("cherry", 48, 12.95, 3),
new fruit_trees("pine", 36, 9.95, 8),
new fruit_trees("oak", 60, 14.95, 2),
new fruit_trees("peach", 54, 19.95, 3),
new fruit_trees("pear", 36, 11.85, 2),
new fruit_trees("apple", 62, 13.45, 5)
};
mainlist = new ListForTrees(ax);
fruit_trees current = mainlist.First_tree;
while (current != null)
{
TxtOutput.AppendText(current.ToString() + Environment.NewLine);
current = current.next_tree;
}
}
private void ShowFirstItem_Click(object sender, EventArgs e)
{
//Show Next Item
labelSpecificTree.Text = mainlist.First_tree.GetTreeType;
}
private void ShowNextItem_Click(object sender, EventArgs e)
{
fruit_trees obj = mainlist.GetNextTree();
if (obj.next_tree == null)
{
labelSpecificTree.Text = mainlist.First_tree.GetTreeType.ToString();
}
else
{
mainlist.current++;
labelSpecificTree.Text = obj.next_tree.GetTreeType.ToString();
}
}
private void ShowLastItem_Click(object sender, EventArgs e)
{
// Show First Item
labelSpecificTree.Text = mainlist.last_tree.GetTreeType;
}
So I'm slightly confused by what's going on here. You're implementing your own ArrayList like class which is not really a list at all... I don't really see the point of doing such things in C# but alright... Here's the method;
fruit_trees[] InsertNode(fruit_trees current, fruit_trees nNode)
{
fruit_trees[] temp = new fruit_trees[ax.Length + 1];
int i = 0;
while (ax[i] != current)
{
temp[i] = ax[i];
i++;
}
temp[i+1] = nNode;
while (i < ax.Length)
{
temp[i+1] = ax[i];
}
return temp;
}
call this like such;
ax = InsertNode(current, nNode);
What we're doing is making a new list with one extra slot, copying each fruit_tree over til we hit the node we want to insert after, then we copy the new tree in. After that we copy over the rest of the old array. Then we return the new array.