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
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.Windows forms app (.net framework) c#
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.
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
So you didn't have any experience with c# before this game?
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
Reddit
From the csharp community on Reddit: Please Help me. I spent 3 days...
Explore this post and more from the csharp community
Thread on Reddit you can check it
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>)
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)
Problem is
this.Controls.Remove(x);
in foreach( Control x in this.Controls)
I assume this.Controls
is IEnumerableI 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 ?
If you can put it in foreach you can make it work absolutely the same in for assuming it supports numerical iteration
I dont know if the thing I say is stupid, sorry if they are
Things you say are not stupid, we all begin somewhere
OH
I GOT IT
THIS FIXED IT
MY GOD
well, don't thank me, thank the redditors
it was the proposed solution
you cared about it, tried to help
thats the most important part for me, thank you very much too
you're welcome
glad you managed to fix it
this isnt exactly to help but more of a performance / i guess readability question... why is this
inside the initial loop?
ooooh nevermind
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.
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.
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
oh right
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 💀
like this right ?
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
omit them ?
}}}} like this ?
in example:
really ?
yea
idk it may be compiler specific but seeing as you are using visual studio as well i think it works
how to program is gonna know where to stop ?
tab spacing ?
it only works with 1 line of code
oh ok
after that its considered out of the condition i believe
i don't think so
it compiles so idk
Isn't that an error?
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
Well, better use
{}
for readabilityi guess i just enjoy it more than
if { }
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
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?
Thanks for pointing out my error! no, i don't think this will cause offset
also isnt length a property and not a method
in lists at least
depends on the IEnumerable i think
apparently Length is for arrays and Count is for lists
List.Count Property (System.Collections.Generic)
Gets the number of elements contained in the List.
$code
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/