Randomizing from a list of variables

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Randomizing from a list of variables

James C. Wilson
Hi all,
 
I don't suppose there's a way to randomly reference a variable, is there?
Suppose I had four sounds, each preloaded into a local variable, perhaps named "local sound_1", local sound_2", etc, and wanted to play one of them at random every six seconds? I can do this by loading straight from the hard drive, but I'd like to be able to do it via locals, if possible.
 
Thanks,
 James
Reply | Threaded
Open this post in threaded view
|

Re: Randomizing from a list of variables

Urs Holzer-2
Hi James

James C. Wilson wrote:
> I don't suppose there's a way to randomly reference a variable, is
> there? Suppose I had four sounds, each preloaded into a local
> variable, perhaps named "local sound_1", local sound_2", etc, and
> wanted to play one of them at random every six seconds? I can do this
> by loading straight from the hard drive, but I'd like to be able to do
> it via locals, if possible.

Put them into an array instead. Say, the first sound in sound[1], the
second one in sound[2] and so on. Then generate a random number, like
that:

rand = math.random[sound.maxn]

Then you can use sound[rand] to get the sound you want to play.

I hope this is what you need.

Greetings
Urs



Reply | Threaded
Open this post in threaded view
|

Re: Randomizing from a list of variables

James C. Wilson
Thanks! I'm afraid I don't know what an array is, however...Could you explain if possible?
 
Thanks,
 James

From: Urs Holzer <[hidden email]>
To: [hidden email]
Sent: Tuesday, June 28, 2011 6:11 AM
Subject: Re: Randomizing from a list of variables

Hi James

James C. Wilson wrote:
> I don't suppose there's a way to randomly reference a variable, is
> there? Suppose I had four sounds, each preloaded into a local
> variable, perhaps named "local sound_1", local sound_2", etc, and
> wanted to play one of them at random every six seconds? I can do this
> by loading straight from the hard drive, but I'd like to be able to do
> it via locals, if possible.

Put them into an array instead. Say, the first sound in sound[1], the
second one in sound[2] and so on. Then generate a random number, like
that:

rand = math.random[sound.maxn]

Then you can use sound[rand] to get the sound you want to play.

I hope this is what you need.

Greetings
Urs


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Pipmak-Users mailing list
[hidden email]
news://news.gmane.org/gmane.games.devel.pipmak.user
https://lists.sourceforge.net/lists/listinfo/pipmak-users


Reply | Threaded
Open this post in threaded view
|

Re: Randomizing from a list of variables

Urs Holzer-2
Hi James

James C. Wilson wrote:
> Thanks! I'm afraid I don't know what an array is, however...Could you
> explain if possible?

A value in Lua can be of type table. A table actually associates a key
(which is also just a value) to another value. Of course it can have
many entries. Say foo is a table, then

foo["bar"]

returns the value associated to the key "bar". Using

foo["bar"] = 1

Sets the value associated to the key "bar" to 1. In other words, a table
stores values which you can read and set by key.

Lua does not know arrays as other programming languages do, since in
Lua, an array is realized by a table which only has integers as keys. So
if I say "array" here, I mean actually a table which has only integers
as keys.

Example:

local sound = {}  -- create a table
sound[1] = sound1  -- sound now associates the value sound1 to the key 1
sound[2] = sound2  -- sound now associates the value sound2 to the key 2
sound[3] = sound3  -- sound now associates the value sound3 to the key 3

local n = 2  -- Store value 2 in the variable n
sound[n]  -- Returns the value associated to the key 2

You can read up more about tables here: http://www.lua.org/pil/2.5.html

Description from http://en.wikipedia.org/wiki/Associative_array:
An associative array (also associative container, map, mapping,
dictionary, finite map, table, and in query-processing an index or index
file) is an abstract data type composed of a collection of unique keys
and a collection of values, where each key is associated with one value
(or set of values). The operation of finding the value associated with a
key is called a lookup or indexing, and this is the most important
operation supported by an associative array. The relationship between a
key and its value is sometimes called a mapping or binding. For example,
if the value associated with the key "bob" is 7, we say that our array
maps "bob" to 7. Associative arrays are very closely related to the
mathematical concept of a function with a finite domain. As a
consequence, a common and important use of associative arrays is in
memoization.

Hope this helps

Greetings
Urs



Reply | Threaded
Open this post in threaded view
|

Re: Randomizing from a list of variables

James C. Wilson
Thank you Urs! That worked spendidly, thank you.
 
-James

--- On Wed, 6/29/11, Urs Holzer <[hidden email]> wrote:

From: Urs Holzer <[hidden email]>
Subject: Re: Randomizing from a list of variables
To: [hidden email]
Date: Wednesday, June 29, 2011, 2:42 AM

Hi James

James C. Wilson wrote:
> Thanks! I'm afraid I don't know what an array is, however...Could you
> explain if possible?

A value in Lua can be of type table. A table actually associates a key
(which is also just a value) to another value. Of course it can have
many entries. Say foo is a table, then

foo["bar"]

returns the value associated to the key "bar". Using

foo["bar"] = 1

Sets the value associated to the key "bar" to 1. In other words, a table
stores values which you can read and set by key.

Lua does not know arrays as other programming languages do, since in
Lua, an array is realized by a table which only has integers as keys. So
if I say "array" here, I mean actually a table which has only integers
as keys.

Example:

local sound = {}  -- create a table
sound[1] = sound1  -- sound now associates the value sound1 to the key 1
sound[2] = sound2  -- sound now associates the value sound2 to the key 2
sound[3] = sound3  -- sound now associates the value sound3 to the key 3

local n = 2  -- Store value 2 in the variable n
sound[n]  -- Returns the value associated to the key 2

You can read up more about tables here: http://www.lua.org/pil/2.5.html

Description from http://en.wikipedia.org/wiki/Associative_array:
An associative array (also associative container, map, mapping,
dictionary, finite map, table, and in query-processing an index or index
file) is an abstract data type composed of a collection of unique keys
and a collection of values, where each key is associated with one value
(or set of values). The operation of finding the value associated with a
key is called a lookup or indexing, and this is the most important
operation supported by an associative array. The relationship between a
key and its value is sometimes called a mapping or binding. For example,
if the value associated with the key "bob" is 7, we say that our array
maps "bob" to 7. Associative arrays are very closely related to the
mathematical concept of a function with a finite domain. As a
consequence, a common and important use of associative arrays is in
memoization.

Hope this helps

Greetings
Urs


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Pipmak-Users mailing list
Pipmak-Users@...
news://news.gmane.org/gmane.games.devel.pipmak.user
https://lists.sourceforge.net/lists/listinfo/pipmak-users
Reply | Threaded
Open this post in threaded view
|

Re: Randomizing from a list of variables

James C. Wilson
In reply to this post by Urs Holzer-2


--- On Wed, 6/29/11, Urs Holzer <[hidden email]> wrote:

From: Urs Holzer <[hidden email]>
Subject: Re: Randomizing from a list of variables
To: [hidden email]
Date: Wednesday, June 29, 2011, 2:42 AM

Hi James

James C. Wilson wrote:
> Thanks! I'm afraid I don't know what an array is, however...Could you
> explain if possible?

A value in Lua can be of type table. A table actually associates a key
(which is also just a value) to another value. Of course it can have
many entries. Say foo is a table, then

foo["bar"]

returns the value associated to the key "bar". Using

foo["bar"] = 1

Sets the value associated to the key "bar" to 1. In other words, a table
stores values which you can read and set by key.

Lua does not know arrays as other programming languages do, since in
Lua, an array is realized by a table which only has integers as keys. So
if I say "array" here, I mean actually a table which has only integers
as keys.

Example:

local sound = {}  -- create a table
sound[1] = sound1  -- sound now associates the value sound1 to the key 1
sound[2] = sound2  -- sound now associates the value sound2 to the key 2
sound[3] = sound3  -- sound now associates the value sound3 to the key 3

local n = 2  -- Store value 2 in the variable n
sound[n]  -- Returns the value associated to the key 2

You can read up more about tables here: http://www.lua.org/pil/2.5.html

Description from http://en.wikipedia.org/wiki/Associative_array:
An associative array (also associative container, map, mapping,
dictionary, finite map, table, and in query-processing an index or index
file) is an abstract data type composed of a collection of unique keys
and a collection of values, where each key is associated with one value
(or set of values). The operation of finding the value associated with a
key is called a lookup or indexing, and this is the most important
operation supported by an associative array. The relationship between a
key and its value is sometimes called a mapping or binding. For example,
if the value associated with the key "bob" is 7, we say that our array
maps "bob" to 7. Associative arrays are very closely related to the
mathematical concept of a function with a finite domain. As a
consequence, a common and important use of associative arrays is in
memoization.

Hope this helps

Greetings
Urs


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Pipmak-Users mailing list
Pipmak-Users@...
news://news.gmane.org/gmane.games.devel.pipmak.user
https://lists.sourceforge.net/lists/listinfo/pipmak-users