generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Sending all variables in the scope as a parameter?

Previous Thread :: Next Thread 
badgraphix is not online. badgraphix
Joined: 26 Nov 2007
Total Posts: 22800
07 Jun 2016 12:19 PM
Sometimes when I create a module, I just do it so I can segment my code more and make it more readable. The problem with a module is that unlike code within its home script, it doesn't have access to all the variables. I can manually insert the ones I need with parameters, but I'm wonder if there's a method to just send the whole scope of variables to a module, almost as if it was just contained within the same script that's using require() on it.
Report Abuse
GoogleAnalytics is not online. GoogleAnalytics
Joined: 08 Aug 2008
Total Posts: 455
07 Jun 2016 12:23 PM
ermm...


Just assign single table and type all your values inside
Report Abuse
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
07 Jun 2016 12:24 PM
You need getfenv, which takes a function's environment (i.e. all variables the function has access to) and send them in a table.

http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#getfenv
Report Abuse
GoogleAnalytics is not online. GoogleAnalytics
Joined: 08 Aug 2008
Total Posts: 455
07 Jun 2016 12:26 PM
It would erase old variables since your chancing environment or not ?
Report Abuse
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
07 Jun 2016 12:47 PM
Nope, erasing old variables would be setfenv (I think).
Report Abuse
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
07 Jun 2016 12:48 PM
Also while I'm posting here, congrats on getting your account back badgraphix ;D
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
07 Jun 2016 03:14 PM
You can't use getfenv in this case, because that returns the function environment (the GLOBAL VARIABLES of that function). If you wanted to do that you would have to make all the variables global AND give the function its own environment (that ofc. should fallback to the original one) so your new globals don't clutter up the global environment.

local function sendExample()
var1 = 5
var2 = 10
var3 = 20
blahblah(getfenv()) -- send the table of globals
end

setfenv(sendExample, setmetatable({}, {__index=getfenv()}))

If you wanted to send all the values alone, not the table of globals, you'd have to iterate over the environment and pack the values into an array and unpack the array.

The better solution would be to just put all the variables into a table or just know the name of the variables and send them explicitly.
Report Abuse
badgraphix is not online. badgraphix
Joined: 26 Nov 2007
Total Posts: 22800
08 Jun 2016 08:53 AM
Aw, that's unfortunate. I'm guessing it's unlikely we'll get a built-in function for this type of thing in the future.
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
08 Jun 2016 02:51 PM
I mean there _sort of_ is a built in feature, but it only exists in vanilla Lua and it's definitely not meant for this. Roblox removed almost everything from the debug library so yeah
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image