Add variable to variable

Hello,
I need to add a variable to another variable
Code:

Json
{
	"1": 
	{
		"name":"musketeer",
		"id":"hero",
		"skill":{
			"1":{
				"name":"test_skill_1_musketeer",
				"description":"desc_skill_1_musketeer",
				"doing":"-hp"
			},
			"2":{
				"name":"test_skill_2_musketeer",
				"description":"desc_skill_2_musketeer",
				"doing":"+hp"
			},
		},
	},
}

In the function:
for p,k in pairs(decoded2) do
	k.name.myname = k.name
	k.name = display.newImage("img/local/heroes/"..k.name.."/"..k.name..".png", _W/15, _H/1.8);
	k.name.id = k.id;
	k.name.hp = 100;

	for a,m in pairs(k.skill) do
		k.name.skill_a_id = a;
	end
end

what should it look like?
k.name.skill_a_id = a;

how I can get variable ? like:
musketeer.skill_1_id = 1;
musketeer.skill_ 2_id = 2;

You can do something like this:

local character = {}

character.id = "hero"
character.name = "musketeer"

character.skills = {}
character.skills[1] = {}
character.skills[1].name = "test skill 1"
character.skills[1].description = "description skill 1"

print (character.id, character.name, character.skills[1].name, character.skills[1].description)

Thanks for the advice! But is it possible to do this with automation?
I then just can add a variable to the for like

for p,k in pairs(decoded2) do
	k.name.myname = k.name
	k.name = display.newImage("img/local/heroes/"..k.name.."/"..k.name..".png", _W/15, _H/1.8);
	k.name.id = k.id;
	k.name.hp = 100;
	k.name.skill_1_id = 1;
	k.name.skill_2_id = 2;
end

But is it possible to somehow do as I wrote in the first post?

Sure. You can put this into a for loop or read json into that. You only need to change [1] part to your needs. The rest is the same.

@redbol - forgive me for asking, but why are you requiring such a complex way of storing arrays? What data are you attempting to keep track of? Perhaps we can help you simplify, resulting in easier to set and access variables and their attributes?

@troylyndon I don’t know why I am complicating things, apparently there is no experience to see simplification options…
To start, I want to bring out the skills and the heroes themselves. This is where the fun begins, I don’t know how, when choosing a hero’s skill, for example, “damage”, apply it to another hero.
Is it possible to somehow save skill data, such as damage or debuff, and apply it to another hero?

@redbol this is another way to do this…

local c_name=1
local c_id=2
local c_description=3
local c_hp=4
local c_skillList=5
local c_skillName=1
local c_skillDescription=2
local c_skillDoing=3

Then, I would create characters as simpler arrays, like this:

V={}  --to use for Global variables
V.characters[1]={}
V.characters[1][c_name]="musketeer"
V.characters[1][c_id]="hero"
V.characters[1][c_skillList]={}
V.characters[1][c_skillList][1]={}
V.characters[1][c_skillList][1][c_skillName]="test_skill_1_musketeer"
V.characters[1][c_skillList][1][c_skillDescription]="desc_skill_1_musketeer"
V.characters[1][c_skillList][1][c_skillDoing]="-hp"
V.characters[1][c_skillList][2]={}
V.characters[1][c_skillList][2][c_skillName]="test_skill_2_musketeer"
V.characters[1][c_skillList][2][c_skillDescription]="desc_skill_2_musketeer"
V.characters[1][c_skillList][2][c_skillDoing]="+hp"

In this way, you can easily access a characters data in a single line, for example, to find out how many skills a character has…
local numberOfSkillsOfCharacterOne=#V.characters[1][c_skillList]

to get what the first skill does, you reference it directly as…
local skillOfCharacterOneDoes=V.characters[1][c_skillList][1][c_skillDoing]

I don’t know how you are going to use your variables, or if you should have 1 character list and another completely different set of skills in its own list that can be referenced from within the character, to avoid having characters with duplicate data, but I think you get the idea here.

If it wasn’t clear enough, the number value after V.characters could be a player or character. Whereas the number value after the [c_skillList] represents a reference to the skill number of that player or character.

Hope this helps!

1 Like

If you are looking to assign data from a JSON string to tables, then the easiest way would loop through them, as you had initially suggested, but first you’d need to fix that JSON (as it isn’t valid as is) and then make changes to your loop too.

First, if you are going to create units/characters like that, i.e. loading them from a JSON string, then you should have all information required for creating them in said string. For instance, what if there is a character that doesn’t have 100 hp? You’d have to add conditional statements to the loop, but you could just add a new entry to the JSON for hp.

Also, you could just use JSON arrays for “skill” entry. Since you are using “1” and “2” keys, why not just store these as array entries 1 and 2? Finally, why not just store “img”, “width” and “height” in the string too? If you want to make things easier for you, then just throw them in and be happy.

So, the first thing I’d do is reformat your data to something like:

[
    {
	    "name": "musketeer",
        "id": "hero",
        "hp": "100",
        "img": "img/local/path/musketeer.png",
        "width": "40",
        "height": "30",
	    "skill":[
             {
			    "name": "test_skill_1_musketeer",
			    "description": "desc_skill_1_musketeer",
			    "doing": "-hp"
		    },
		    {
			    "name": "test_skill_2_musketeer",
			    "description": "desc_skill_2_musketeer",
			    "doing": "+hp"
		    }
	    ]
    }
]

Now, since you know the data structure of your JSON string, you can easily write a loop that will wrap things up nicely. However, you should not make changes to the original table like you do in your proposed loop, but instead:

local json = require( "json" )

-- Presuming the JSON data is stored in a file called "data.json" in the project root.
local path = system.pathForFile( "data.json" )
local file, errorString = io.open( path, "r" )
local contents = file:read( "*a" )
local sampleData = json.decode( contents )
io.close( file )

local unit = {}
for i = 1, #sampleData do
    local t = sampleData[i]
    unit[i] = display.newImageRect( t.img, t.width, t.height )
    unit[i].name = t.name
    unit[i].id = t.id
    unit[i].hp = t.hp
    unit[i].skill = {}
    for j = 1, #t.skill do
        unit[i].skill[j] = {}
        unit[i].skill[j].name = t.skill[j].name
        unit[i].skill[j].description = t.skill[j].description
        unit[i].skill[j].doing = t.skill[j].doing
    end
end

And now you have access to all units you’ve created as well as their original data values. Now, you could go even more streamlined and automate this entire process inside the loop without needing to explicitly assign a single variable, but this will get you started.

2 Likes

@bgmadclown, @troylyndon, @XeduR Thank you very much for the good explanation and examples! Your posts have been very helpful! :relaxed:

2 Likes