Page 1 of 1

[{Done}] Why does my table not work

Posted: Mon Jun 18, 2018 7:39 pm
by TheSAguy
Hi,

I was hoping someone could spot why my below code is not working as intended.
I'm creating tables from 1 to 100.

The Scale works, but not the selection, collision or tint. Those do not work...

Code: Select all


require("prototypes.New_Units.ne-biter-animations")

--- Tint 
local ne_tank_tint = {}
local tint_r = 0

for i = 1, 100 do
	tint_r = tint_r + 1
	table.insert(ne_tank_tint, {{r=(tint_r/255), g=(tint_r/255), b=(tint_r/255), a=0.65}})			
end
	
--- Collision Box
local ne_collision_box = {}
local c1 = 0

for i = 1, 100 do
	c1 = c1 + 0.0057
	table.insert(ne_collision_box, {{-(0.0943 + c1), -(0.0943 + c1)}, {(0.0943 + c1), (0.0943 + c1)}})			
end
	
--- Selection Box
local ne_selection_box = {}
local s1 = 0
local s2 = 0
		
for i = 1, 100 do
	s1 = s1 + 0.0097
	s2 = s2 + 0.00234
	table.insert(ne_selection_box, {{-(0.1903 + s1), -(0.34766 + s2)}, {(0.34766 + s2), (0.19032 + s1)}})				
end

--- Scale
local ne_scale = {}
local scale = 0

for i = 1, 100 do
	scale = scale + 0.0175
	table.insert(ne_scale, 0.2325 + scale)
end



for i = 1, 100 do

    NE_Tank_Unit = table.deepcopy(data.raw.unit["small-biter"])
    NE_Tank_Unit.name = "ne-biter-tank-" .. i
    NE_Tank_Unit.collision_box = ne_collision_box[i]
    NE_Tank_Unit.selection_box = ne_selection_box[i]
    NE_Tank_Unit.max_health = i
    NE_Tank_Unit.attack_parameters.animation = ne_biter_attack_animation(ne_scale[i], ne_tank_tint[i], ne_tank_tint[i])
    NE_Tank_Unit.run_animation = ne_biter_run_animation(ne_scale[i], ne_tank_tint[i], ne_tank_tint[i])
    
	data:extend{NE_Tank_Unit}

end
  

Thanks.

Re: Why does my table not work

Posted: Mon Jun 18, 2018 8:03 pm
by eradicator
Care to be significantly more precise than "It ain't workin!" :P?
Also why are you using 5 seperate loops instead of just one? (rethorical question)

Re: Why does my table not work

Posted: Mon Jun 18, 2018 8:44 pm
by TheSAguy
Scale, Health, Name all work, but the Selection, Collision and Tint does not work.

Image

Updated code:

Code: Select all

require("prototypes.New_Units.ne-biter-animations")


local ne_tank_tint = {}
local tint_r = 0
local ne_collision_box = {}
local c1 = 0
local ne_selection_box = {}
local s1 = 0
local s2 = 0
local ne_scale = {}
local scale = 0


for i = 1, 100 do
	-- Tint
	tint_r = tint_r + 1
	table.insert(ne_tank_tint, {{r=(tint_r/255), g=(tint_r/255), b=(tint_r/255), a=0.65}})		
	-- Collision Box
	c1 = c1 + 0.0057
	table.insert(ne_collision_box, {{-(0.0943 + c1), -(0.0943 + c1)}, {(0.0943 + c1), (0.0943 + c1)}})	
	-- Selection Box
	s1 = s1 + 0.0097
	s2 = s2 + 0.00234
	table.insert(ne_selection_box, {{-(0.1903 + s1), -(0.34766 + s2)}, {(0.34766 + s2), (0.19032 + s1)}})	
	-- Scale
	scale = scale + 0.0175
	table.insert(ne_scale, 0.2325 + scale)
end


for i = 1, 100 do

NE_Tank_Unit = table.deepcopy(data.raw.unit["small-biter"])
NE_Tank_Unit.name = "ne-biter-tank-" .. i
NE_Tank_Unit.collision_box = ne_collision_box[i]
NE_Tank_Unit.selection_box = ne_selection_box[i]
NE_Tank_Unit.max_health = i
NE_Tank_Unit.attack_parameters.animation = ne_biter_attack_animation(ne_scale[i], ne_tank_tint[i], ne_tank_tint[i])
NE_Tank_Unit.run_animation = ne_biter_run_animation(ne_scale[i], ne_tank_tint[i], ne_tank_tint[i])
    
data:extend{NE_Tank_Unit}

end

Re: Why does my table not work

Posted: Mon Jun 18, 2018 11:27 pm
by eradicator
TheSAguy wrote:does not work.
Precise. Description. Of. Behavior.
What, how, when, why "does not work"? Do they taste like apple instead of orange? Is there some sort of error message?
Also with "remove the loops" i actually meant you should shove the whole thing into a single loop and get rid of all those temporary tables/variables.

Re: Why does my table not work

Posted: Mon Jun 18, 2018 11:48 pm
by TheSAguy
What does not work:

Collision and Selection box does not get bigger as it should. It staying very small.
Tint does not change color as it should.

The current loop should add to each and make it bigger or change the color slightly.
eradicator wrote:
Also with "remove the loops" i actually meant you should shove the whole thing into a single loop and get rid of all those temporary tables/variables.
Not exactly sure how to do this, since I thought I did :)

Re: Why does my table not work

Posted: Tue Jun 19, 2018 12:02 am
by eradicator
TheSAguy wrote:Not exactly sure how to do this, since I thought I did :)
I still count two loops there, and 9 unneeded temporary variables.

Code: Select all

--PSEUDOCODE:
--DIRECTLY USE:
NE_Tank_Unit.collision_box = {{-(0.0943 + c1 * i), -(0.0943 + c1 * i)}, {(0.0943 + c1 * i), (0.0943 + c1 * i)}}

--INSTEAD OF:
table.insert(ne_collision_box, {{-(0.0943 + c1), -(0.0943 + c1)}, {(0.0943 + c1), (0.0943 + c1)}})   
NE_Tank_Unit.collision_box = ne_collision_box[i]
@Box Sizes:
After the last loop finishes loop through all the newly created biters, print the boxes to log and check what the actual values are. By a quick look the biggest collision box would be about 0.0057 * 100 = 0.57, so slightly more than one tile in size? Maybe they're just too small.

@Tint:
Same procedure. Print out the tables after the last loop and check if they look correct.

Re: Why does my table not work

Posted: Tue Jun 19, 2018 5:11 pm
by TheSAguy
Okay, so I got everything working, excecpt for the Tint.
This is the table with a tint:

It seems like the game does not like the tint. vs tint.

Image
To create the above table I used:

Code: Select all

for i = 1, 100 do
	-- Tint	
	local tint_r = 1
	local tint_g = 0
	local tint_b = 0
	table.insert(ne_tank_tint, {{r=tint_r, g=tint_g, b=tint_b, a=0.65}})		
end
Here, I just used: ne_tank_tint = {r=1, g=0, b=0, a=0.65} and it works:

Image

So again, the only difference between the two is that in the table, it adds the brackets [] after the word tint.

When I hard-code the tint table:

Code: Select all

ne_tank_tint = {{r=1, g=0, b=0, a=0.65},	{r=0.99, g=0, b=0, a=0.65},	{r=0.98, g=0, b=0, a=0.65},	{r=0.97, g=0, b=0, a=0.65},	{r=0.96, g=0, b=0, a=0.65},	{r=0.95, g=0, b=0, a=0.65},	{r=0.94, g=0, b=0, a=0.65},	{r=0.93, g=0, b=0, a=0.65},	{r=0.92, g=0, b=0, a=0.65},	{r=0.91, g=0, b=0, a=0.65},	{r=0.9, g=0, b=0, a=0.65},	{r=0.89, g=0, b=0, a=0.65},	{r=0.88, g=0, b=0, a=0.65},	{r=0.87, g=0, b=0, a=0.65},	{r=0.86, g=0, b=0, a=0.65},	{r=0.85, g=0, b=0, a=0.65},	{r=0.84, g=0, b=0, a=0.65},	{r=0.83, g=0, b=0, a=0.65},	{r=0.82, g=0, b=0, a=0.65},	{r=0.81, g=0, b=0, a=0.65},	{r=0.8, g=0, b=0, a=0.65},	{r=0.79, g=0, b=0, a=0.65},	{r=0.78, g=0, b=0, a=0.65},	{r=0.77, g=0, b=0, a=0.65},	{r=0.76, g=0, b=0, a=0.65},	{r=0.75, g=0, b=0, a=0.65},	{r=0.74, g=0, b=0, a=0.65},	{r=0.73, g=0, b=0, a=0.65},	{r=0.72, g=0, b=0, a=0.65},	{r=0.71, g=0, b=0, a=0.65},	{r=0.7, g=0, b=0, a=0.65},	{r=0.69, g=0, b=0, a=0.65},	{r=0.68, g=0, b=0, a=0.65},	{r=0.67, g=0, b=0, a=0.65},	{r=0.66, g=0, b=0, a=0.65},	{r=0.65, g=0, b=0, a=0.65},	{r=0.64, g=0, b=0, a=0.65},	{r=0.63, g=0, b=0, a=0.65},	{r=0.62, g=0, b=0, a=0.65},	{r=0.61, g=0, b=0, a=0.65},	{r=0.6, g=0, b=0, a=0.65},	{r=0.59, g=0, b=0, a=0.65},	{r=0.58, g=0, b=0, a=0.65},	{r=0.57, g=0, b=0, a=0.65},	{r=0.56, g=0, b=0, a=0.65},	{r=0.55, g=0, b=0, a=0.65},	{r=0.54, g=0, b=0, a=0.65},	{r=0.53, g=0, b=0, a=0.65},	{r=0.52, g=0, b=0, a=0.65},	{r=0.51, g=0, b=0, a=0.65},	{r=0.5, g=0, b=0, a=0.65},	{r=0.49, g=0, b=0, a=0.65},	{r=0.48, g=0, b=0, a=0.65},	{r=0.47, g=0, b=0, a=0.65},	{r=0.46, g=0, b=0, a=0.65},	{r=0.45, g=0, b=0, a=0.65},	{r=0.44, g=0, b=0, a=0.65},	{r=0.43, g=0, b=0, a=0.65},	{r=0.42, g=0, b=0, a=0.65},	{r=0.41, g=0, b=0, a=0.65},	{r=0.4, g=0, b=0, a=0.65},	{r=0.39, g=0, b=0, a=0.65},	{r=0.38, g=0, b=0, a=0.65},	{r=0.37, g=0, b=0, a=0.65},	{r=0.36, g=0, b=0, a=0.65},	{r=0.35, g=0, b=0, a=0.65},	{r=0.34, g=0, b=0, a=0.65},	{r=0.33, g=0, b=0, a=0.65},	{r=0.32, g=0, b=0, a=0.65},	{r=0.31, g=0, b=0, a=0.65},	{r=0.3, g=0, b=0, a=0.65},	{r=0.29, g=0, b=0, a=0.65},	{r=0.28, g=0, b=0, a=0.65},	{r=0.27, g=0, b=0, a=0.65},	{r=0.26, g=0, b=0, a=0.65},	{r=0.25, g=0, b=0, a=0.65},	{r=0.24, g=0, b=0, a=0.65},	{r=0.23, g=0, b=0, a=0.65},	{r=0.22, g=0, b=0, a=0.65},	{r=0.21, g=0, b=0, a=0.65},	{r=0.2, g=0, b=0, a=0.65},	{r=0.19, g=0, b=0, a=0.65},	{r=0.18, g=0, b=0, a=0.65},	{r=0.17, g=0, b=0, a=0.65},	{r=0.16, g=0, b=0, a=0.65},	{r=0.15, g=0, b=0, a=0.65},	{r=0.14, g=0, b=0, a=0.65},	{r=0.13, g=0, b=0, a=0.65},	{r=0.12, g=0, b=0, a=0.65},	{r=0.11, g=0, b=0, a=0.65},	{r=0.1, g=0, b=0, a=0.65},	{r=0.09, g=0, b=0, a=0.65},	{r=0.08, g=0, b=0, a=0.65},	{r=0.07, g=0, b=0, a=0.65},	{r=0.06, g=0, b=0, a=0.65},	{r=0.05, g=0, b=0, a=0.65},	{r=0.04, g=0, b=0, a=0.65},	{r=0.03, g=0, b=0, a=0.65},	{r=0.02, g=0, b=0, a=0.65},	{r=0.01, g=0, b=0, a=0.65}}
It works, it's just when I try and create the table with the loop that it fails.

Re: Why does my table not work

Posted: Tue Jun 19, 2018 5:47 pm
by eradicator
TheSAguy wrote:it adds the brackets [] after the word tint.
How did you manage to get this far without understanding basic lua concepts? The brackets are a table index and not some random decoration after some random word. You're nesting the table when you shouldn't.

Code: Select all

table.insert(ne_tank_tint, {{r=tint_r, g=tint_g, b=tint_b, a=0.65}})  --wrong
table.insert(ne_tank_tint,  {r=tint_r, g=tint_g, b=tint_b, a=0.65} )  --correct
TheSAguy wrote: When I hard-code the tint table:
[...]
It works, it's just when I try and create the table with the loop that it fails.
Yea, beacuse that hard-coded table actually has the correct format.

If you had just used a single loop then you wouldn't need those temporary tables and the error would've been much easier to spot in the first place.

Re: Why does my table not work

Posted: Tue Jun 19, 2018 6:18 pm
by TheSAguy
Thanks!
:mrgreen: