Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Setting Variables True/False in Tables?

Asked by 8 years ago
ModeString = {"Normal", "Colored"}
ModeValue = {_G.normal, _G.colored}

function PickMode()
    local nextmode = math.random(#ModeString)
    Talk("The gamemode is ".. ModeString[nextmode],2, "")
    ModeValue[nextmode] = true
end

function ResetMode()
    for i,v in pairs(ModeValue) do
        v = false
    end
end

This is a portion of a functioning script. These functions, however, aren't working entirely. The non-functioning portion is where I try to set a global variable as true

ModeValue[nextmode] = true The code works perfectly up until this line. It seems to skip this line without displaying an error in the output. Also, ResetMode()obviously doesn't work; I used the same thought process as the line above. Is there an error in this code, or is this simply not possible?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Changing v changes v. v is not linked up to the corresponding thing in the table, or where it came from.

The same is said for how you use _G.normal. The list doesn't remember where the expression came from. It'll simply drop a false or true in there. Modifying ModeValue[nextmode] modifies ModeValue. You didn't tell it to change _G so it won't.




For ResetMode:

In order to modify ModeValue, you have to explicitly reference it:

        ModeValue[i] = false



For PickMode:

The simplest way is to store the name:

ModeValue = {"normal", "colored"}

----

_G[ModeValue[nextmode]] = true

however at that point you might as well just use ModeString instead.




Is there a particular reason you're using booleans in _G instead of something more controlled, e.g., a list of modules to run, for example?

0
Thanks for your help! Currently, I have a main script which controls the majority of what happens in the game. It indirectly controls another script by activating and deactivating it. This second script uses the Game Mode global variables to spawn things in a certain way for that specific gamemode. To answer the second part of your question, I don't know of a more controlled way. I don't even know ShiningWindow 127 — 8y
0
...what modules are for that matter. [Sorry, the comment got cut off] ShiningWindow 127 — 8y
0
This'll work, as you learn more you'll probably figure out a cleaner way to do it. BlueTaslem 18071 — 8y
Ad

Answer this question