C
C#13mo ago
ansy

cookie clicker store

Hello i am here ones again asking for help. My store buttons need to be dissabled if i don't have te right amount of cookies and enable when i have the right amount or more. I already have a method for isolating the number from the text in my textblock. The problem i have been having is that when i get my store too take away cookies the counting of the cookies just resumes from the last number that was on before the subtraction of the number. example: i have 18 cookies and i pay for the clicker worth 15 cookies. It subtracts and it sais 3 cookies, but when i click on the cookie again it jumps to 19 cookies all of a sudden. Another problem i have been having is the button disabling it doesn't wanna work however hard i try. Can someone help me i really need this to work. here is the repository link;https://bitbucket.org/pxlrepository/c-project-cookie-clicker/src/master/ I work in a WPF in a .net framework in visual studios If anyone can help it would be much appreciated
113 Replies
ansy
ansyOP13mo ago
I feel like i am just stupid but idk how i need to do this otherwise
Buddy
Buddy13mo ago
Note, that people usually do not bother searching through an entire repository to help If you can please use the site below and paste in the relevant code.
MODiX
MODiX13mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
ansy
ansyOP13mo ago
Ah thx sorry
ansy
ansyOP13mo ago
BlazeBin - kxewswlgsfsk
A tool for sharing your source code with the world!
ansy
ansyOP13mo ago
this right?
Buddy
Buddy13mo ago
If that is the code that causes issues, then yes.
ansy
ansyOP13mo ago
it is i also put the code for the buttons
Buddy
Buddy13mo ago
Don't base your entire game logic on the text of the GUI but store the game data inside of a class. In your case, it's a double
ansy
ansyOP13mo ago
oh?
Buddy
Buddy13mo ago
The GUI should just respond to events called by the game logic. Just as an example you can have an event like CookiesChanged then you can subscribe to that event and update your GUI based on that specific event
public class MyGameData
{
public event EventHandler<double> CookiesChanged;

private double _cookies;

public double Cookies
{
get => _cookies;
set
{
_cookies = value;
// You can also check if value and cookies are different here to prevent CookiesChanged from updating even if it's the same value.
CookiesChanged?.Invoke(value);
}
}
}
public class MyGameData
{
public event EventHandler<double> CookiesChanged;

private double _cookies;

public double Cookies
{
get => _cookies;
set
{
_cookies = value;
// You can also check if value and cookies are different here to prevent CookiesChanged from updating even if it's the same value.
CookiesChanged?.Invoke(value);
}
}
}
As a quick example So whenever Cookies property value is set, it will set the backing field value _cookies to the value and then it will call CookiesChanged event So the GUI can just subscribe to said event then you have your text ready to go without any issues.
ansy
ansyOP13mo ago
i am sorry i am trying to comprehend this its late for me. If i get it right is that i am trying to do too much in one GUI and i should split it up? and need another GUI to keep track of the number of cookies i have? if i am wrong please bare with me i am feeling like an idiot rn
Buddy
Buddy13mo ago
No. Do not store / use information about the game in the UI, but in the code underneath the GUI You at the moment use the text of the GUI to see how many "cookies" you have. This is what you should not do, use the GUI as a way to store information.
ansy
ansyOP13mo ago
i am sorry for being an idiot would you mind maybe hop in a call tomorrow so you can instruct me a bit if your up for that i get it if you don"t want to i been at it for 6 hours with no progress.
Buddy
Buddy13mo ago
I do not help by calls, I do however help by text. In this server
ansy
ansyOP13mo ago
it was worth a shot i am just very green let us say i started programming in september in a university. we just learned about arrays
Buddy
Buddy13mo ago
No problem, programming is a big subject. Especially the do's and don'ts, how to structure the project, and so forth. Basically, keep the game logic out of the GUI (Graphical User Interface)
ansy
ansyOP13mo ago
school expects us to make full on cookie clicker clone with our limited knowledge we have its just frustrating cause it feels like when i make some kind of progress i take 10 steps back
Buddy
Buddy13mo ago
I know how it feels. Although when I joined the programming course I already had about 7 years of programming experience. But my pals didn't.
ansy
ansyOP13mo ago
yeah to be honest i am feeling i am wasting people time asking for help with stuff i barely comprehend
Buddy
Buddy13mo ago
But that is the way to structure any kind of project, to keep it out of the graphical interface. Keep the code in the backend. The GUI should only respond to events being called by the code in the backend No you are not! People that helps here actually likes to help, you are not wasting anyones time!
ansy
ansyOP13mo ago
so the button event are GUI right?
Buddy
Buddy13mo ago
Anything that you see on your screen (window) is GUI
ansy
ansyOP13mo ago
oh ok so litterly every event i made is GUI
Buddy
Buddy13mo ago
The events are not GUI, they are just events being called by the GUI such as the button click event
ansy
ansyOP13mo ago
oh ok gui is just all the things in my window
ansy
ansyOP13mo ago
like this
No description
Buddy
Buddy13mo ago
You can start like this
public class MyGameData
{
public static double MyCookies { get; set; }
}
public class MyGameData
{
public static double MyCookies { get; set; }
}
$static
MODiX
MODiX13mo ago
In C#, static allows you to have members (classes, methods, etc) that are not tied to any particular instance and are therefore always, globally, accessible. When applying static members, take the following considerations: • If there are to be multiple instances of a class, do not use static • If you need to track state, do not use static • If you are going to have multi threaded workflows, do not use static unless you're aware of the caveats static is best used for stateless methods, extension methods/classes and in areas where you understand the pros/cons. Read more here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static
Buddy
Buddy13mo ago
Then you can just do MyGameData.MyCookies += 1; Which will increment MyCookies by 1 static meaning it is globally accessible and not tied to any instance, meaning you don't have to call new and pass said instance around. So you can just do MyClassName.MyStaticVariable and not
MyClassName myName = new MyClassName();
myName.MyCookies += 1;
MyClassName myName = new MyClassName();
myName.MyCookies += 1;
ansy
ansyOP13mo ago
ok and if i make that i should bind MyCookie too my mouse down event?
Buddy
Buddy13mo ago
Subscribe to the click event, yes. and then increment the MyCookies by one. You won't see anything in the GUI, but it will increment in the back-end. After that you just have to hook up some code that sees the change and sends back a response to the GUI.
ansy
ansyOP13mo ago
ok so if i get it right i need to make "public static double cookies ( get; set;)"
Buddy
Buddy13mo ago
wrong brackets, but yes.
ansy
ansyOP13mo ago
oh yeah
MODiX
MODiX13mo 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/
ansy
ansyOP13mo ago
private static double MyCookies
{
get;
set;
}
private static double MyCookies
{
get;
set;
}
so like this? now followup question the code you set here should it be in the same method or no?
Buddy
Buddy13mo ago
You should not do it the way I wrote what you replied to but this above https://discord.com/channels/143867839282020352/1177729264540459128/1177748113662955641
MODiX
MODiX13mo ago
Buddy
You can start like this
public class MyGameData
{
public static double MyCookies { get; set; }
}
public class MyGameData
{
public static double MyCookies { get; set; }
}
Quoted by
<@203166497198047232> from #cookie clicker store (click here)
React with ❌ to remove this embed.
Buddy
Buddy13mo ago
A static property / field is a good start because you don't have to mess around with passing the instance between everything.
ansy
ansyOP13mo ago
oh fuck i feel stupid i thought public class was the same as public window omg
Buddy
Buddy13mo ago
You can have it anywhere you'd like but I would recommend to dedicate it's own class for it. Somewhere you'll know where it is.
ansy
ansyOP13mo ago
well we never learned about extra files yet we only learned to work with the .xaml file and the .xaml.cs file so i need to make a public class to do the cookies counting if i am right and to keep track of it?
Buddy
Buddy13mo ago
Yes. Next step for you is to make an event Don't worry, this will guide you. https://learn.microsoft.com/en-us/dotnet/csharp/events-overview As long as you like to read 😛
ansy
ansyOP13mo ago
public class MyCookieData
{
public static double MyCookies {get; set;}
}
public class MyCookieData
{
public static double MyCookies {get; set;}
}
like this? or the get set are place holders?
Buddy
Buddy13mo ago
For now, yes.
ansy
ansyOP13mo ago
ok imma add that then so that has been added in my code
Buddy
Buddy13mo ago
Next step is to create a backing field, you can do that by creating a field static double _myCookies; in the same class as you just created, note that you should not declare it as public as you don't want anyone to access that field but that specific class itself. MyCookies property only acts as a "middle-man" or a proxy. GUI -> Button Click -> MyCookies + 1 -> _myCookies -> CookiesChanged -> (callback) OnCookiesChanged -> (GUI) Update GUI text
ansy
ansyOP13mo ago
well i need to bind it too my image mouse down event so i thing it should be in the mouse down event then?
Buddy
Buddy13mo ago
I see, yes. You need to do that. You should only increment it in the mouse down (click) event, yes.
ansy
ansyOP13mo ago
ok this is doing my school project so with the mouse event i used this
private void ResizeCookie(double factorVergroting)
{
//berekend de nieuwe grote
double newWidth = originalWidth * factorVergroting;
double newHeight = originalHeight * factorVergroting;

//past de nieuwe groote toe
ImageBtnCookie.Width = newWidth;
ImageBtnCookie.Height = newHeight;
}


private void ImageBtnCookie_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isMouseDown = true;

ResizeCookie(0.9);
}
private void ResizeCookie(double factorVergroting)
{
//berekend de nieuwe grote
double newWidth = originalWidth * factorVergroting;
double newHeight = originalHeight * factorVergroting;

//past de nieuwe groote toe
ImageBtnCookie.Width = newWidth;
ImageBtnCookie.Height = newHeight;
}


private void ImageBtnCookie_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isMouseDown = true;

ResizeCookie(0.9);
}
it needs to resize too so i put that with it too
Buddy
Buddy13mo ago
Now, how do you access the class that you just wrote and it's property?
ansy
ansyOP13mo ago
rn its under my public MainWindow i feel like i did wrong
Buddy
Buddy13mo ago
Feel free to post code $paste
MODiX
MODiX13mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Buddy
Buddy13mo ago
It's also good to keep the helpers updated in what you are doing by pasting the code from time to time.
ansy
ansyOP13mo ago
a shit i shall show you the whole thing
ansy
ansyOP13mo ago
BlazeBin - yvthqujplsrw
A tool for sharing your source code with the world!
ansy
ansyOP13mo ago
so i have almost 200 lines of code what you just saw was the resizing method and the mouse down event
Buddy
Buddy13mo ago
Yeah, you do not want it inside of another class in this case, also known as a nested class.
ansy
ansyOP13mo ago
you mean the public class?
Buddy
Buddy13mo ago
Get it out of the class for the window (MainWindow)
ansy
ansyOP13mo ago
ok did that
Buddy
Buddy13mo ago
How do you think you can access the property inside of the newly created class?
ansy
ansyOP13mo ago
so uhm do i need to make another .cs file?
Buddy
Buddy13mo ago
You don't need to, no.
ansy
ansyOP13mo ago
idk then never made a new class before
Buddy
Buddy13mo ago
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static Read this Since this happens to be homework I will not tell you, but I will give hints.
ansy
ansyOP13mo ago
its a project worth 60% so yeah my question did i place my class wrong
Buddy
Buddy13mo ago
In the previous code snippet, yes.
ansy
ansyOP13mo ago
fuck do i need to place it above the MainWidow?
Buddy
Buddy13mo ago
If you don't want nested classes, then yes.
ansy
ansyOP13mo ago
which is the purpose of what we are doing rn
Buddy
Buddy13mo ago
Removing the nested classes?
ansy
ansyOP13mo ago
so it doesn't become nested is what i think your getting at right?
ansy
ansyOP13mo ago
BlazeBin - cskczzzqtqvc
A tool for sharing your source code with the world!
Buddy
Buddy13mo ago
It is still inside of the class Also those images won't be displayed for the end-user in the XAML since it is a path that only exist on your drive, it will look for the file on the drive, it isn't embedded into the app unless you specify that it is.
ansy
ansyOP13mo ago
i thought i had those images in the assets folder of the project
Buddy
Buddy13mo ago
You do, but you specify the full path
ansy
ansyOP13mo ago
ah i need to change that to assets/name.jpg i presume?
Buddy
Buddy13mo ago
Try it and see
ansy
ansyOP13mo ago
no errors so it worked so the public class should still be under the namespace yes just out of the mainwindow?
Buddy
Buddy13mo ago
Try to run the app as well
ansy
ansyOP13mo ago
BlazeBin - poojxzmymrtx
A tool for sharing your source code with the world!
Buddy
Buddy13mo ago
Great You still kept the old class (possible duplicate) :d
ansy
ansyOP13mo ago
wait huh what you mean oop god i am blind this didn't happen in my visual studio also for some reason every image show besides the last one i have everything the same too
Buddy
Buddy13mo ago
That's what I expected
ansy
ansyOP13mo ago
how every picture is in assets
Buddy
Buddy13mo ago
Show me your solution explorer in Visual Studio
ansy
ansyOP13mo ago
here
No description
Buddy
Buddy13mo ago
You need to mark those images as Resource by going into their properties
ansy
ansyOP13mo ago
ok in properties
Buddy
Buddy13mo ago
From visual studio that is
ansy
ansyOP13mo ago
hmh mhm rn in catigorized
ansy
ansyOP13mo ago
No description
Buddy
Buddy13mo ago
Alright, then you should be able to define it's relative path for that specific image. and it should show up
ansy
ansyOP13mo ago
in full path? or is there an option i aint seeing
ansy
ansyOP13mo ago
or is this the one you meant
No description
Buddy
Buddy13mo ago
no, not full. Relative. You don't need to set an option Either way I need to get going, it's 1:30 AM here. Try your best, read the links I sent previously and the code.
ansy
ansyOP13mo ago
oh your live in CET? same here
Buddy
Buddy13mo ago
Yes.
ansy
ansyOP13mo ago
i should sleep too haha ^^' managed too put all files too embedded resource
Buddy
Buddy13mo ago
Make sure they are marked as a Resource
ansy
ansyOP13mo ago
so defined the relative path as Assets/nameImage.png and set the build action to resource https://paste.mod.gg/dgpnqgdwnnlv/1
BlazeBin - dgpnqgdwnnlv
A tool for sharing your source code with the world!
ansy
ansyOP13mo ago
i still don't get the last picture to show but imma look into it found the damn problem my button wasn't big enough for the image
ansy
ansyOP13mo ago
changed height of the BtnMi too 63 https://paste.mod.gg/cnpeauepgwja/1
BlazeBin - cnpeauepgwja
A tool for sharing your source code with the world!
ansy
ansyOP13mo ago
cleaned up unneeded code in my methods https://paste.mod.gg/krxhtbssvetb/0
BlazeBin - krxhtbssvetb
A tool for sharing your source code with the world!
ansy
ansyOP13mo ago
BlazeBin - krxhtbssvetb
A tool for sharing your source code with the world!
ansy
ansyOP13mo ago
@Networking is🎄save me pls sorry i tried my best i read over and over i am just to fucking stupid to get it idk what to do sorry but vague pointers will not further anyhing
Omnissiah
Omnissiah13mo ago
dude, don't do this a thousand times
else if (welkeKnop.Name.Substring(welkeKnop.Name.Length - 2)
else if (welkeKnop.Name.Substring(welkeKnop.Name.Length - 2)
just create a variable and calculate the substring in it if some did this in my code
return double.Parse(nummerDeel);
return double.Parse(nummerDeel);
i would tell him to change it and pass the culture param, probably InvariantCulture also
Regex.Match(text, @"\d+")
Regex.Match(text, @"\d+")
create a compiled regex instead of using the free regex.match last but not least put strings like "Cl", "Gr", "Mi", and so on in constants or in their own class or somewhere else
ansy
ansyOP13mo ago
I got it all fixed thx for all the help and learning opportunities
Want results from more Discord servers?
Add your server