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 » Scripting Helpers
Home Search
 

Re: Global functions and Local functions.

Previous Thread :: Next Thread 
As8D is online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
08 May 2013 01:43 PM
Hello!
Today, me, Jobro and VirtualUser tried to solve this problem:

-------------------------------------------------------------------------------------

You declare a global function in Script1, like this:

_G.myFunction = function(...)
print(script:GetFullName(), ...)
end

And then, in Script2, you call the function.

_G.myFunction()

But!
-------------------------------------------------------------------------------------

The problem is that the function in Script1 will need to know the environment of the script that called it.

To make the problem easier to understand:

The function is made in Script1

When we call the function in Script2, it will be... yeah, called from Script2, but...

It will run in Script1, therefore making it print
Script1

Instead of Script2.


Jobro tried to make the discovery he made yesterday hook up all functions and give them an argument, allowing us to get the required variables. This is not succeeded yet, so I'm posting the problem here.


So, you can also think of the process like this:

Script1:
Make function, starting as global.

Script2:
Getting global function.
Turning it local. (lolhow... that's the problem? Or maybe another solution?)
Calling the local function.



Thank you for your time,
- As, we need a kingdom for scripters, right? :] I mean... like a world. Visually. Bam!
Report Abuse
Dr01d3k4 is not online. Dr01d3k4
Joined: 11 Oct 2007
Total Posts: 17916
08 May 2013 01:48 PM
Guessing here:

Script1.lua
_G.testFunc = -> print script\GetFullName!

Script2.lua
repeat wait! until _G.testFunc
getfenv(0).testFunc = _G.testFunc
testFunc!

Idk? Some hacky thing with get/set fenv?
Report Abuse
As8D is online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
08 May 2013 02:08 PM
No, that wouldn't work...

It would still be referring to _G.testFunc, which would run in Script1...

If you want to, you can join our Skype chatroom? :P

dekkonot, Jobro13, Fattycat(something), Prehistoricman, Trioxide, Me, Luke111000, VirtualUser, WaffleWarrior36... and some guys I don't know.


- As,
Report Abuse
Dr01d3k4 is not online. Dr01d3k4
Joined: 11 Oct 2007
Total Posts: 17916
08 May 2013 02:10 PM
Can you set the fenv of another script? If you can then you could do:

getfenv(workspace.Script1).script = script
_G.testFunc!
getfenv(workspace.Script1).script = workspace.Script1


I may join the skype.
Report Abuse
As8D is online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
08 May 2013 02:14 PM
Sorry, you will need a number (ID) in order to set the environment of something.

Maybe functions work too, but nothing else.


Ok, I'll PM u.

- As, SunScript :D Hehe...
Report Abuse
Dr01d3k4 is not online. Dr01d3k4
Joined: 11 Oct 2007
Total Posts: 17916
08 May 2013 02:19 PM
Hm... am I allowed to do this:

Script1:
_G.testFunc = -> print script\GetFullName!

setmetatable _G, __index: (ind) => if ind == "fenv" then getfenv!


Script 2:
_G.fenv.script = script
_G.testFunc!
Report Abuse
As8D is online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
08 May 2013 03:53 PM
_______________________________

Okay, this problem isn't fixed yet.

What we want to accomplish is to turn a global function into a local function. This includes that it should NOT be required to post the script or it's environment into a global table, or try to fake this...


Tell me if you want to join our Skype chat, and Jobro have a shared document that we can write on.


- As, PM me for cake... no... Argh! So many PMs!!!
Report Abuse
getkoed2 is not online. getkoed2
Joined: 11 Apr 2010
Total Posts: 1144
08 May 2013 04:02 PM
-- script 1
_G.myFunction = function(obj, ...)
print(obj:GetFullName(), ...)
end

-- script 2
_G.myFunction(script)

hurr
Report Abuse
As8D is online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
08 May 2013 04:19 PM
Well..

The problem is that I have to play around with some variables located in, let's say one LocalScript, while the API is created in another LocalScript.

Also, I don't like having to add in arguments to fake the goal of this problem.


- As, 3 billion KOs... to random players within 20 years.
Report Abuse
getkoed2 is not online. getkoed2
Joined: 11 Apr 2010
Total Posts: 1144
08 May 2013 04:25 PM
Sorry, I have no idea then.
Report Abuse
As8D is online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
08 May 2013 04:35 PM
Ohwell, ok.

Wanna join Skype chat? Thing...


Though it's somewhat derailed from any scripting topic. x_x


*Sleeping on my keyboard*zzzzzz


- As, am I dreaming? xaszzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Report Abuse
As8D is online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
08 May 2013 04:41 PM
oh yeah...

If there is a way to "clone" a function, while we can see that it changes memory address, then the problem is fixed!


ANYONE, feel free to come with suggestions and ideas!


- As, iwannagosleepnowbutyourekeepingmeawakendamnyouhehe.
Report Abuse
littleAU999 is not online. littleAU999
Joined: 15 Jan 2011
Total Posts: 1375
08 May 2013 05:04 PM
I'll try to figure this out l8r,I gotta go to church now
If I get a solution I'll post back on hare.
o3o
Report Abuse
littleAU999 is not online. littleAU999
Joined: 15 Jan 2011
Total Posts: 1375
08 May 2013 05:16 PM
Am I allowed to this:

(ran in a sb)

Script 1:

_G.test = function()
print(script:GetFullName());
end;

Script 2:

script.Name = "hi";

setfenv(_G.test,{print=print,script=script});

_G.test();

> Workspace.hi

Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
08 May 2013 06:04 PM
MoonScript is awesome.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
08 May 2013 06:53 PM
"Am I allowed to this:

(ran in a sb)

Script 1:

_G.test = function()
print(script:GetFullName());
end;

Script 2:

script.Name = "hi";

setfenv(_G.test,{print=print,script=script});

_G.test();

> Workspace.hi"



setfenv(_G.test, getfenv(0))
_G.test()


This should solve the problem. Good work. :)
Report Abuse
littleAU999 is not online. littleAU999
Joined: 15 Jan 2011
Total Posts: 1375
08 May 2013 08:53 PM
Thanks!
Report Abuse
As8D is online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
09 May 2013 05:31 AM
Ohwell, if there's no other ways to do it...

I just really don't want to have to overwrite a global variable to fake what is the goal of this problemosh... You know, if I forget to do the setfenv. ouch.


- As, ohwell... maybe not completely bad. Hm. But it's not the real ware.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
09 May 2013 06:21 PM
You could do a global function like this:


_G.Do = function(env, func, ...)
    setfenv(func, env)
    func(...)
end


_G.Do(getfenv(0), _G.Test)



That way you won't ever forget to set the environment.
Report Abuse
As8D is online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
09 May 2013 06:39 PM
Well...

I dropped the idea about making an API (which was the reason for this problem), as... I already had made nearly all of the stuff the API should do.

I have a neet API sheet I love to spread around, so PM me if u want it too c: Not that you can use it very much, but it might give some inspiration... somehow... *cough*...


- As, sorry not for saying this before. But thank you all, it seems like a nice solution :P
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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