ProtectTable() command which would prevent more variables being added

Feature requests for future versions can be voiced here
Post Reply
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

ProtectTable() command which would prevent more variables being added

Post by Bugala »

Probably too much work, but throwing this here in case you one day start making some debugging thing.

Came to my mind that one of the hard to track bugs is when you do a misspelling on a variable.

As example:

Enemy.MovementAmount = 1
vs
Enemy.MovmenetAmount = 2

You might only notice that there is some strange behavior (as in this case enemy moves at wrong speed) and it might be difficult to track it down, since point is that you have in your code a place where you are setting the right speed, but you are putting it to a wrong named variable.

This kind of bug is quite common (to me at least, although I have so far been able to find them quite quick), and if there would be option to protect tables in some way, it would be easy to spot.

What I am after with this protection is that suppose I have table:
Enemy = {x, y, movementamount}


If I could then protect this table somehow, as in it wouldn't accept any more indexes, then if I would put Enemy.MovmentAmount=2, it would give me error saying that Enemy is protected from adding more variables, at which point I would instantly know where the error is.

Basically I can already do this myself in way of having Enemy.SetX(), Enemy.getX(), but especially if there are lots of these variables in a table, it starts to become quite messy making each variable have their own setter and getter commands, and even more problematic when looking at list of functions and trying to find some certain function in IDEs list of functions, since all those Getter and Setter functions would be bloating the list.

An example of how this could work:

Code: Select all

TurnOnTableProtection(True)
Enemy = {x, y, movementamount}
ProtectTable(Enemy)
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: ProtectTable() command which would prevent more variables being added

Post by jPV »

You can do it with metamethods. See the "__newindex" example in the docs:
http://www.hollywood-mal.com/docs/html/ ... prMM_.html
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: ProtectTable() command which would prevent more variables being added

Post by Bugala »

True! And great idea JPV. Didnt come to my mind it could already be achieved through metamethods, and also, this opens other possibilities too, could turn out something very useful in the future now that I realise there is such an option.
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: ProtectTable() command which would prevent more variables being added

Post by Bugala »

Took a try and here is a working example:

Code: Select all

mt = {}

Function mt.__newindex(Table, Index, Content)
	DebugPrint("Error! - - - Trying to Add new Table Index!")
	DebugPrint("Table:")
	DebugPrint(Table)
	DebugPrint("Index:")
	DebugPrint(Index)
	DebugPrint("Content:")
	DebugPrint(Content)
EndFunction

TestTable = { tester = 1, testor = 2, testosterone = 3 }

SetMetaTable(TestTable, mt) 


TestTable.Tester = 9
testTable.Testre = 8
Post Reply