Dynamite doesnt blow up when you shoot it (see code)

Place to get help with not working mods / modding interface.
ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Dynamite doesnt blow up when you shoot it (see code)

Post by ficolas »

The dynamite entity doesnt blow up when you shoot it, only when another dynamite activates it.

Code: Select all

	if glob.dynamite~=nil then
		for i,_ in pairs(glob.dynamite) do
			glob.dynamite[i].timer=glob.dynamite[i].timer+1
			if glob.dynamite[i].entity.isvalid() and glob.dynamite[i].timer==600  then
				game.createentity({name="huge-explosion",position=glob.dynamite[i].entity.position})
				posi=glob.dynamite[i].entity.position
				for pos=0,10,2.5 do
					local entities=game.findentities({{posi.x-pos,posi.y-pos},{posi.x+pos,posi.y+pos}})
					for i,_ in pairs(entities) do
						if entities[i].health~=nil and entities[i].name~="dynamite" then
							if entities[i].health<=50 then
								entities[i].die()
							else
								entities[i].health=entities[i].health-50
							end
						elseif entities[i].name=="iron-ore" or entities[i].name=="copper-ore" or entities[i].name=="coal" or entities[i].name=="stone" then
							entities[i].amount=entities[i].amount-1
							game.createentity{name="item-on-ground", position=entities[i].position, stack={name=entities[i].name}}
						end
					end
				end
				glob.dynamite[i].entity.destroy()
				table.remove(glob.dynamite ,i) 
			elseif glob.dynamite[i].entity.isvalid()==false then
				table.remove(glob.dynamite, i)
			elseif glob.dynamite[i].entity.isvalid() then
				dyn=game.findentities({{glob.dynamite[i].entity.position.x,glob.dynamite[i].entity.position.y},{glob.dynamite[i].entity.position.x,glob.dynamite[i].entity.position.y}})
				for z,_ in pairs(dyn) do
					if dyn[z].health~=100000 and dyn[z].name=="dynamite" then
						glob.dynamite[i].timer=570
					end
				end
			end
		end
	end
do the entity variable get dissabled when the health is changed or something?

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Dynamite doesnt blow up when you shoot it (see code)

Post by FreeER »

Might I assume this code is in ontick? If so it would be because when you shoot the dynamite you reduce it's health and once the health is less than 0 the entity 'dies' (is destroyed and any dying_explosion is called as well as loot, etc.) If this code is inside of ontick, then it is not called when the dynamite dies, only when it's timer runs out or nearby dynamite explodes.

Just my opinion here but this

Code: Select all

                         if entities[i].health<=50 then
                            entities[i].die()
                         else
                            entities[i].health=entities[i].health-50
seems like over kill to me... especially when you are running it every loop (0,2.5,5,7.5, and 10) thus the closest entities are being hit with a health-50 5 times (assuming they could survive that). I would either swap this to a lower damage (say 10) or use 10 in your findentities instead of pos.

Also

Code: Select all

                         entities[i].amount=entities[i].amount-1
                         game.createentity{name="item-on-ground", position=entities[i].position, stack={name=entities[i].name}}
From my understanding you are only giving the player 1 resource when they blow it up? Seriously? I'd say this is some weak dynamite but maybe it's simply directed outward...
In the dynamite mod I created I used this

Code: Select all

game.createentity({ name="item-on-ground", position = {event.entity.position.x, event.entity.position.y}, stack={name="iron-ore", count=ironore}}) end
ironore of course was a random number from 0 - resource.amount
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Dynamite doesnt blow up when you shoot it (see code)

Post by ficolas »

FreeER wrote:Might I assume this code is in ontick? If so it would be because when you shoot the dynamite you reduce it's health and once the health is less than 0 the entity 'dies' (is destroyed and any dying_explosion is called as well as loot, etc.) If this code is inside of ontick, then it is not called when the dynamite dies, only when it's timer runs out or nearby dynamite explodes.
The dynamite health is 100000 or so xD.
Just my opinion here but this

Code: Select all

                         if entities[i].health<=50 then
                            entities[i].die()
                         else
                            entities[i].health=entities[i].health-50
seems like over kill to me... especially when you are running it every loop (0,2.5,5,7.5, and 10) thus the closest entities are being hit with a health-50 5 times (assuming they could survive that). I would either swap this to a lower damage (say 10) or use 10 in your findentities instead of pos.

Also

Code: Select all

                         entities[i].amount=entities[i].amount-1
                         game.createentity{name="item-on-ground", position=entities[i].position, stack={name=entities[i].name}}
From my understanding you are only giving the player 1 resource when they blow it up? Seriously? I'd say this is some weak dynamite but maybe it's simply directed outward...
In the dynamite mod I created I used this

Code: Select all

game.createentity({ name="item-on-ground", position = {event.entity.position.x, event.entity.position.y}, stack={name="iron-ore", count=ironore}}) end
ironore of course was a random number from 0 - resource.amount
I know is too op on damage, and too low on resources, I will create different types of explosives some day, this is just for testing, I will change most of the values

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Dynamite doesnt blow up when you shoot it (see code)

Post by FreeER »

wow, i would guess then that you are referring to this portion (which I must have completely skipped before lol) which is constantly resetting the timer to 570, not letting it reach 600 and thus never exploding

Code: Select all

             elseif glob.dynamite[i].entity.isvalid() then
                dyn=game.findentities({{glob.dynamite[i].entity.position.x,glob.dynamite[i].entity.position.y},{glob.dynamite[i].entity.position.x,glob.dynamite[i].entity.position.y}})
                for z,_ in pairs(dyn) do
                   if dyn[z].health~=100000 and dyn[z].name=="dynamite" then
                      glob.dynamite[i].timer=570
                   end
                end
Why use dyn at all? Why not use something like -edit-DON'T USE THIS LOL, left to show how stupid I can be :lol:

Code: Select all

             elseif glob.dynamite[i].entity.isvalid() then 
                if glob.dynamite[i].health < 100000 then
                    if glob.dynamite[i].timer=570 then
                        glob.dynamite[i].timer=glob.dynamite[i].timer+1
                    else glob.dynamite[i].timer=570
                    end
                end
Better code:

Code: Select all

             elseif glob.dynamite[i].entity.isvalid() then
                if glob.dynamite[i].health < 100000 then
                    if glob.dynamite[i].timer<570 then
                        glob.dynamite[i].timer=570
                    else glob.dynamite[i].timer=glob.dynamite[i].timer+1
                    end
                end
There, that should actually work :lol:
Also I'd move this portion to the top of the if statement (that way the majority will match the first test instead of having to do all three checks before finding the code to execute), but that's only if you care about optimization lol
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Dynamite doesnt blow up when you shoot it (see code)

Post by ficolas »

Thanx, I alredy "solved" it (I had the idea, but I didnt fix it until now.)=

Also, each dinamite puts 4 or so resources on the floor, not 1, but the code should only make it put one, and also, it removes 4 from the ore vein, so thats why I let it like so.

Post Reply

Return to “Modding help”