ID:168666
 
I'm tinkering around with my own scripting language but I've run into the problem that I can't verify if a varible exists before I try to access it. Rather than letting the BYOND itself handle the problem I want to trap it myself. I know I could just iterate through the keys of the vars list but for a datum with a large number of vars that could be very slow to do every single time a variable is accessed.
What about just doing if("varnametocheck" in vars)?
In response to tenkuu
What about just doing if("varnametocheck" in vars)?

Unfortunantly I think that checks the contents of the vars not the keys for the associations :P.
This is the method used in my ItaniumCore.
It works fine except for one thing,if the var is null(doesnt exist) itll return the error.So I suggest setting null vars to 0.
/** NOTE **/
/**The Var parameter should be non text(eg Egg not "Egg")**/
#define VarCheck(datum,var) \
if(!datum.vars["[var]"])#error Variable Does not Exist!;

Hopefully that helps.


Itanium
In response to Itanium
Yeah I thought of trying that first however if I even try to read from a variable in the vars list that does not exist I get a run time error rather than a null :P.
In response to Theodis
Thats strange because if the var doesnt exiist you should get a compile-time error.

Sorry I could'nt be of help to you.

Itanium
In response to Itanium
Err you must have missed my question :P. Here is the test code I used.

widget
var/a
var/b = 1
mob/verb/test()
var/widget/w = new()
world << w.vars["a"]
world << w.vars["b"]
world << w.vars["c"]


It'll crash when trying to output that third line since c doesn't exist. I want to find a way that is relativly fast to figure out dynamically if the variable does exist.
In response to Theodis
I understand that but if you look at my code,if the var doesnt exists you will get a compile time error telling you.
eg:
/** Itanium **/
varCheck(/widget,c)
In response to Theodis
I gave you an alternate method to see if a variable exists. If that's not what you're after, perhaps you should be a little more clear. :p



widget
var/a
var/b = 1
mob/verb/test()
var/widget/w = new()
if("c" in w.vars)
src << "c is defined for a widget"
else
src << "Error, c not defined."

In response to tenkuu
Ahh so it does work :P. My problem is I tested it like
world << "c" in w.vars
Which has some precedense problems and outputted nothing :P.
However like
world << ("c" in w.vars)
it gives me the expected 0.

How low is the in operator?
In response to Theodis
Down below ?.
In response to tenkuu
tenkuu:Down below ?.

Referring to the operators priority when placed in an expresson.
Eg:
/** In this instance n would be set to nonexistant before it was checked to even be in the list **/
if(!d in list(a,b,c,d))



Itanium
In response to Itanium
No, I mean down below ?, the operator. As in "exp"?res1:res2. I know what I'm talking about, most of the time. :p Didn't mean to confuse anyone into having to tell me what he was talking about.
In response to tenkuu
:)

Its strange that you'd stick your tongue at me necause you worded a question wrong.

down below ? <-- are you forgetting that thats a qusetion and not meaning to give you a grammar lesson but a question mark comes after a question.

down below '?' would have actually stood out more and I would have known it to be the operator.


Itanium
In response to Itanium
Not to drag this out, but I did have a period after the question mark. In the context of Theodis's question, anyone should have known I meant the ? operator, without me specifically having to say it was an operator. And more importantly Theodis would have known what I was talking about anyway. But I'll concede that I would have been clearer with it as '?'. :)
In response to tenkuu
Down below ?.

Amazingly enough it is :P. This generates an error.
var/a = "c" in w.vars ? 1 : 0

[edit]
Wow it is even below assignment! It may very well be the last thing to be done in the order of operations :P.
In response to Theodis
The complete order of operators can be found in the BYOND 2 Sourcebook (Blue Book).
In response to PirateHead
The complete order of operators can be found in the BYOND 2 Sourcebook (Blue Book).

It's not quite complete :P. No in, assignment, or new operators.