Hi. I'm trying to work on a way to represent gun magazines realistically, in that each magazine has a list of bullets that it goes down when firing, so that different bullet types (dif. color tracers, normal bullets, slugs, buckshot, etc.) can be loaded at once in a magazine.
I want an efficient way to represent them, and currently I'm thinking of having a table with every # key within the table holding a string with the bullet's ID (such as [1] = "bullet_type1", [2] = "bullet_type2", [3] = "bullet_type1", etc.)
I'm just wondering if there is a better way, and if this could cause significant slowdown if a shitton of people are firing guns at once.
While I'm at it, I'm currently representing magazines as an individual "object", so it can have meta functions such as Add, Subtract, etc. for bullets, and bullets are simply just a table within a global table that has an id to lookup (for example, when the gun reaches "bullet_type1" in the magazine, it will go to the global table and put that key in, and take the data from the found table, and remove that bullet from the mag.)
If any clarification is needed, please ask. Any replies are appreciated, thanks!
[editline]16th March 2017[/editline]
Digging around the gmod lua wiki, would a stack object be good for the magazine's storage of bullets? It matches how a magazine works after all (push/pop). Would just pushing and popping the bullet's id be good?
A stack could be used however you wouldn't then be able to get information about the whole magazine without rolling through it, without playing around and not treating it like a stack. This could be bad if you wanted to show the contents of the mag in a GUI.
An object for the mag makes plenty of sense with specific functions to interact with the mag.
Your table idea looks like it would work absolutely fine and is a good way of doing it as to not replicate data, assuming that all bullets of type1 are identical.
Either way this sounds like it will be pretty cool. I look forward to a release!
I would just make sure that your framework is properly setup so you can change your underlying implementation at any given time in case it requires some performance tuning.
You could also avoid modifying the table by simply pointing out how many bullets there are left to fire and only iterate to that amount so you can avoid the table modification all at once, all that has to be provided is how the full magazine looks like and then increment once fired so thats the next bullet.
Sorry, you need to Log In to post a reply to this thread.