Help needed with variables and concatenation!

Sorry for the undescriptive title but, I am not sure what to call this?

So what I am trying to do is to use one function (named “buy”) to purchase an item. To try and keep the code shorter, I made a variable called “item” and one named “price”. When the user taps on the item to buy, it opens a ‘window’ with a purchase button and at the same time sets ‘item=“colorRed”’ and ‘price=“200”’. Then, if the user taps the purchase button it calls the “buy” function, which in turn, subtracts the price from their current coins. This function is also supposed set a variable (colorRed) to true in a table. To do this I am trying to do

(“gameData.” … item) = true

this gives me an error of “ambiguous syntax (function call x new statement) near ‘(’”

BTW, gameData is a table where I store all of the, well, game data.

So what am I doing wrong? Is this even possible?

Also, please forgive me because I am probably doing everything wrong… I am very new to programming!!

--this is not the actual code, but is a bit more simple, and hopefully helps make it easier to understand what I am trying to do item = "colorRed" price = 200 function Buy(event) if gameData.coins \>= price then gameData.coins = gameData.coins - price ("gameData." .. item) = true end end

I’m not going to pretend to be a lua master, but your piece here:

("gameData." .. item) = true

Is what is causing the mischief. I assume you don’t want to do this:

gameData.item = true

because you want to be able to iterate through a list and locate the item that was touched/bought/purchased/used? 

Hey thanks for the reply!!!  :slight_smile:

Yes that is what is causing my problem… But this still doesn’t quite do what I need.

I need where it says “item” to be the item that was originally tapped. So when I tap “colorRed” for example, it sets the variable, “item” to colorRed.

I want this so when I call the buy function, it does

gameData.colorRed = true

(colorRed is just basically an object you will be able to buy in game… I want it to store it’s value in a table, and when the game loads, if gameData.colorRed = true, then I can add colorRed into the game.)

gameData.item = true

This is almost correct, except it sets a value in the table named “item” to true, and I need it to set “colorRed” or “colorBlue” or whatever the “item” variable is set at, to be true.

I don’t even know if this is possible to do. I may just need to make a separate buy function for each object in the shop? 

I’m a little lost in what you’re trying to do, but that’s never stopped me from jumping in. :slight_smile:

Take a look at this code:

local itemTable = {colorRed=true, colorBlue=false} local gameData = {} gameData.item = itemTable print(gameData.item.colorRed)

If you run that code “true” will print in the console. You can change the different items like this:

gameData.item.colorBlue = true

So while I don’t know exactly what you’re doing, I’m pretty sure that using a table of items in the gameData.item property might give you a solution.

 Jay

The problem with your original code is that lua doesn’t know if you want to make a function call or are you starting a new statement:

gameData.coins = gameData.coins - price
(“gameData.” … item) = true

If you look at it you can see that your brackets could also be parameters for a function called price.

You can solve it by inserting a semi colon after price like this:

gameData.coins = gameData.coins - price;
(“gameData.” … item) = true

This line will produce an error also as you are trying to set true to a string.

I think what you want to do si

gameData[item] = true

Hey, thanks everyone for replying!!

And thanks to primoz.cerar!

He actually figured it out. That is what I was trying to do, I just wasn’t sure how to go about doing it. Or explaining what I was trying to do in the first place!  :slight_smile:

But once again, thanks for helping, guys! I am very new here, and this seems like a great community! :smiley:

I’m not going to pretend to be a lua master, but your piece here:

("gameData." .. item) = true

Is what is causing the mischief. I assume you don’t want to do this:

gameData.item = true

because you want to be able to iterate through a list and locate the item that was touched/bought/purchased/used? 

Hey thanks for the reply!!!  :slight_smile:

Yes that is what is causing my problem… But this still doesn’t quite do what I need.

I need where it says “item” to be the item that was originally tapped. So when I tap “colorRed” for example, it sets the variable, “item” to colorRed.

I want this so when I call the buy function, it does

gameData.colorRed = true

(colorRed is just basically an object you will be able to buy in game… I want it to store it’s value in a table, and when the game loads, if gameData.colorRed = true, then I can add colorRed into the game.)

gameData.item = true

This is almost correct, except it sets a value in the table named “item” to true, and I need it to set “colorRed” or “colorBlue” or whatever the “item” variable is set at, to be true.

I don’t even know if this is possible to do. I may just need to make a separate buy function for each object in the shop? 

I’m a little lost in what you’re trying to do, but that’s never stopped me from jumping in. :slight_smile:

Take a look at this code:

local itemTable = {colorRed=true, colorBlue=false} local gameData = {} gameData.item = itemTable print(gameData.item.colorRed)

If you run that code “true” will print in the console. You can change the different items like this:

gameData.item.colorBlue = true

So while I don’t know exactly what you’re doing, I’m pretty sure that using a table of items in the gameData.item property might give you a solution.

 Jay

The problem with your original code is that lua doesn’t know if you want to make a function call or are you starting a new statement:

gameData.coins = gameData.coins - price
(“gameData.” … item) = true

If you look at it you can see that your brackets could also be parameters for a function called price.

You can solve it by inserting a semi colon after price like this:

gameData.coins = gameData.coins - price;
(“gameData.” … item) = true

This line will produce an error also as you are trying to set true to a string.

I think what you want to do si

gameData[item] = true

Hey, thanks everyone for replying!!

And thanks to primoz.cerar!

He actually figured it out. That is what I was trying to do, I just wasn’t sure how to go about doing it. Or explaining what I was trying to do in the first place!  :slight_smile:

But once again, thanks for helping, guys! I am very new here, and this seems like a great community! :smiley: