[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Defining global variables
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Defining global variables

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Pages: 1 2  

Posted by Batista   (15 posts)  [Biography] bio
Date Sat 26 Aug 2006 12:09 AM (UTC)
Message
Okay, I already know that once the solution is posted I'll smack myself in the head and scream, "WHY DIDN'T I THINK OF THAT?!" But anywho...

For some reason, I'm having a ton of trouble trying to create a global variable in the script file. I'll readily admit that I'm a total newbie when it comes to using the script file, but I figured this would work:

global testvar = GetVariable("value")


or


global testvar

testvar = GetVariable("value")


I'm totally out of ideas. I'm not even sure if it's worth it. I'm wanting to save code time in triggers and aliases by not having to constantly redefine the same local variables I use in other scripts. :/
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sat 26 Aug 2006 12:51 AM (UTC)
Message
Quote:

testvar = GetVariable("value")


All variables are global in Lua by default, so that is all you have to do.

Inside a function, you can localize a variable like this:


function test (blah)
 local i, j = 5, 6

 print (i, j)

 foo = 42
end -- test


In this case both i and j are local to the function test. However foo is a global variable.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Gore   (207 posts)  [Biography] bio
Date Reply #2 on Mon 08 Jan 2007 01:47 PM (UTC)

Amended on Mon 08 Jan 2007 01:48 PM (UTC) by Gore

Message
Quote:
local i, j = 5, 6


I'm a complete lua newbie as well, what does that mean exactly?
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #3 on Mon 08 Jan 2007 05:34 PM (UTC)
Message
That declares two local variables, i and j, and assigns 5 to i and 6 to j.

In Lua, a list of values assigned to a list of variables will assign them one by one in the same order as they're given; excess values are thrown away and if a variable has no value it is set to nil.

E.g. a,b = 1 means b will be nil.

a,b = 1,2,3 means that a==1, b==2 and the 3 is thrown away.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Gore   (207 posts)  [Biography] bio
Date Reply #4 on Tue 09 Jan 2007 03:17 PM (UTC)
Message
thanks a bunch, how come in the tutorial it says you declare a variable with var ::=name or somesuch, but you can use global name or local name here?
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #5 on Tue 09 Jan 2007 09:11 PM (UTC)
Message
Much like any other language, Lua offers several declaration methods for variables. What the tutorial offers is the "textbook" examples that most new users will be most comfortable learning because they offer the "most sound" programming structure.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Gore   (207 posts)  [Biography] bio
Date Reply #6 on Wed 10 Jan 2007 04:56 AM (UTC)
Message
Ahhh I see. What would be the most "correct" way of doing things, doesit not really matter?

[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #7 on Wed 10 Jan 2007 05:43 AM (UTC)
Message
Generally you want all variables to be local unless they have to be global. It's cleaner, and avoids leaving variables all over the place. More importantly, it can prevent somewhat subtle bugs in code, if two functions rely on the same variable that should be local but gets modified somewhere else.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Gore   (207 posts)  [Biography] bio
Date Reply #8 on Wed 10 Jan 2007 06:07 AM (UTC)

Amended on Wed 10 Jan 2007 06:47 AM (UTC) by Gore

Message
So basically

var ::=variablename
Creates a global variable called variablename

variablename="whatever"

creates a global variable called variablename with the string "whatever" in it

local variablename="whatever"

does the same, but locally?

or even just

local variablename
global variablename

to make variables without anything in them?

target=""
Note (target)

function Target_Player (n,o,wildcard)
	target=wildcard[1];
end


Weird, if I put a global before the target I get the error:

Error number: 0
Event:        Compile error
Description:  [string "Script file"]:73: `=' expected near `target'
Called by:    Immediate execution

[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #9 on Wed 10 Jan 2007 07:54 AM (UTC)
Message
I've never seen Lua use the ::= notation. You don't need it, in any case. You might be confusing it with the manual's statement of the language grammar, e.g.
Quote:

    stat ::= varlist1 `=´ explist1
    varlist1 ::= var {`,´ var}
    explist1 ::= exp {`,´ exp}

Those aren't actually Lua statements; it's a meta-statement, if you will, describing what Lua code looks like.

If you don't prefix the first use of a variable in a given lexical scope with local, then it is global.

e.g.
local foo = 2 -- foo is now local in this scope
fo = 3 -- oops, created a new global variable


There is no way that I know of to create an empty global variable, other than perhaps just:
someglobal = nil
but I'm not sure why you'd want to do that.

However, typing just
local foo
means that from that point on in the lexical scope, foo will be local.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Gore   (207 posts)  [Biography] bio
Date Reply #10 on Wed 10 Jan 2007 09:08 AM (UTC)
Message
Ah heh. On my old mud, I would use a ton of variables for different things, on Lua is it worth it to group which variables I can by tables?

Say, if I have 4 variables for targetting, should I make a targetting table?

dim target, target_one, target_two, target_three

target =
	{
	cur = "",
	one = "",
	two = "",
	three = "",
	}


or is it not worth it? or is it more of a "do whatever you want" type of thing? heh
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #11 on Wed 10 Jan 2007 04:09 PM (UTC)
Message
I guess it depends. Having a table can make it easier to pass around a bunch of variables at the same time. It also lets you group things, which might be useful in some circumstances. Either way the usefulness is stylistic, not functional, I think. (But don't underestimate the importance of correct/good style.) Just do what makes the most sense to you, I suppose. :)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #12 on Thu 11 Jan 2007 03:26 AM (UTC)
Message
Quote:

or even just

local variablename
global variablename

...

Weird, if I put a global before the target I get the error:



There is no "global variablename" syntax - you can forget that, it was a guess by the original poster.

Variables are global by default (strictly speaking, they are looked up in the environment table that applies to the current function).

However if, in the lexical scope of a variable reference, that variable has been declared local, or is a function argument, then no table lookup occurs and the local variable is used.

You can't just look at a line like this:


a = 42


... and assume a global variable is being created/assigned. You would need to see if, further back, there was a local statement declaring "a", or if it is a function argument to the current function, or a parent function.

Quote:

... to make variables without anything in them?


There is no way of making a global variable with nothing in it. Doing this for example, simply deletes a current global variable, if one exists:


a = nil


It does not create an "empty" variable named "a".

You can, however make an empty local variable, eg.


local a = nil


In this case "= nil" is redundant, it would default to nil.



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #13 on Thu 11 Jan 2007 03:32 AM (UTC)
Message
Quote:

Say, if I have 4 variables for targetting, should I make a targetting table?


The usefulness of a table like you describe is, that you can pass the table around, as the table reference is passed (eg. to function calls).

So you could have:


function f (t)
  t.one = "Nick"
end -- function f

target =
	{
	cur = "",
	one = "",
	two = "",
	three = "",
	}

f (target)

print (target.one) --> Nick


However without using the table, parameters are passed by value, and this would not work:


function f (x)
  x = "Nick"
end -- function f


local target, target_one, target_two, target_three

f (target_one)

print (target_one)  --> nil


In this case we are passing target_one down *by value* and not by reference, so changing it inside a function merely changes the local copy. You may not want this.

Effectively, inside function f, the variable x is local, however it is initialized with the value that the function caller gives it.



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Gore   (207 posts)  [Biography] bio
Date Reply #14 on Thu 11 Jan 2007 04:59 AM (UTC)
Message
Lua is so much neater and more interesting than vbscript.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


28,434 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]