Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411275 Posts in 69323 Topics- by 58380 Members - Latest Member: bob1029

March 28, 2024, 05:03:06 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)General programming discussion
Pages: 1 ... 14 15 [16]
Print
Author Topic: General programming discussion  (Read 28915 times)
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #300 on: May 06, 2018, 07:19:21 AM »

This discussion is coming dangerously close to the line of unacceptable personal attacks. Please try to deescalate. Resolve your disagreements by debating ideas rather than attacking people, or agree to disagree.
Logged

JWki
Level 4
****


View Profile
« Reply #301 on: May 07, 2018, 01:41:36 PM »

I think this is just one of the topics where it is very very hard to not keep bashing each others heads in over. It also is one of the topics where the discussion tends to become very non-productive quickly.

In the end of the day, I'd argue it doesn't really matter all too much - what works for you, works for you. There's personal opinions that you can hold - I for example mostly find RAII unnecessary, have not seen nor can I really imagine a lot of situations where it would be helpful in avoiding bugs better than other, simpler solutions (I strongly believe in rigid assertions and instrumentation rather than trying to enforce correctness on the language level), and I simply prefer explicitness over the implicitness of RAII. When I read some code I don't really want to have and go check docs or read some class implementation to know what is happening, and RAII by design tends to hide some stuff that is happening - i.e. files being closed, handles being freed, etc etc.
However, I'm able to work in a codebase using RAII, even though I personally might not like it, and reversely a proponent of RAII should be able to work in a codebase that prohibits or discourages its use. That just professionalism.
Logged
Daid
Level 3
***



View Profile
« Reply #302 on: May 08, 2018, 04:23:09 AM »

Conceptually, hiding of closing/freeing memory is the idea of RAII.

However, I do have noticed that people are confused as hell when you go beyond the normal "free memory" and "close stream" things. I once had a NetworkPacket class, during initialization you give the socket that you want to sent it trough. And then during destruction it would get send.

It got people confused really fast. And was a good example of a bad design decision, even tough it made writing the code quicker and less error prone.

Right now, I have code that does this:
Code:
bool MyObject::onDamage(int amount)
{
  hp -= amount;
  if (hp <= 0)
    delete this;
}

While deleting yourself is perfectly valid in my code, as long as it's the last thing you do with that object. It might cause a few: Blink Blink from people.
Logged

Software engineer by trade. Game development by hobby.
The Tribute Of Legends Devlog Co-op zelda.
EmptyEpsilon Free Co-op multiplayer spaceship simulator
JWki
Level 4
****


View Profile
« Reply #303 on: May 08, 2018, 02:36:40 PM »

I guess to some the idea of hiding something like that doesn't really make sense.
Logged
Ordnas
Level 10
*****



View Profile WWW
« Reply #304 on: May 10, 2018, 12:11:11 AM »

In that example, delete the object when hp goes to zero, is not something that could be dangerous to use, if someone that use that code doesn't know the implementation details? Some game object could depend on it, and find a reference null when hp goes to zero.

Maybe an Actor manager could be more appropriate to decide deallocation of entities in the scene? Also it could be benefit the memory from the garbage collector not deleting the objects during gameplay and avoid stuttering. I suppose that MyObject is a component, that would be more generic, and reinforce to use a bool and manage the object by a manager.
Logged

Games:

Oats
Level 1
*


High starch content.


View Profile
« Reply #305 on: May 10, 2018, 12:58:45 AM »

Yeah, that code sounds pretty unsafe, even if its guaranteed no other game object references that object, a collision response or some other event object could, potentially resulting in a null pointer, I'd just flag a "isAlive" = false then clear up dead objects at the end of the tick, when all events will already be handled, but I guess that just depends on the games structure.

edit. on another note, anyone here used any of google's hosting platforms, or actually any hosting platforms for game server side stuff?
« Last Edit: May 10, 2018, 05:21:22 AM by Oats » Logged

eh
Daid
Level 3
***



View Profile
« Reply #306 on: May 10, 2018, 07:32:28 AM »

Remember, this is C++ code. And C++ code is always fun and never fully safe :D (In case anyone wonders, this is toy code, for my personal entertainment and hobby)

On normal C++, a delete like that could be unsafe because you would end up with dangling pointers. However, I'm using my own version of smart pointers which get set to NULL when the target object is deleted. And so, yes, other objects have to check if pointers are not NULL before using them. But logic wise, that makes sense, if you have an AI controlled object, that keeps a reference to a target, you need to check if the target is still "alive" before you use it.

Before, I had reference counting with "isAlive" flags, caused problems with circular references. As I could only delete the object and know that all references where cleared when the counter was zero. So for the next iteration, I went with this design of keeping a linked list of all smart-pointers that are pointing to this object.

I'm still not sure if deleting "at the spot" or after the game tick is the best option. If memory management was a problem, I would just use an overloaded new/delete to improve caching/performance. But so far, that never has been an issue in my applications.


(Small note, these smart pointers are a bit more complex then I sketch here. It also has a smart pointer list, where it is safe to delete an object that is in that list while iterating over that list)
Logged

Software engineer by trade. Game development by hobby.
The Tribute Of Legends Devlog Co-op zelda.
EmptyEpsilon Free Co-op multiplayer spaceship simulator
Pages: 1 ... 14 15 [16]
Print
Jump to:  

Theme orange-lt created by panic