• What Do You Need Help With? V6
    7,544 replies, posted
[QUOTE=ZeekyHBomb;41739605]Compiler compatibility should not be an issue. The rest is mostly subjective. The most imported thing is to stay consistent. Remember that auto only resolves to "basic types" though, no references or cv-qualifiers.[/QUOTE] If I understand you correctly I can't do "auto &" or "const auto" in function declarations? If so I may stick with the old style declaration for the most part, because I make liberal use of const and references in an attempt to force the compiler to catch my mistakes :v: [editline]6th August 2013[/editline] [code] main.cpp:9:16: error: ‘bar’ function with trailing return type has ‘auto&’ as its type rather than plain ‘auto’ auto &bar() -> int { [/code] this makes me sad. Why did the revision exclude this?
Nah, what I meant is that you have to do that, since auto by itself will never resolve to a reference or const or volatile stuff. [editline]7th August 2013[/editline] You mean auto bar() -> int&.
-snip- Ah, now I understand; thanks for the help! I think I'm going to try moving to all new form, because I see no downsides, and use auto for local variables wherever it makes sense (not just apply it everywhere I can). [editline]6th August 2013[/editline] With reference to consistency, what do you think of still using void to declare a function which returns nothing? That way when you see a function, you can know if it returns anything ("auto") or nothing ("void") up front and not have to look for a return type in the latter case. Also, it doesn't make much sense to actually "return" nothing, so "-> void" seems awkward. tl;dr is this: [code] auto foo() -> int; void bar(); [/code] better than: [code] auto foo() -> int; auto bar() -> void; [/code] ? It's kind of like the difference between a "subroutine" (void) and a proper "function"/"method" (auto), but with strange keywords.
I am unsure how to use textures in OpenGL. I am loading them correctly (I think, the reason I say this is because they are assigned a proper value). This is in my draw loop: [cpp]glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureValue); glUniform1i(glGetUniformLocation(mainProg, "texSampler"), 0); glDrawElements(GL_TRIANGLES, i->triangles*3, GL_UNSIGNED_INT, 0);[/cpp] However, all I get is black. The loading part is here: [cpp]GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); int width, height; unsigned char* image = SOIL_load_image(name.c_str(), &width, &height, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); glBindTexture(GL_TEXTURE_2D, 0);[/cpp] Ignore the variable name inconsistency, they are just different functions. Edit: The solution lies in that you have to have to have some tex parameters, namely the min and mag filter. Well now that I've fixed that, I have another issue. Since I'm just using SOIL to load image, it doesn't invert the Y axis, and I can't set it to do that since there are no flags to set. What would be the way to go to fix this? Fuck it I'll be lazy and just change the shader's y coord.
I'm using Bullet physics and it seems that it crashes right when my program finishes running. I think it has something to do with memory management. I just declared all the Bullet stuff on the stack before, but now I've made it in explicit memory management style (the freeing order matters, so smart pointers wouldn't solve this problem). But now to the problem. It seems that my rigid bodies (which are declared as members of stack objects) also make the program crash since they reference some object relevant to the whole "world". Since they're on the stack, they get deleted right after I've deleted all the "global" bullet objects manually. It just seems so complicated to migrate everything to manual memory management. Is there any simpler solution?
[QUOTE=Rayjingstorm;41739831]With reference to consistency, what do you think of still using void to declare a function which returns nothing? That way when you see a function, you can know if it returns anything ("auto") or nothing ("void") up front and not have to look for a return type in the latter case. Also, it doesn't make much sense to actually "return" nothing, so "-> void" seems awkward. tl;dr is this: [code] auto foo() -> int; void bar(); [/code] better than: [code] auto foo() -> int; auto bar() -> void; [/code] ? It's kind of like the difference between a "subroutine" (void) and a proper "function"/"method" (auto), but with strange keywords.[/QUOTE] Well defined exceptions to the rule should be fine. [QUOTE=thf;41746629]I'm using Bullet physics and it seems that it crashes right when my program finishes running. I think it has something to do with memory management. I just declared all the Bullet stuff on the stack before, but now I've made it in explicit memory management style (the freeing order matters, so smart pointers wouldn't solve this problem). But now to the problem. It seems that my rigid bodies (which are declared as members of stack objects) also make the program crash since they reference some object relevant to the whole "world". Since they're on the stack, they get deleted right after I've deleted all the "global" bullet objects manually. It just seems so complicated to migrate everything to manual memory management. Is there any simpler solution?[/QUOTE] Order of destruction is well defined, so smart pointers should be fine. Except when we're talking about globals between files (though within one translation unit the order is still well defined). I not sure I caught correctly what's going on. The problem is that you have this rigid body-member referencing some other bullet object that you delete, but the rigid body-member will try to access at some point? Why would you delete something that is still referenced? Did you intend to destroy the object that has the rigid body-member first? What's stopping you from doing that?
[QUOTE=ZeekyHBomb;41746765]Well defined exceptions to the rule should be fine. Order of destruction is well defined, so smart pointers should be fine. Except when we're talking about globals between files (though within one translation unit the order is still well defined). I not sure I caught correctly what's going on. The problem is that you have this rigid body-member referencing some other bullet object that you delete, but the rigid body-member will try to access at some point? Why would you delete something that is still referenced? Did you intend to destroy the object that has the rigid body-member first? What's stopping you from doing that?[/QUOTE] I have a heap allocated object that the rigid body references. But the rigid body is stack allocated and will be freed after it because of that.
[cpp]struct Foo { Foo(int &ref):Ref(ref) {} ~Foo(); int &Ref; }; Foo::~Foo() { //does something with Ref } int main() { auto ip = new int; Foo f(*ip); delete ip; //f.~Foo() crash }[/cpp] So this? Would be solved with smart pointers [cpp]int main() { auto ip = std::make_unique<int>(); Foo f(*ip); //first f.~Foo() will be called - ip (a.k.a. f.Ref) is still valid //ip.~unique_ptr<int>() will be called, (ip->get()) will be freed, but is no longer relevant for f }[/cpp]
[QUOTE=ZeekyHBomb;41746908][cpp]struct Foo { Foo(int &ref):Ref(ref) {} ~Foo(); int &Ref; }; Foo::~Foo() { //does something with Ref } int main() { auto ip = new int; Foo f(*ip); delete ip; //f.~Foo() crash }[/cpp] So this? Would be solved with smart pointers [cpp]int main() { auto ip = std::make_unique<int>(); Foo f(*ip); //first f.~Foo() will be called - ip (a.k.a. f.Ref) is still valid //ip.~unique_ptr<int>() will be called, (ip->get()) will be freed, but is no longer relevant for f }[/cpp][/QUOTE] So you're saying that the stack destructs backwards? Changing it to a smart pointer would then make ip destruct after f, right? Hmm, you know what? I think I might try allocating the Bullet objects all on the stack. Because I'm not sure at all what went wrong when I tried to do it then.
Yes, destruction happens in reverse order (for exactly this reason :smile:).
[QUOTE=ZeekyHBomb;41747055]Yes, destruction happens in reverse order (for exactly this reason :smile:).[/QUOTE] Thanks! It actually seems to work now. I have no idea how it didn't work before :v: Now to another question about using Bullet with my code. As I said, I store a reference to a rigid body in an object. Now that my requirements have changed slightly, the constructor for the rigid body needs a value. And the only way to get that value is to call a method which takes a reference and writes to it. So I essentially need to defer construction of my rigid body until that. The simplest way I could think of was to just wrap it in a std::unique_ptr<btRigidBody>. Any simpler way?
[cpp]private: static Value getRigidBodyValue() { Value val; btGetValue(val); return std::move(val); }[/cpp] Either that (member function) or as a local function in the implementation file for your object. [cpp]MyObject::MyObject() :RigidBody(getRigidBodyValue()) {[/cpp] [url]http://xkcd.com/859/[/url]
[QUOTE=ZeekyHBomb;41747281][cpp]private: static Value getRigidBodyValue() { Value val; btGetValue(val); return std::move(val); }[/cpp] Either that (member function) or as a local function in the implementation file for your object. [cpp]MyObject::MyObject() :RigidBody(getRigidBodyValue()) {[/cpp] [url]http://xkcd.com/859/[/url][/QUOTE] Haha damn, why didn't I think of that. Thanks man! [editline]7th August 2013[/editline] Now what if I want to call a member function on a member I just constructed before passing a reference to that member to the next member constructor? [cpp] A::A() : x(0), // Here is where I need to do x.y() z(x) {} [/cpp]
I have two tables. One is for patterns, one is for responses. I'm not entirely sure if I've set it up right, though. Here's the code that deals with it: [lua]function searchAGTPattern(message) message = message:upper() for pattern in pairs(AGTPattern) do if message:match(pattern) then return pattern.Responses else pattern = 0 return pattern.Responses end end end [/lua] Here's a snippet of one table (it doesn't seem to format correctly but ideally everything is lined up nicely): [lua]AGTPattern = { ["**NO KEY FOUND** DO NOT TOUCH THIS KEY"] = {Responses = {106,107,108,109,110,111,112,129,130,131,132}},--[1] ["**REPEAT WHISPER** DO NOT TOUCH THIS KEY"] = {Responses = {113,114,115,116,117}}, --[2] ["YOU'RE"] = {Responses = {6,7,8,9}}, --[3] ["I DON'T"] = {Responses = {10,11,12,13}}, --[4] ["I FEEL"] = {Responses = {}} --Finish this table later (i'm not quite done) }[/lua] The second table is basically the same but it's just full of Responses. (I'll number them and then call them appropriately) I haven't written it yet. Would the way I've written the code work? Would "return pattern.Responses" return the appropriate response if I'm calling one table within another one? I'm still trying to figure this out.
[lua]function searchAGTPattern(message) message = message:upper() for pattern in pairs(AGTPattern) do --this will only get the key, you need pattern, responses if message:match(pattern) then return pattern.Responses --and here return responses.Responses then else pattern = 0 --I'm not sure what you're doing here return pattern.Responses --but this will break (accessing 0.Responses) end end end [/lua] [QUOTE=Zinayzen;41748837]Here's a snippet of one table (it doesn't seem to format correctly but ideally everything is lined up nicely):[/QUOTE] It doesn't line up, because tab width are different than in your editor. Don't align stuff with tabs. Indentation with tabs is fine, but use plain spaces to align stuff. [QUOTE]The second table is basically the same but it's just full of Responses. (I'll number them and then call them appropriately) I haven't written it yet.[/QUOTE] Currently the value of the pairs in AGTPattern, of second table as I presume you call them, it a table with a single key. It could just as well be a simple array (which in Lua is just a special kind of table, but it would still make it less complex). Maybe you're planning on extending that though. [QUOTE]Would "return pattern.Responses" return the appropriate response if I'm calling one table within another one? I'm still trying to figure this out.[/QUOTE] I commented your code above. I still quote you on this because your wording is incorrect, you cannot call a table. I'm guessing you mean either putting a table in another one (a.k.a. nesting tables), which is fine, or you mean accessing entries of the table via the dot, which is also fine.
That is what I meant, yes. Sorry, like I said I'm still trying to figure this out so I'm not particularly surprised my vocabulary is totally wrong. Thanks for the help. Speaking of tab indenting, when I access my code in like Notepad or something, it's not spaced out at all. It's very disconcerting. Is there a way to make the actual .lua file easily readable? [editline]7th August 2013[/editline] [QUOTE=ZeekyHBomb;41749389][lua]function searchAGTPattern(message) message = message:upper() for pattern in pairs(AGTPattern) do --this will only get the key, you need pattern, responses if message:match(pattern) then return pattern.Responses --and here return responses.Responses then else pattern = 0 --I'm not sure what you're doing here return pattern.Responses --but this will break (accessing 0.Responses) end end end [/lua] [/QUOTE] I'm setting pattern to 0 to grab the default response. Does Lua not start tables at 0? My plan was to set it to 0 (or I guess 1 if they start at 1) which I've got set to **NO KEY FOUND**, so I'd just use that to send back generic responses if I couldn't find a specific pattern.
You mean like the plain Notepad? notepad.exe? I don't know if it has much tab settings. I'd barely recommend coding in it. You could exclusively use spaces instead of tabs, then it should display the same everywhere - you still need monospaced fonts though. Lua arrays begin at 1. It's supposedly easier to understand than 0-based arrays, but personally I think it just creates an inconsistency to most other languages. It's probably true that most people expect array to start with index 1, but I think it's very easy to learn that they start at 0 as well. Anyway, that code still effectively tries to index a number, as in 0.Responses. That won't work, 0 is not a table. You might mean AGTPattern[1].Responses. But it's probably not your intention to just "give up" after the first string was not a match - your function would always return AGTPattern[1].Responses (provided AGTPattern is an array with at least one entry). I think you just want to move the second return (my modified version) outside the loop, so when the loop finishes and did not return, which means no pattern successfully matched, it returns the first entry.
I usually just open the .lua file (which on my computer defaults to Notepad because it's much faster than opening an editor) to poke around with files, and most of them are formatted fine so it's fine. Mine, inexplicably, don't. I'll deal with it later, I guess. They start at 1? That seems...inconsistent. But whatever, I'll adjust it. And yeah, I meant AGTPattern[1].Responses. Thanks. I'll probably rewrite all of this, I'm learning more now and realizing how b ad most of the stuff I've written actually is. (Just out of curiosity, if I did do it like AGTPattern[1].Responses, how would I write the data for it? I was originally planning on just making two arrays and using the same index for both (parallel arrays?) but if I can make a table within a table, that might be smarter.)
I would recommend Vim (or gvim), but I shall be a bit more realistic and suggest Geany, gedit or Notepad++. [lua]local outer = { number_array = {1, 2, 3}, table_array = { { a = 1, b = 2 }, { a = 3, b = 4 }, { a = 5, b = 6, c = 7 } }, table_table = { one = { word = "bird", number = 7 }, two = { yay = "yay", nay = "nay" } }, } print(outer.number_array[1]) -- 1 outer.number_array[2] = 4 print(outer.number_array[2]) -- 4 if outer.table_array[1].a == outer.table_array[1]["a"] then -- both are correct ways of accessing the element print(true) -- true end --print(outer.table_array.1.a) -- error print(table_table["one"].word) -- "bird" print(outer.nope) -- nil outer.nope = "Eeyup" print(outer.nope) -- "Eeyup"[/lua] That should be sufficient :)
Ok, so I'm back with my GWEN questions. I'm writing chat for my game and I have beautiful TextBox for entering message. I have added on return (enter) press to it, and I want to pass std::string (message), and address of sf::TcpSocket (SFML2). So I have written function [CODE]void SendChatMessage(Gwen::Event::Info data);[/CODE] Passing std::string was easy (but is it done properly? Can I make it easier?): [CODE]void* data = const_cast<char*>(txtChat->GetText().c_str()); txtChat->onReturnPressed.Add(windowChat, &ClientState::SendChatMessage, data);[/CODE] But how to pass address of sf::TcpSocket instance also? BTW I missed this thread and my GWEN problems :)
Just pass the callback to some object, which can access the txtChat and the socket, so it can pass txtChat->GetText() and the sf::TcpSocket to the ClientState instance. [editline]7th August 2013[/editline] btw, the pointer returned by c_str is const for a reason. The stuff you do might work, but it's still not considered very clean. You should try to always avoid const_cast. And the internal pointer might even change and point to a new chunk of memory (e.g. if the buffer got too small), in which case the Callback would receive a stale pointer.
[QUOTE=ZeekyHBomb;41750488]Just pass the callback to some object, which can access the txtChat and the socket, so it can pass txtChat->GetText() and the sf::TcpSocket to the ClientState instance.[/QUOTE] Yay! It works, again I should have know that. Thank you! [QUOTE=ZeekyHBomb;41750488] btw, the pointer returned by c_str is const for a reason. The stuff you do might work, but it's still not considered very clean. You should try to always avoid const_cast. And the internal pointer might even change and point to a new chunk of memory (e.g. if the buffer got too small), in which case the Callback would receive a stale pointer.[/QUOTE] Yea, I still need a better understanding of all low level stuff. I'm working on it :D.
what is the best way to get the current elapsed time in seconds in a c# app? say for example, i click a button and i want the button to become clickable in 2 seconds. i'd record the time the button was clicked then add 2 to it, and if the next time the button is clicked is less than the first time then it doesn't do anything.
[QUOTE=twoski;41752087]what is the best way to get the current elapsed time in seconds in a c# app? say for example, i click a button and i want the button to become clickable in 2 seconds. i'd record the time the button was clicked then add 2 to it, and if the next time the button is clicked is less than the first time then it doesn't do anything.[/QUOTE] Not quite what you asked for, but you could create a Timer, set the interval to 2000 (milliseconds) and start it. In the event handler, stop the timer and enable the button again.
I don't know if it's the best way, but [url=http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx]System.Timers.Timer[/url] is one possibility. [editline]8th August 2013[/editline] :ninja:
For events use a Timer, for long/imprecise durations during which the program doesn't run continuously DateTime.Now (can go backwards) and to measure durations precisely use a Stopwatch instance.
Anyone got an idea on how to make things like reading data less ugly? For example, I am reading .mtl files. Right now I have a class that stores all the possible data a material can have, and then just stripping info from the file and storing it in the appropriate variables. It means a lot of copy and pasting, and a lot of bad looking code. Is there a way to streamline this? I thought of using a map, but is that kind of overhead necessary?
So I'm making a game in C# using SFML. Every time a character moves/rotates it stores their previous location/rotation before moving, as to move them back upon collision. So can anyone why this gets it stuck (red box is the 'hitbox'): [URL=http://s9.photobucket.com/user/Billy2600/media/collision2_zps1b8246ba.png.html][IMG]http://i9.photobucket.com/albums/a56/Billy2600/th_collision2_zps1b8246ba.png[/IMG][/URL] But collisions like these do not: [URL=http://s9.photobucket.com/user/Billy2600/media/collision1_zpse9badb5a.png.html][IMG]http://i9.photobucket.com/albums/a56/Billy2600/th_collision1_zpse9badb5a.png[/IMG][/URL]
[QUOTE=WTF Nuke;41752586]Anyone got an idea on how to make things like reading data less ugly? For example, I am reading .mtl files. Right now I have a class that stores all the possible data a material can have, and then just stripping info from the file and storing it in the appropriate variables. It means a lot of copy and pasting, and a lot of bad looking code. Is there a way to streamline this? I thought of using a map, but is that kind of overhead necessary?[/QUOTE] Maybe you could write a specialized script for your purpose that generates the code for you from a smaller definition. I'm afraid that without seeing actual snippets of code I can't give a more specialized answer. I'd probably have a map with callbacks and then use some templated functions to automate type conversions. [QUOTE=Billy2600;41752679]So I'm making a game in C# using SFML. Every time a character moves/rotates it stores their previous location/rotation before moving, as to move them back upon collision. So can anyone why this gets it stuck (red box is the 'hitbox'): [URL=http://s9.photobucket.com/user/Billy2600/media/collision2_zps1b8246ba.png.html][IMG]http://i9.photobucket.com/albums/a56/Billy2600/th_collision2_zps1b8246ba.png[/IMG][/URL] But collisions like these do not: [URL=http://s9.photobucket.com/user/Billy2600/media/collision1_zpse9badb5a.png.html][IMG]http://i9.photobucket.com/albums/a56/Billy2600/th_collision1_zpse9badb5a.png[/IMG][/URL][/QUOTE] I recommend printing out some debugging info, like the boolean whether they collide and the current position. When it gets stuck, the position would have to somehow be different from just before the collision. From there you can try to pin down the instruction that somehow modifies the old location of otherwise effects the current location after a collision happened. This you can do either by adding more print statements or if you have a good debugger you can also break conditionally when the collision still happens after resetting the position and then step back a couple of times.
Thank you for your advice Zeeky. I [i]think[/i] I've fixed it. Basically in certain situations the previous location wasn't large enough to move the object out of the wall. I just had it double check the collisions after movement, and if it was still colliding added/subtracted a .005 and sent it back through the loop.
Sorry, you need to Log In to post a reply to this thread.