Forum

> > CS2D > Scripts > Converting string...?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Converting string...?

11 replies
To the start Previous 1 Next To the start

old Converting string...?

KenVo
User Off Offline

Quote
Example:
PLAYERS[id].Class = "Rogue"
--When I press the first button in my menu, PLAYERS[id].SROGUE = PLAYERS[id].SROGUE + 1
1
2
3
4
5
6
7
--cut
		local playerclass = ".S"..string.upper(PLAYERS[id].Class)
		if (button == 1) and PLAYERS[id].SkillPt > 0 then
			PLAYERS[id] .. playerclass ..[button] = PLAYERS[id] .. playerclass ..[button] + 1
			print(PLAYERS[id] .. playerclass ..[button])
		end	
--cut

instead of this:
1
2
3
4
5
6
7
8
9
10
11
12
if (button == 1) and PLAYERS[id].SkillPt > 0 then
			if PLAYERS[id].Class == "Rogue" then
				PLAYERS[id].SROGUE[button] = PLAYERS[id].SROGUE[button] + 1
				print(PLAYERS[id].SROGUE[button])
			elseif PLAYERS[id].Class = "Warrior" then
				PLAYERS[id].SWARRIOR[button] = PLAYERS[id].SWARRIOR[button] + 1
				print(PLAYERS[id].SWARRIOR[button])
			elseif PLAYERS[id].Class = "Mage" then
				PLAYERS[id].SMAGE[button] = PLAYERS[id].SMAGE[button] + 1
				print(PLAYERS[id].SMAGE[button])
			end	
		end

old Re: Converting string...?

Apache uwu
User Off Offline

Quote
You can try:

1
2
PLAYERS[id].Class = "Rogue"
PLAYERS[id]["S"..PLAYERS[id].Class] = PLAYERS[id]["S"..PLAYERS[id].Class] + 1

old Re: Converting string...?

Roni
User Off Offline

Quote
Is the cs2d even taking the first code o.O?

Try this it's a enhanced version form textual concept

LAYERS[id].Class = "Rogue"
PLAYERS[id][ playerclass ] = PLAYERS[id][playerclass] + 1

(sorry for the bad formatting I'm at my mobile right now)

old Re: Converting string...?

KenVo
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
PLAYERS = {SROGUE = {0,0},Class = "Rogue"}
local playerclass = ".S"..string.upper(PLAYERS.Class).."[1]"
print(playerclass)
print(PLAYERS.SROGUE[1])
print("PLAYERS"..playerclass)
PLAYERS[playerclass] = PLAYERS[playerclass] + 1
print (PLAYERS[playerclass])

[code]
--OUT PUT--
.SROGUE[1]
0
PLAYERS.SROGUE[1]
input:6: attempt to perform arithmetic on field '?' (a nil value)

old Re: Converting string...?

Apache uwu
User Off Offline

Quote
Last move:

1
2
3
PLAYERS[id].Class = "Rogue"

loadstring("PLAYERS[id].S"..string.upper(PLAYERS[id].Class).."=PLAYERS[id].S"..string.upper(PLAYERS[id].Class).."+1")

old Re: Converting string...?

KenVo
User Off Offline

Quote
1
2
3
4
5
6
7
PLAYERS = {SROGUE = {0,0},Class = "Rogue"}
local playerclass = ".S"..string.upper(PLAYERS.Class).."[1]"
print(PLAYERS.SROGUE[1])
print("PLAYERS"..playerclass)
loadstring("PLAYERS.S"..string.upper(PLAYERS.Class).."[1] = PLAYERS.S"..string.upper(PLAYERS.Class).."[1] + 1")
--PLAYERS.SROGUE[1] = PLAYERS.SROGUE[1] + 1
print (PLAYERS.SROGUE[1])

No error but 'PLAYERS.SROGUE[1]' won't increase

1
2
3
4
--OUT PUT--
0
PLAYERS.SROGUE[1]
0

old Re: Converting string...?

EngiN33R
Moderator Off Offline

Quote
1
2
3
4
5
local playerclass="S"..string.upper(PLAYERS[id].Class)
	if (button==1) and PLAYERS[id].SkillPt > 0 then
		_G["PLAYERS["..id.."]."..playerclass.."["..button.."]"]=_G["PLAYERS["..id.."]."..playerclass.."["..button.."]"]+1
		print(_G["PLAYERS["..id.."]."..playerclass.."["..button.."]"])
	end

Just a wild idea. Will it work? Lemme know.
edited 1×, last 29.01.12 05:37:31 pm

old Re: Converting string...?

Roni
User Off Offline

Quote
1
2
3
4
5
6
PLAYERS = {SROGUE = {0,0},Class = "Rogue"}
local playerclass = "S"..string.upper(PLAYERS.Class)
print(playerclass)
print(PLAYERS.SROGUE[1])
PLAYERS[playerclass][1] = PLAYERS[playerclass][1] + 1
print (PLAYERS[playerclass][1])
try this.
you problem is that you try to form variable constructs into strings but then you´d need a load string (what textual context tried) but this is way to complicated for such an problem.

old Re: Converting string...?

KenVo
User Off Offline

Quote
user Roni has written
1
2
3
4
5
6
PLAYERS = {SROGUE = {0,0},Class = "Rogue"}
local playerclass = "S"..string.upper(PLAYERS.Class)
print(playerclass)
print(PLAYERS.SROGUE[1])
PLAYERS[playerclass][1] = PLAYERS[playerclass][1] + 1
print (PLAYERS[playerclass][1])
try this.
you problem is that you try to form variable constructs into strings but then you´d need a load string (what textual context tried) but this is way to complicated for such an problem.


I tried yours and it works, thank you !

But now I got another problem...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PLAYERS = {SK={1}, Class={"Rogue","Mage","Warrior"}}
ROGUESKILLS = {[1] ={name ={"Assasin's blade"}}}
print(PLAYERS.SK[1])	
print(ROGUESKILLS[1].name[1])
print(ROGUESKILLS[1].name[PLAYERS.SK[1]])

local classskill = string.upper(PLAYERS.Class[1]).. "SKILLS"
print(classskill)
if ROGUESKILLS[1].name[1] then
print("HI")
end

if classskill[1].name[1] then
print("NOOB")
end

OUTPUT
1
2
3
4
5
6
1
Assasin's blade
Assasin's blade
ROGUESKILLS
HI
input:13: attempt to index field '?' (a nil value)

I can't print 'NOOB' because 'classskill' has to be first
edited 3×, last 29.01.12 09:38:30 pm

old Re: Converting string...?

EngiN33R
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PLAYERS = {SK={1}, Class={"Rogue","Mage","Warrior"}}
ROGUESKILLS = {[1] ={name ={"Assasin's blade"}}}
print(PLAYERS.SK[1])     
print(ROGUESKILLS[1].name[1])
print(ROGUESKILLS[1].name[PLAYERS.SK[1]])

local classskill = string.upper(PLAYERS.Class[1]).. "SKILLS"
print(classskill)
if ROGUESKILLS[1].name[1] then
print("HI")
end

if _G[classkill][1].name[1] then
print("NOOB")
end

Again, try it like this.

old Re: Converting string...?

KenVo
User Off Offline

Quote
user EngiN33R has written
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PLAYERS = {SK={1}, Class={"Rogue","Mage","Warrior"}}
ROGUESKILLS = {[1] ={name ={"Assasin's blade"}}}
print(PLAYERS.SK[1])     
print(ROGUESKILLS[1].name[1])
print(ROGUESKILLS[1].name[PLAYERS.SK[1]])

local classskill = string.upper(PLAYERS.Class[1]).. "SKILLS"
print(classskill)
if ROGUESKILLS[1].name[1] then
print("HI")
end

if _G[classkill][1].name[1] then
print("NOOB")
end

Again, try it like this.


1
2
3
4
5
6
1
Assasin's blade
Assasin's blade
ROGUESKILLS
HI
input:13: attempt to index field '?' (a nil value)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview