C
C#11mo ago
Erek Hai

Please Help me. I spent 3 days trying to fix it to no avail.

I am very new to coding and this is my first project. Trying to fix it but I couldnt manage it. https://github.com/merenbal/TrenchesOfGallipoli This is the whole project so you can inspect whatever you want. Main problem is bullets are supposed to be removed with the enemy they hit. But most of the times bullets kill more than 1 and get removed at the location based remover. When there is lots of objects in the screen and game gets slower bullets and removed like they should but whenever game gets faster it returns back to killing more than 1. I tried matching speed and interval to perfectly divisible Numbers but that didnt help. I uploaded pictures of all the actions lead to deletion of the enemy and bullet plus their speed and timer interval. If youre kind enough to help me fix it you can add me on Discord too, as soon as problem is solved you can block me I wont disturb you again. Thank you for at least reading it.
GitHub
GitHub - merenbal/TrenchesOfGallipoli
Contribute to merenbal/TrenchesOfGallipoli development by creating an account on GitHub.
50 Replies
Stefanidze
Stefanidze11mo ago
Hello. What does foreach (Control in this.Controls){//...} list? It looks like optimization problem to me. Also, i don't know how you call these events, but it seems to me that bullet killing enemy and bullet getting removed are executed at the same time which is causing the issue. Consider adding async elements and awaiting execution of a bullet deletion before executing anything else? BTW, what project type are you working with? I's not exactly clear from the first look at the repository.
Erek Hai
Erek HaiOP11mo ago
Windows forms app (.net framework) c#
Stefanidze
Stefanidze11mo ago
why are you trying to make a game in winforms? Not to say it's wrong, but there are much easier to work with solutions, like godot or unity.
Erek Hai
Erek HaiOP11mo ago
Our professor of this class made it mandatory to make game in winforms He didn’t teach us shit, just sent MOOICTs c# winforms game tutorials and said make a game for your final project All the code is made with stuff I learned from reverse engineering MOOICTs projects
Stefanidze
Stefanidze11mo ago
So you didn't have any experience with c# before this game?
Erek Hai
Erek HaiOP11mo ago
Yep, just creating some very very basic console apps such as calculating if student passed or not by summing and dividing his notes I’m working more than 6 hours a day for more than 1.5 week just to create assets, music, learning how to code etc But at this stage I’m stuck with this problem, people on Reddit come up with some problem of this code but all they are pointing out is complex for my level and I don’t know how to implement/fix it
Erek Hai
Erek HaiOP11mo ago
Thread on Reddit you can check it
Stefanidze
Stefanidze11mo ago
Ok, so what exactly is confusing for you here? First thing i can point out: Do not remove elements from the array in foreach do for (<variable>, <= Array.Length, <increment>)
Erek Hai
Erek HaiOP11mo ago
Soldiersbullet is not in an array, how can I put them in array ? Makebullet is in different class and shoot bullet is in different class List<PictureBox> BulletList = new List<PictureBox>(); but I cant find which element I should add inside BulletList.Add(THIS)
Stefanidze
Stefanidze11mo ago
Problem is this.Controls.Remove(x); in foreach( Control x in this.Controls) I assume this.Controls is IEnumerable
Erek Hai
Erek HaiOP11mo ago
I will get rid of foreaches and turn them into for loops but unless I put bullets into bullet list I cant make the for loop work as foreach right ?
Stefanidze
Stefanidze11mo ago
If you can put it in foreach you can make it work absolutely the same in for assuming it supports numerical iteration
Erek Hai
Erek HaiOP11mo ago
I dont know if the thing I say is stupid, sorry if they are
Stefanidze
Stefanidze11mo ago
Things you say are not stupid, we all begin somewhere
Erek Hai
Erek HaiOP11mo ago
OH I GOT IT THIS FIXED IT MY GOD
Stefanidze
Stefanidze11mo ago
well, don't thank me, thank the redditors it was the proposed solution
Erek Hai
Erek HaiOP11mo ago
you cared about it, tried to help thats the most important part for me, thank you very much too
Stefanidze
Stefanidze11mo ago
you're welcome glad you managed to fix it
Ninja Kirby
Ninja Kirby11mo ago
this isnt exactly to help but more of a performance / i guess readability question... why is this inside the initial loop? ooooh nevermind
Erek Hai
Erek HaiOP11mo ago
Hi. It's a bit hard writing code over chat, so I wrote down how I meant it in a dotnetfiddle: https://dotnetfiddle.net/LmkE7i
foreachcontrols | C# Online Compiler | .NET Fiddle
foreachcontrols | Test your C# code online with .NET Fiddle code editor.
Erek Hai
Erek HaiOP11mo ago
Instead of removing the Control x inside the foreach, which will break the foreach itself, you gather up the controls and then remove them after. This is what the pdnagilum sent me.
Ninja Kirby
Ninja Kirby11mo ago
no wait i think you should move that little snippet outside the loop i dont think you want it to run for every control especially when the code has no use for any captured variables within said loop
Erek Hai
Erek HaiOP11mo ago
oh right
Ninja Kirby
Ninja Kirby11mo ago
it could bog down performance is all also i was thinking of suggesting that but i was kinda scared of being called stupid and inefficient 💀
Erek Hai
Erek HaiOP11mo ago
like this right ?
Ninja Kirby
Ninja Kirby11mo ago
indeed also if you dislike those brackets taking up 3 spaces for 1 line you can omit them SPECIFICALLY IF there's only one line of code idk about how people see that readability wise though
Erek Hai
Erek HaiOP11mo ago
omit them ? }}}} like this ?
Ninja Kirby
Ninja Kirby11mo ago
in example:
if (true)
{
Console.WriteLine("Hello!!!");
}

// is equal to

if (true)
Console.Writeline("Hello!!!");
if (true)
{
Console.WriteLine("Hello!!!");
}

// is equal to

if (true)
Console.Writeline("Hello!!!");
Erek Hai
Erek HaiOP11mo ago
really ?
Ninja Kirby
Ninja Kirby11mo ago
yea idk it may be compiler specific but seeing as you are using visual studio as well i think it works
Erek Hai
Erek HaiOP11mo ago
how to program is gonna know where to stop ? tab spacing ?
Ninja Kirby
Ninja Kirby11mo ago
it only works with 1 line of code
Erek Hai
Erek HaiOP11mo ago
oh ok
Ninja Kirby
Ninja Kirby11mo ago
after that its considered out of the condition i believe
Stefanidze
Stefanidze11mo ago
i don't think so
Ninja Kirby
Ninja Kirby11mo ago
it compiles so idk
Stefanidze
Stefanidze11mo ago
Isn't that an error?
Ninja Kirby
Ninja Kirby11mo ago
it doesnt know that console is an available class despite me using system but as you can see it still outputs hi showing that the code works properly
Stefanidze
Stefanidze11mo ago
Well, better use {} for readability
Ninja Kirby
Ninja Kirby11mo ago
i guess i just enjoy it more than if { }
Stefanidze
Stefanidze11mo ago
Also, @Erek Hai, there is your original error in foreach(Control in ControlsToDelete). This time iteration is not important but it can hurt performance. do
for (int i; i <= controlsToDelete.Length(); i++)
{
this.Controls.Remove(controlsToRemove[i]);
EnemyList.Remove((PictureBox)controlsToRemove[i]);
}
for (int i; i <= controlsToDelete.Length(); i++)
{
this.Controls.Remove(controlsToRemove[i]);
EnemyList.Remove((PictureBox)controlsToRemove[i]);
}
Ninja Kirby
Ninja Kirby11mo ago
controlsToDelete.Length you mean? or is it an implicit operation? also doesnt using <= cause off by 1 since length is always the amount of elements (because it starts at 0) + 1?
Stefanidze
Stefanidze11mo ago
Thanks for pointing out my error! no, i don't think this will cause offset
Ninja Kirby
Ninja Kirby11mo ago
also isnt length a property and not a method in lists at least
Stefanidze
Stefanidze11mo ago
depends on the IEnumerable i think
Ninja Kirby
Ninja Kirby11mo ago
apparently Length is for arrays and Count is for lists
Sir Rufo
Sir Rufo11mo ago
$code
MODiX
MODiX11mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Want results from more Discord servers?
Add your server