Jump to content




Loading strings for variable naming


  • You cannot reply to this topic
28 replies to this topic

#21 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 21 December 2012 - 06:15 AM

View PostPixelToast, on 21 December 2012 - 05:57 AM, said:

pcall(setfenv(function() code end,setmetatable({[string]=something},getfenv()))
if you are worried about os.run
That code is not even near flexible. Overriding the metatable of a function's environment table can be unwanted and isn't flexible at all. This code will get longer if you want to define multiple variables. And even then, he wants to access global variables defined somewhere else, not define new ones. And finally, you make a point of not using getfenv and then u use getfenv, setfenv and setmetatable... You can easily define
local G = getfenv()
at the top and use G from there on.

#22 Doyle3694

  • Members
  • 815 posts

Posted 21 December 2012 - 06:32 AM

OK, think I got it. will that make
function varCheck(var, stan)
   if var == nil then
	  getfenv()[var] = stan		  
   end							
end	

a working version?

#23 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 21 December 2012 - 06:35 AM

View PostDoyle3694, on 21 December 2012 - 06:32 AM, said:

OK, think I got it. will that make
function varCheck(var, stan)
   if var == nil then
	  getfenv()[var] = stan		  
   end							
end	

a working version?
No, you are still indexing a nil value. You wanted to get a variable by it's name, right? Then it would be like this:
function varCheck(varName, stan)
  getfenv()[varName] = getfenv()[varName] or stan
end	


#24 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 December 2012 - 06:36 AM

I very much doubt that nil is usable as an index.

#25 Doyle3694

  • Members
  • 815 posts

Posted 21 December 2012 - 06:39 AM

oh yeah, to tired to think now. Thanks. Understand of all of that very well. Just being an idiot not thinking of it being passed as a string rather than a value.

#26 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 21 December 2012 - 06:40 AM

View PostLyqyd, on 21 December 2012 - 06:36 AM, said:

I very much doubt that nil is usable as an index.
Indeed, it isn't. That's why his function couldn't ever work, but mine would. Just be sure to pass the varName as a string.

Edit: ok, all is done then. :)

#27 ChunLing

  • Members
  • 2,027 posts

Posted 21 December 2012 - 07:21 AM

You have to admit, you didn't explain that varName was the string matching var's identifier. Though you did call it varName...which...does that count as an explanation? I can't tell.

I mean, I can see a tired Doyle trying to pass tostring(var) into the function as varName, which would be the wrong interpretation but consistent with what you said.

Edited by ChunLing, 21 December 2012 - 07:23 AM.


#28 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 21 December 2012 - 07:25 AM

View PostChunLing, on 21 December 2012 - 07:21 AM, said:

You have to admit, you didn't explain that varName was the string matching var's identifier. Though you did call it varName...which...does that count as an explanation? I can't tell.
I thought it would have been clear from the numerous times I explained that earlier in this thread. I actually had the word string in the sentence earlier, but I couldn't get grammar right so I dropped it. :P Anyway, I wasn't clear, he didn't get something, asks for it, and I explain it better. It's how forums work.

#29 ChunLing

  • Members
  • 2,027 posts

Posted 21 December 2012 - 08:11 AM

Ideally. But this thread is full of "I shouldn't laugh because some of this is actually pretty difficult, but... :wacko: ".

I mean, I know I shouldn't laugh anyway, but sometimes I do.

Anyway, some of this really is just hard to get a handle on, no matter how well you explain it.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users