Forum

> > CS2D > Scripts > Table generator
Forums overviewCS2D overview Scripts overviewLog in to reply

English Table generator

18 replies
To the start Previous 1 Next To the start

old Table generator

KenVo
User Off Offline

Quote
Hey guys! I'm working on my rpg project, and I need someone to help me with item affixes. So, an item table is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
[300] = {
		name = "leather helmet", 
		desc = "Protect yourself from headshots!", 
		r = 128, g = 64, b = 0, 
		action = "equip", 
		slot = 1, 
		eimage = "gfx/weiwen/helmet.png", 
		fimage = "gfx/weiwen/helmet.png", 
		def = 0.05, 
		speed = -1,
		func = equip,
	},

It would take me a long time to add all item's affixes to all items. For example, I want to add:
- Speed bonus
- Health bonus
- Magic dmg bonus
- Attack dmg bonus
- Magic resist bonus
- Physical resist bonus
- +EXP bonus
- +Money drop bonus
- +Item drop chance bonus
- Dodge bonus
- Critical chance bonus
- Critical dmg bonus
- Mana per 5 secs bonus
- Health per 5 secs bonus
- ETC............

I would have to add those item affixes to each item ID, and there are a lot of combinations like:

Item 301: Health bonus
Item 302: Health bonus + Magic resist bonus
Item 303: Health bonus + Speed bonus
Item 304: ETC.........................

There could be over 100 tables for each item that I want to add. Can someone give me an idea how to generate table (items) randomly with different item bonus to each item?

old Re: Table generator

EngiN33R
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
order = {"name","desc","r","g","b","action","slot","eimage","fimage","def","atk","speed"}

function generateItem(...)
	local t={};
	for _,v in pairs(args) do
		for _,vv in pairs(order) do
			t[vv]=v
		end
	end
	return t
end

I think this should work. The three dots in the arguments section is intentional! Don't change it to anything else!

The order table is the stats of your item, and the arguments for the generateItems function will be added strictly following that order - i.e. name first, desc second, r third, etc.

old Re: Table generator

KenVo
User Off Offline

Quote
Sorry for this stupid question but how do I use that function? Can you give me an example?

old Re: Table generator

EngiN33R
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
order = {"name","desc","r","g","b","action","slot","eimage","fimage","def","speed","func"}

function generateItem(...)
     local t={};
     for _,v in pairs(args) do
          for _,vv in pairs(order) do
               t[vv]=v
          end
     end
     return t
end

items[300] = generateItem("leather helmet","Protect yourself from headshots!",128,64,0,"equip",1,"gfx/weiwen/helmet.png","gfx/weiwen/helmet.png",0.05,-1,equip) -- I don't know how Tibia's item table is called

That way you'll generate the same table you have in the OP. It's not the most efficient way, but it works. You can see that the order table contains all fields that were in the OP and the arguments supplied to the generator function are in that strict order.

old Re: Table generator

KenVo
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
ITEMS={
	[300] = {
		name = "leather helmet", 
		desc = "Protect yourself from headshots!", 
		r = 128, g = 64, b = 0, 
		action = "equip", 
		slot = 1, 
		eimage = "gfx/weiwen/helmet.png", 
		fimage = "gfx/weiwen/helmet.png", 
		def = 0.05, 
		speed = -1,
		func = equip,
	},
}
order = {"name","desc","r","g","b","action","slot","eimage","fimage","def","speed","func"}
args={magic=1}

function generateItem(...)
local t={};
for _,v in pairs(args) do
for _,vv in pairs(order) do
t[vv]=v
end
end
return t
end

ITEMS[300] = generateItem("leather helmet","Protect yourself from headshots!",128,64,0,"equip",1,"gfx/weiwen/helmet.png","gfx/weiwen/helmet.png",0.05,-1,equip,magic)
ITEMS[301] = generateItem(magic)

print(ITEMS[300].name)
print(ITEMS[301].name)
print(ITEMS[300].magic)

===OUTPUT===
1
1
nil

Ok, so I added table args(idk if i'm supposed to) otherwise it won't work. Then I added 'magic = 1' to the table and ',magic' after equip. I tried to print it out and it shows "nil". However, when I try to print the item's name, which is the first element of the item's table, it shows the magic value(1)
BTW I have no idea what OP means...

old Re: Table generator

EngiN33R
Moderator Off Offline

Quote
OP means opening post.

You should not have added the args table, it's generated each time a function is executed locally in the function and contains the function's arguments. Also, I made a mistake, it's not args - it's arg.

This code is fully working - I tested it myself.

1
2
3
4
5
6
7
8
9
10
11
order = {"name","desc","r","g","b","action","slot","eimage","fimage","def","speed","func"}

function generateItem(...)
local t={};
for k,v in pairs(arg) do
if k~="n" then -- the last item in the arg table is 'n', the number of items in the table
t[order[k]]=v
end
end
return t
end

I then executed the function like so and got this:

1
2
3
item = generateItem('leather helmet','Protect yourself from headshots!',128,64,0,'equip',1,'gfx/weiwen/helmet.png','gfx/weiwen/helmet.png',0.05,-1,equip)

print(item.name) --> leather helmet

old Re: Table generator

KenVo
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
order = {"name","desc","r","g","b","action","slot","eimage","fimage","def","speed","func"}

function generateItem(...)
	local t={};
	for k,v in pairs(arg) do --> input:5: bad argument #1 to 'pairs' (table expected, got nil)
		if k~="n" then -- the last item in the arg table is 'n', the number of items in the table
			t[order[k]]=v
		end
	end
	return t
end

item = generateItem('leather helmet','Protect yourself from headshots!',128,64,0,'equip',1,'gfx/weiwen/helmet.png','gfx/weiwen/helmet.png',0.05,-1,equip)

print(item.name)

I copy EXACTLY what you typed and I got this error

old Re: Table generator

Anders4000
User Off Offline

Quote
user KenVo has written
Code >


I copy EXACTLY what you typed and I got this error


What error did you get? (:

old Re: Table generator

Anders4000
User Off Offline

Quote
Sad, cus "var in pairs()" is the only thing in the script that i do not understand :p
Engineer will probably find a fix for you tomorrow though (:

old Re: Table generator

sheeL
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
order = {"name","desc","r","g","b","action","slot","eimage","fimage","def","speed","func"}

function generateItem()
     local t={};
     for k,v in pairs(arg) do
     if input:5: bad argument #1 to pairs (table expected, got nil)
          if k~="n" then 
               t[order[k]]=v
          end
     end
     return t
 end
end
item = generateItem('leather helmet','Protect yourself from headshots!',128,64,0,'equip',1,'gfx/weiwen/helmet.png','gfx/weiwen/helmet.png',0.05,-1,equip)

print(item,"name")

√ untested. try changing the values

old Re: Table generator

KenVo
User Off Offline

Quote
user sheeL has written
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
order = {"name","desc","r","g","b","action","slot","eimage","fimage","def","speed","func"}

function generateItem()
     local t={};
     for k,v in pairs(arg) do
     if input:5: bad argument #1 to pairs (table expected, got nil)
          if k~="n" then 
               t[order[k]]=v
          end
     end
     return t
 end
end
item = generateItem('leather helmet','Protect yourself from headshots!',128,64,0,'equip',1,'gfx/weiwen/helmet.png','gfx/weiwen/helmet.png',0.05,-1,equip)

print(item,"name")

√ untested. try changing the values


Wtf? I don't want to be mean but don't try to help other people if you have no idea what you are doing.
1
if input:5: bad argument #1 to pairs (table expected, got nil)
First,This was a comment and you fixed it to an if...,then... statement. You didn't even put then after it. Why would you do this anyway???

Second,
1
print(item,"name")
again, wtf?

Also, you removed the ... in the function, even though Engineer told me not to. Sorry, I just hate people that act like they know stuffs even though they don't.

old Re: Table generator

Flacko
User Off Offline

Quote
I tested it and it worked fine.
Although I'd have used ipairs or a for-loop instad of pairs

old Re: Table generator

sheeL
User Off Offline

Quote
user Flacko has written
I tested it and it worked fine.
Although I'd have used ipairs or a for-loop instad of pairs


@user KenVo: understand -.-'
GHT DETECTED --'

old Re: Table generator

Flacko
User Off Offline

Quote
Interesting.
The lua demo doesn't allow to use 'arg'
Using {...} instead made the trick.

Anyways, the code engineer posted works on CS2D
Although I'd make a small tweak:
1
2
3
4
5
6
7
function generateItem(...)
	local t={}
	for i=1, #arg do
		t[order[i]] = arg[i]
	end
	return t
end

old Re: Table generator

KenVo
User Off Offline

Quote
user sheeL has written
@user KenVo: understand -.-'
GHT DETECTED --'


What you said just doesn't make any sense. Also, use acronyms only when they make sense in a sentence.

@user Flacko: thank you

old Re: Table generator

EngiN33R
Moderator Off Offline

Quote
user Flacko has written
Although I'd make a small tweak:
1
2
3
4
5
6
7
function generateItem(...)
	local t={}
	for i=1, #arg do
		t[order[i]] = arg[i]
	end
	return t
end


Yeah, I guess I use pairs way too often.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview