• What Do You Need Help With? V6
    7,544 replies, posted
Okay, first off, I'd like to state I'm pretty new to programming. The project I've been working on is Duck Hunt. Basically, I have some small things done. What I'm trying to figure out now is when I click/press a certain button, the gun fires (decreasing the ammo amount by 1). I have that when I press the 'p' key that it decreases the ammo amount, but how would I get it so it does it every time I hit the key? Here's the code: [code] if(start == true) { ammo = 3; if (keyboard.IsKeyDown(Keys.P)) { gunShot = true; } if (gunShot == true) { ammo--; } } [/code] I'm using Microsoft Visual Studio 2010, with XNA 4.0.
You don't need to create another if statement just for decreasing the ammo. Also, put the -- before the integer so the compiler evaluates the value first. [code]if(start == true) { ammo = 3; if (keyboard.IsKeyDown(Keys.P)) { gunShot = true; --ammo; } }[/code] Hope this helps. Edit: Ignore my code, follow Zeeky's instructions instead. My brain's melted.
Use a loop, e.g. [url=http://msdn.microsoft.com/en-us/library/2aeyhxcd(v=vs.100).aspx]while[/url]. Further more, if(start == true) is redundant, just do if(start). [code]if(a) { b = true; } if(b) { ... }[/code] Would also much simpler just be [code]if(a) { ... }[/code] without the need of another bool b (or in your case 'gunShot'). And your spacing is inconsistent: On the first if there's no space, on the following two there is one between the keyword and the parenthesis.
[QUOTE=ZeekyHBomb;42255579]Use a loop, e.g. [url=http://msdn.microsoft.com/en-us/library/2aeyhxcd(v=vs.100).aspx]while[/url]. Further more, if(start == true) is redundant, just do if(start). [code]if(a) { b = true; } if(b) { ... }[/code] Would also much simpler just be [code]if(a) { ... }[/code] without the need of another bool b (or in your case 'gunShot'). And your spacing is inconsistent: On the first if there's no space, on the following two there is one between the keyword and the parenthesis.[/QUOTE] If I try your second suggestion, all it does is bring the ammo down by one, but then when I let go of P, it goes back up.
What's the code you have now?
Can someone help me with my Linked List? I'm getting these errors: In member function 'void LinkedList::addFirst(std::string)':| error: conversion from 'Node*' to non-scalar type 'Node' requested| error: cannot convert 'Node' to 'Node*' in assignment| error: 'next' was not declared in this scope| error: 'head' was not declared in this scope| error: 'head' was not declared in this scope| I still don't fully understand pointers, so I'm not sure if I'm doing it right. But shouldn't my pointers, next and head work because they are global variables? Or do I have to create an object to use them because they are from a friend class?
Read [url=http://facepunch.com/showthread.php?t=1250528&p=42175006&viewfull=1#post42175006]this[/url]. If you still don't know what's wrong, just say so and I'll explain the precise problem with your code.
[QUOTE=ZeekyHBomb;42256781]Read [url=http://facepunch.com/showthread.php?t=1250528&p=42175006&viewfull=1#post42175006]this[/url]. If you still don't know what's wrong, just say so and I'll explain the precise problem with your code.[/QUOTE] I still don't know what's wrong. Pointers are confusing man, I feel like I understand the concept but I can't get it to work.
I fucking love you Zeeky.
[QUOTE=xExigent;42255736]If I try your second suggestion, all it does is bring the ammo down by one, but then when I let go of P, it goes back up.[/QUOTE] You are setting the ammo back to 3 every time that code runs, you are subtracting 1 every time but next time it just gets set back to 3 so it appears to be stuck on 2.
[QUOTE=wlitsots;42256986]I still don't know what's wrong. Pointers are confusing man, I feel like I understand the concept but I can't get it to work.[/QUOTE] Alright, so the first problem is, that new returns a pointer to the allocated object. It's not just the (plain/non-pointer) type, because that would mean it lives on the stack (and gets popped off the stack when the scope end). If you know what references are, it also cannot be that, because references carry a non-ownership semantic and that would (semantically) imply that the object returned by new gets managed by someone else. You need to declare the variable newNode as a pointer to Node (the same syntax you used for head, tail and next in Node). That takes care of the first and second error. The second error is resolved, because you were assigning newNode of type Node to n.tail (which is of type Node*). Since newNode is now also Node*, it's the same type and no conversion needs to be done. I skipped over the next three errors, because I thought they were all pointer-related, but the last three you posted are not related to that. There are simply no variables with these name in the scope. You probably meant n.next and n.head, I didn't try to really understand your code though, so I might be wrong. It will certainly compile with that change, but take care that it's semantically correct, too! [QUOTE=WTF Nuke;42257008]I fucking love you Zeeky.[/QUOTE] :3 [editline]21st September 2013[/editline] no homo [editline]21st September 2013[/editline] maybe a little bit homo maybe
I've read something about the Builderpattern, but I'm not sure why someone should use it. I mean it looks terrible and makes the creation of an object unnecessarily complex. I would rather try to invest more time in hiding the process of creation than using the Builderpattern. Is there any good reason to use it?
nevermind
[QUOTE=Dyler;42257277]I've read something about the Builderpattern, but I'm not sure why someone should use it. I mean it looks terrible and makes the creation of an object unnecessarily complex. I would rather try to invest more time in hiding the process of creation than using the Builderpattern. Is there any good reason to use it?[/QUOTE] This patterns is for objects that have a lot of customization and achieve the differentiation not via inheritance, but by storing a bunch of variables. If the customization should only happen once, when creating the object, and not change after that, you need a lot of constructor parameters. If you have default values for most of them, you'll need a lot of variations, creating an amount of constructors exponentially to the number of parameters. The Builder pattern uses an object, where you can set these parameters one-by-one, even after creation, and then either use a method creating the customizable object or have a constructor in the customizable object taking this builder-object. Now the number of methods only increases linearly to the number of parameters.
Does anyone know of any open source modern OpenGL projects that I can poke around? Preferably ones that work with 3D.
[url=http://www.xonotic.org/]Xonotic[/url] [url=http://www.redeclipse.net/]Red Eclipse[/url] [url=http://www.unvanquished.net/]Unvanquished[/url] [editline]21st September 2013[/editline] [url=http://ttimo.github.io/doom3.gpl/]Doom 3[/url] maybe?
[QUOTE=ZeekyHBomb;42255940]What's the code you have now?[/QUOTE] [code] if(start == true) { ammo = 3; if (keyboard.IsKeyDown(Keys.P)) { ammo--; } } [/code] That's what I have now, and as I said, it only reduces it by one while P is down.
Because if start is true, you're setting ammo to 3.
In addition to what Agent said, you need to initialize ammo with 3 somewhere outside the loop. Since you have no explicit loop-construct in the posted code (although there might be outside of the snippet you posted) I'm going to assume that this is in some kind of update-function that is called repeatedly by the XNA framework. In this case, you need to put it in some kind of initialization-function, perhaps the constructor of the object.
'object reference not set to an instance of an object' Does this mean I don't have any instances of a object that I'm trying to call? Because I've initialized the Queen class and Worker class like the book told me to, but pressing any of the buttons on the form bring me that error. Form1: [code] InitializeComponent(); Worker[] workers = new Worker[4]; Queen queen = new Queen(workers); workers[0] = new Worker(new string[] { "Nectar collector", "Honey manufacturing" }); workers[1] = new Worker(new string[] { "Egg care", "Baby bee tutoring" }); workers[2] = new Worker(new string[] { "Hive maintenance", "Sting patrol" }); workers[3] = new Worker(new string[] { "Nectar collector", "Honey manufacturing", "Egg care", "Baby bee tutoring", "Hive maintenance", "Sting patrol" }); } Queen queen; private void button2_Click(object sender, EventArgs e) { if (queen.AssignWork(comboBox1.Text, (int)numericUpDown1.Value) == false) MessageBox.Show("No workers are avaiable to do the job'" + comboBox1.Text + "'", "The queen bee says..."); else MessageBox.Show("The job'" + comboBox1.Text + "' will be done in" + (int)numericUpDown1.Value + "shifts", "The queen bee says..."); } private void button1_Click(object sender, EventArgs e) { report.Text = queen.WorkTheNextShift(); } }[/code]
[code]Queen queen = new Queen(workers);[/code] This initializes a local variable "queen" which [i]shadows[/i] the class-scope variable "queen".
I have this code to delete the first node from a linked list in c++. [code] void delFirst() { if (n.head == NULL) { errorMsg(); } else { temp = n.head->next; n.head = temp; } }[/code] So, I'm not actually deleting the node, I'm just not pointing to it anymore. I'm pretty sure c++ doesn't have a garbage collector so does that mean my program will run out of memory or be really slow once I start deleting a bunch of nodes?
[QUOTE=wlitsots;42279496]I have this code to delete the first node from a linked list in c++. [code] void delFirst() { if (n.head == NULL) { errorMsg(); } else { temp = n.head->next; n.head = temp; } }[/code] So, I'm not actually deleting the node, I'm just not pointing to it anymore. I'm pretty sure c++ doesn't have a garbage collector so does that mean my program will run out of memory or be really slow once I start deleting a bunch of nodes?[/QUOTE] You have to call delete on the node pointer. So if the pointer is n.head, you say [cpp]delete n.head;[/cpp] Not sure how safe it is in your case, but when you delete you just delete the data at that address, anything that is still pointing to that data is still pointing to that location, but there will be no data there and so you get access violations and dangling pointers. Memory leaks are bad, avoid those. Look into smart pointers (unique_ptr and shared_ptr in C++11).
[QUOTE=WTF Nuke;42279543]You have to call delete on the node pointer. So if the pointer is n.head, you say [cpp]delete n.head;[/cpp] Not sure how safe it is in your case, but when you delete you just delete the data at that address, anything that is still pointing to that data is still pointing to that location, but there will be no data there and so you get access violations and dangling pointers. Memory leaks are bad, avoid those. Look into smart pointers (unique_ptr and shared_ptr in C++11).[/QUOTE] Thanks. My program still works with or without that delete line but I guess it would have gotten bad if I didn't have it and was deleting a bunch of nodes?
Depends on your definition of 'bad'. As long as the program runs, no one can use that memory. Once it quits, the OS will reclaim the memory anyways. It is still considered bad practice to not free heap memory on all paths.
Does anyone know of any .net library that allows you to get information about the hardware that the program is running on? Stuff like current CPU load, running processes, available and total ram, disk usage... Oh and that works with mono
[QUOTE=Richy19;42281720]Does anyone know of any .net library that allows you to get information about the hardware that the program is running on? Stuff like current CPU load, running processes, available and total ram, disk usage... Oh and that works with mono[/QUOTE] It's all available in vanilla .NET, if that's good enough for you. CPU: [code]PerformanceCounter counter = new PerformanceCounter(); counter.CategoryName = "Processor"; counter.CounterName = "% Processor Time"; counter.InstanceName = "_Total"; public string GetCpuUsage() { return counter.NextValue() + "%"; }[/code] Running Processes: [code]Process[] processes = Process.GetProcesses(); foreach(Process p in processes) { Console.WriteLine("Process: {0} ID: {1}", p.ProcessName, p.Id); }[/code] RAM usage: [code]PerformanceCounter counter = new PerformanceCounter("Memory", "Available MBytes"); public string GetRamUsage() { return counter.NextValue() + "MB"; }[/code] Disk usage: [code]DriveInfo[] drives = DriveInfo.GetDrives(); foreach(DriveInfo drive in drives) { Console.WriteLine("{0}: {1}/{2}", drive.Name, drive.AvailableFreeSpace, drive.TotalSize); }[/code]
So i've got a dynamic array of pointers. Now i want to delete it. But i'm kinda stuck on how to do it [CPP] // the dynamic array of pointers KeyValuePair **m_KeyValuePairPtr; [/CPP] So this way, [CPP] KeyValueList::~KeyValueList() { delete[] m_KeyValuePairPtr; } [/CPP] This way [CPP] KeyValueList::~KeyValueList() { for (int i = 0; i < m_Length; ++i) delete m_KeyValuePairPtr[i]; } [/CPP] OR this way? [CPP] KeyValueList::~KeyValueList() { for (int i = 0; i < m_Length; ++i) delete m_KeyValuePairPtr[i]; delete[] m_KeyValuePairPtr; } [/CPP]
Depends on how you allocated them. Also consider if using std::vector, if C++11 with std::unqiue_ptr, or boost::ptr_vector might be appropriate. [editline]23rd September 2013[/editline] Probably the third though.
Thanks [QUOTE=ZeekyHBomb;42285692]Depends on how you allocated them.[/QUOTE] [CPP] void KeyValueList::Insert(KeyValuePair *elementPtr) { if (m_Length == m_Capacity) { m_Capacity += 5; KeyValuePair **arrayPtr = new KeyValuePair*[5]; for (int i = 0; i < m_Length; ++i) { arrayPtr[i] = m_KeyValuePairPtr[i]; } delete[] m_KeyValuePairPtr; m_KeyValuePairPtr = arrayPtr; } m_KeyValuePairPtr[m_Length] = elementPtr; ++m_Length; } [/CPP] [QUOTE=ZeekyHBomb;42285692] Also consider if using std::vector, if C++11 with std::unqiue_ptr, or boost::ptr_vector might be appropriate. [/QUOTE] I did consider it. But then the teacher told me that wasn't the exercise.
Sorry, you need to Log In to post a reply to this thread.