Jump to content
Sign in to follow this  
prismite

Variables that contain Variables?

Recommended Posts

Hi all!

GAH! This is hard to explain!

So, lets say i have a variable that gets assigned a random number ... 1 through 10. There is an existing function for all 10 of those numbers, so the random number is really determining which of the functions gets run. 

1 = books

2 = movies

3 = comics

.

.

10 = songs

Once the function has been called and activated, lets say i have 10 results here, each a book/movie/comic/etc that matches its corresponding category. The way i have it setup now is sort of like this:

Func Book_Choice (ByRef $Book)
   $RandomBook = Random (1, 10, 1)
   If $RandomBook = 1 Then $Book = "True Blood"

Now, is there a way to expand upon this so that the If/Then statement could be something like:

Func Book_Choice (ByRef $Book)
   $RandomBook = Random (1, 10, 1)
   If $RandomBook = 1 Then $Book = "True Blood" and $Author = "Charlene Harris" and Year = "2010"

Let me iterate, I know that code is wrong, but thats the only way I can express what I am trying to accomplish, lol. Is there a way to do this?

Share this post


Link to post
Share on other sites

You're right that it's badly explained.

Is this what you want?

Func Book_Choice(ByRef $Book)
    $RandomBook = Random(1, 10, 1)
    If $RandomBook = 1 Then
        $Book = "True Blood"
        $Author = "Charlene Harris"
        $Year = "2010"
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

This uses random indexes of arrays to randomly access the contents of the arrays.

Local $aCategory = ["", "$aBooks", "$aMovies", "$aComics"]

; $aBooks array [0]-Title; [1]- Author; [2]-Year
Local $aBooks = [["True Blood", "Charlene Harris", "2010"], _
        ["Title1", "Author 1", "Year1"], _
        ["Title2", "Author 2", "Year2"]]

; $aBooks array [0]-Title; [1]- Author; [2]-Year
Local $aMovies = [["TitleM0", "Author M0", "YearM0"], _
        ["TitleM1", "Author M1", "YearM1"], _
        ["TitleM2", "Author M2", "YearM2"]]

; $aBooks array [0]-Title; [1]- Author; [2]-Year
Local $aComics = [["TitleC0", "Author C0", "YearC0"], _
        ["TitleC1", "Author C1", "YearC1"], _
        ["TitleC2", "Author C2", "YearC2"]]

For $i = 1 To 10
    Local $cat = $aCategory[Random(1, UBound($aCategory) - 1, 1)] ; Get random category
    Local $iSel = Random(0, UBound(Execute($cat)) - 1, 1) ;  Get random selection within chosen category.

    ConsoleWrite($i & "/" & @TAB & _
            "Category: " & $cat & @TAB & _
            Execute($cat & "[" & $iSel & "][0]") & @TAB & _
            Execute($cat & "[" & $iSel & "][1]") & @TAB & _
            Execute($cat & "[" & $iSel & "][2]") & @LF)
Next
Edited by Malkey

Share this post


Link to post
Share on other sites

Quite, and there is of course the scripting dictionary object.

>Here and >here are some examples of using a dictionary as a local static variable instead of a global.

Share this post


Link to post
Share on other sites

Ok, obviously I suck at explain, so I'll be more candid. 

The program I'm working on is an aid for a game. The point of the program is to quickly and randomly determine multiple factors. In this case, the armament of an individual. So I more literally do this:

Func RandomGuy ()
  ...
   $Pistol = ""
   $Blade = ""
   $Club = ""
   $Armor = ""
   $ARating = ""
   $Damage = ""
   $Range = ""

   Pistol ($Pistol, $Damage, $Range)
   Blades ($Blade, $Damage)
   Armor ($Armor, $ARating)
  ...
EndFunc

Func Blades (ByRef $Blade, $Damage)
   $RandomKnife = Random (1, 17, 1)
   If $RandomKnife = 1 Then 
      $Blade = "Sharp Stick"
      $Damage = "1"
   If $RandomKnife = 2 Then
      $Blade = "Butter Knife"
      $Damage = "1"

(etc etc)
EndFunc

My problem is that the end result is that I dont seem to be able to pull the data I'm looking for. John, your suggestion is a good one, and was actually the first thing I tried. However, the program doesnt seem to retrieve anything other than the first variable. In this above example, it would only pull $Blade but never seems to populate the $Damage one, so I was beginning to think that there must be a special delimiter I'm missing ... something that declares to the program that it needs to read ALL the data in the IF/Then, not just 1 line.

 I'm not smart enough to do Arrarys or Objects just yet. I'm getting there, but I'm working with what I know first.

Edited by prismite

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...