[gameprogrammer] Re: pointer values

  • From: Vince <uberneen@xxxxxxxxx>
  • To: gameprogrammer@xxxxxxxxxxxxx
  • Date: Sat, 27 Nov 2010 17:46:38 -0800 (PST)

The way I understood the question was more along the lines of keeping a lua 
script in sync with whatever entity manager he has in place.  If an entity has 
a logic routine (in the lua script) that iterates through a list of other known 
entities in order to make a decision, he may be requesting information about it 
by calling a host function.  If that host function returns 0, the source entity 
knows that it needs to prune that entity from its list.  This assumes that the 
host program is already cleaning up after itself, otherwise how would it know 
to return 0 to the lua script in the first place?

That's how I took it, having worked with lua on a couple of occasions, but more 
information would certainly be helpful.

Vince~

--- On Sat, 11/27/10, coder4hire <coder4hire@xxxxxxxxxxxxxx> wrote:

From: coder4hire <coder4hire@xxxxxxxxxxxxxx>
Subject: [gameprogrammer] Re: pointer values
To: gameprogrammer@xxxxxxxxxxxxx
Date: Saturday, November 27, 2010, 7:51 PM

It's very dangerous to use pointers that way.
Imagine that you one of your AI instances contains a pointer to target, and 
that target dies, then you would need to zero out the pointer, but how would 
you do that? You would need to have the "target" contain a list of instances 
that are targeting it so that you could notify them that you are dead, but what 
if one of those units died? 
Using pointers that way is an invitation to NULL pointer access exceptions. I 
ran into this problem with an RTS projects a few years ago, the game would 
crash randomly, more often when there were lots of units involved in combat. I 
wanted to keep everything pointer based to keep access speed fast, but it 
turned into a nightmare of epic proportions, because debugging NULL pointer 
exceptions that are semi random in nature is impossible.
I found an interesting article on Gamasutra that provided the perfect solution, 
a Handle Manager. Instead of passing around pointers to instances (or NULL for 
dead/no instances), you pass around a handle, which is an index in a database 
of pointers. When an AI instance is created, you register an entry into the 
Handle Manager and get back an index into the location of your pointer, and 
when you die, you simply notify the Handle Manager to NULL out the entry in the 
table of your index. That way you pass around an index, and when you access the 
index to get the pointer, you know whether the unit is alive or not.
This solution is not perfect, for one it costs twice as much memory, because 
now you store a pointer and an index to that pointer for every unit (granted, 
even if you have thousands of units it wouldn't be a drastic memory increase), 
and there is a slight overhead when accessing the pointers, but for my case it 
was a godsend, it completely solved all of our NULL pointer exceptions with 
very small overhead.
Here's the link 
again:http://www.gamasutra.com/view/feature/4015/managing_data_relationships.php
Good luck! 


---- On Wed, 24 Nov 2010 05:27:20 -0500 Roger Durañona Vargas 
<luo_hei@xxxxxxxx> wrote ---- 

 In my game I have found a problem implementing the AI. I have some Lua 
scripts that request the entity instances from the host application and 
receive the instance as a pointer. I found that I need a way to signal 
the scrit that such entity no longer exists (for example, was killed). 
My question is, is it safe to return 0 as pointer value to tell the 
script that such entity no longer exists? 
 
--  
Roger D. Vargas 
Using Gentoo Linux 2010, Ogre 1.7.1 
Powered by Core 2 Duo E8400 3.0GHz, 2Gb RAM, Radeon HD5770 
http://gpnfn.blogspot.com, The news for game programming newbies 
 
 
--------------------- 
To unsubscribe go to http://gameprogrammer.com/mailinglist.html 
 
 




      

Other related posts: