Go to Page... |
Thread Tools | Display Modes |
01/31/15, 04:15 PM | #1 |
Problem with local variable (it is NIL all of sudden)?
Hey developers,
I got a problem with a source code. Inside my function I use the :GetName() ESO LUA function to get the name of a control. The name is stored inside the local (only known inside my function) variable "equipmentMarkerControlName" I'll then determine the digits at the end of the string and remove them. This works without any problems and the variable will store the new string afterwards, without trailing digits. I know there might be simpler ways to strip the trailing digits in this string but I'm not really well in using RegEx :-( Afterwards I will use a loop to read an array (markedIcons) and if the iconid from the array differs from a function's parameter (markerId) I try to write the variable "equipmentMarkerControlName" to the chat again. But it is NIL again here all of sudden? Why? Lua Code:
Chat output will be: Lua Code:
The function will be called once! For the given example the loop at array "markedIcons" generated 2 chat output rows, because iconIsMarked was true 2 times. I can't see why this variable is NIL. It should contain the string ZO_CharacterEquipmentSlotsRing2FCOIS :-( If I declare the variable "equipmentMarkerControlName" as global it works all of sudden. But why do I have to declare a variable global which I'm using locally inside the same function??? Is is because I'm using a global function (FCOIsMarked(...)) inside my function? I don't think so... Thanks for your help. |
|
01/31/15, 06:31 PM | #2 |
|
Your local variables are local to within that "if...end" statement. Once you pass that "end" your variables go out of scope. If you want them to be local to within the file, you need to declare them before the if statement:
Lua Code:
|
01/31/15, 07:58 PM | #3 |
|
Hey Baertram, here is what I see. I made some comments on your code with minor fixes showing the problems I see.
Your code with comments:
Warning: Spoiler
Lua Code:
Last edited by circonian : 01/31/15 at 10:57 PM. |
01/31/15, 08:47 PM | #4 |
Thx for the information katkat42 and circonian.
I didn't know that variables declared inside an if...end part won't be accessible outside this part. Other developing languages, other rules And about your suggestions: Thx, but the variable equiptmentMarkerControlName will be always the same. This is why I had filled it outside the for...do loop so it won't be overwritten with the same stuff over and over (8 times in total sometimes) again. And thank you for the "leaking to global" tip as well. I thought this _ was intended to be that local dummy, coming from LUA itsself. Or is it just a normal variable and someone has named it _, but it has nothing to do with LUA itsself? Last edited by Baertram : 01/31/15 at 09:04 PM. |
|
01/31/15, 08:56 PM | #5 | |
|
its just a variable, just like naming something x or markerId. You can assign things to it like Lua Code:
I guess its just typically used for "unwanted" things because it is small, easy to identify (hard to mix it up with anything else), and naming it unwantedReturnValue or something would just cluter up code. _ is nice and small & easy to ignore. |
|
01/31/15, 09:03 PM | #6 |
Ok, thanks for the next explanation ;-)
|
|
02/01/15, 05:13 AM | #7 |
|
Two small tips:
1) to strip N characters from the end, you can use negative end index: Lua Code:
2) to strip all digits from the end, you can use pattern replacement: Lua Code:
edit: fixed calls, guess my Lua is waning Last edited by merlight : 02/01/15 at 05:19 AM. |
02/01/15, 11:25 AM | #8 |
Thx merlight, didn't know the negative index works. This is pretty faster then, great!
|
|
ESOUI » Developer Discussions » Lua/XML Help » Problem with local variable (it is NIL all of sudden)? |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|