2

I have a script where I have many variables for different guns. There are

UMP45Ammo = false
UMP45Fov = false

AKMAmmo = false
AKMFov = false

etc I got like 12 guns and more variables

Then I have a global variable called cgun

My problem is when a button is clicked I wanna change the value of selected guns specific property(like ammo to true)

So I tried something like
cgun+Ammo = true and I tried even making new local variable

mgun1 = cgun+Ammo 
mgun1 = true

etc. How can I do this or is it impossible?

4
  • Have you looked into tables? Commented Jul 20, 2020 at 14:02
  • Can you simplify how tables work with this since I cannot seem to see how it works.Thanks! Commented Jul 20, 2020 at 14:06
  • It allows you to access variables by string. Commented Jul 20, 2020 at 14:14
  • 1
    Read the manual (or at least try reading it before asking the internet) Commented Jul 20, 2020 at 14:19

2 Answers 2

4

It may be better to use objects to describe the guns. Then it's easy to change a gun property of the current gun.

-- gun list
UMP45 = {Ammo = false, Fov = false}
AKMA = {Ammo = false, Fov = false}

cgun = UMP45  -- set current gun
print(cgun.Ammo)

cgun.Ammo = true -- set Ammo of current gun
print(cgun.Ammo)
Sign up to request clarification or add additional context in comments.

3 Comments

Will try ill come say if it works gotta edit the script alot to make that work
Worked Thank you Mike67!
Please confirm this answer to get the post off the "No answer" list. Thanks.
0

Yes, it is possible to access global memory using the _G variable. It works, but it's ugly in this case:

_G[cgun.."Ammo"] = 0

Note that if cgun is a reference you have to change it to a string, either using debug.getlocal or using a simple search:

 for k, v in pairs(_G) do if v == cgun then _G[k.."Ammo"] = 0 break end

Again this is not a very good solution. Generally speaking, you don't want to pollute the global space _G with a lot of variables.

Just store a reference to your current gun as Mike suggested.

currentGun = { ammo = 10, name = "AK47" }

5 Comments

this answer is utter non-sense. why would iterate over _G? and what is _G[cgun+"Ammo"] ? it will cause a Lua error for attempting to add string to whatever cgun is
@Piglet It does make sense; the whole second code block addresses the cgun not being a string part. It's ugly, but 2dengine acknowledged that already. It's an example to illustrate how the global environment works and it might be a reasonable aproach when you can't control how variables are stored.
@DarkWiiPlayer I can't follow you. no matter what cgun is, cgun+"Ammo" will cause an error and so does k+"Ammo" am I missing something here?
cgun+"Ammo" will work if cgun is a string where AK47 = true AK47Ammo = 10 and cgun = "AK74" (see original question). If cgun is a reference such as AK47 = {} cgun = AK47 then you need to resolve it to a string - hence the iteration. I tried to answer the original question of whether cgun+"Ammo" could work. Yes, it could work, but it's not a good solution.
String concatenation in Lua is "..", not the plus sign. The only other thing the "+" operator works for is tables with defined metatable methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.