Jump to content

Is it possible? Variable in the variable name


VortDyn

Recommended Posts

Can I create a variable in the variable name?

Timer{playerName} = setTimer ( function(), 1000, 0 )
If the player has the nickname "Palm", then the variable "TimerPalm" should be created
If the player has the nickname "Nick", then the variable "TimerNick" should be created

Edited by VortDyn
Link to comment
  • VortDyn changed the title to Is it possible? Variable in the variable name
  • Moderators

Hello @VortDyn,

I moved your topic to the Scripting section.

The Tutorials section is only for Scripting Tutorials, for scripting questions, use the Scripting section instead.

Make sure to use this section for your next scripting questions.

  • Like 1
Link to comment

The global environment (the table holding the global variables) can be accessed using variable _G.

local playerName = getPlayerName( player )
local variableName = "Timer"+playerName
_G[ variableName ] = setTimer ( function() end, 1000, 0 )

However, building variable names is usually not recommended for generic manipulation of data. Instead of putting the timers into global variable space, you can create your own table specifically for timers:

-- at the top of the script
playerTimers = {}

-- then, somewhere in the code
local playerName = getPlayerName( player )
playerTimers[ playerName ] = setTimer ( function() end, 1000, 0 )

But I suspect that's still not exactly what you want to do. Should the timer be linked to player's name, or to the player? If it's linked to the player's name, then if the player changes their name during the game, playerTimes[playerName] will no longer refer to the timer created before the change. If that's not what you want, you can bypass the retrieval of the name and just use the player element directly as table key:

-- at the top of the script
playerTimers = {}

-- then, somewhere in the code
playerTimers[ player ] = setTimer ( function() end, 1000, 0 )
  • Like 1
  • Thanks 1
Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...