Need help with new building (Tables & On-Sector-Scan)

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Need help with new building (Tables & On-Sector-Scan)

Post by TheSAguy »

I'm trying to add a new building, that acts like a radar and on scan, it will attract nearby biters to attack. I'm calling it a Thumper.

So first thing I need to do is add newly created thumpers to a table each time it's created. If I'm correct, this is needed for the "set_multi_command" that needs a target to be a table.

I'm still having problems with tables, adding I got, but not removing.

Here is my current code.
The below goes in my "function On_Built(event)" code
On-Built:

Code: Select all

--- Add to Table 
if event.created_entity.name == "Thumper" then
 table.insert(global.ThumperTable,event.created_entity)
end

The below goes in my "function On_Removed(event)" code

Code: Select all

--- On Remove
  for k,T in ipairs(global.ThumperTable) do
    if not T.valid then
      table.remove(global.ThumperTable,k)
			if #global.ThumperTable == 0 then
				global.ThumperTable = nil
			end
    end
  end
The below is for each time a thumper scan is complete:

Code: Select all

game.on_event(defines.events.on_sector_scanned, function(event)

	--- Each time a Thumper "Scans", it will attract biters in the area
	if event.radar.name == "Thumper" then
   		game.get_surface(1).set_multi_command({type=defines.command.attack,target=ThumperTable[#ThumperTable-1],distraction=defines.distraction.none},10)
   	end
		
end)
My current issues:
- I don't think my table removal code is correct, but don't know how to fix it.
- For my attack commend, I need to specify a table. So here is where I'll give it the thumber table. But my code is not 100% correct here.
- Lastly, there will be a problem when you have multiple thumpers. Each time a thumper completes a scan, it should trigger the attack, but how do I tell what thumper did the scan and have the biters attach that thumper?
I was thinking of maybe a counter, and each time a scan is completed to attach the next thumper. That way everyone will get a turn, it might not be the one that completes the scan, but each one will have a turn. Not very clean, since ideally the one completing the scan should be attached.


So I'm not sure how to complete/fix this code and was hoping I could get some help!
Thanks.
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Need help with new building (Tables & On-Sector-Scan)

Post by Adil »

TheSAguy wrote: So first thing I need to do is add newly created thumpers to a table each time it's created. If I'm correct, this is needed for the "set_multi_command" that needs a target to be a table.
No no no, you got it wrong this time. As the wiki states set_multi_comand takes table as firs argument, a table which is a command to be set. The command itself must contain fields named target - the entity to attack, and distraction. No redundant tabling of anything is needed.

For currently described functionality you don't really need to track your thumpers explicitly. on_sector_scanned event already reports you the thumper which has made its dirty business. So the code would be like this:

Code: Select all

game.on_event(defines.events.on_sector_scanned, function(event)
   --- Each time a Thumper "Scans", it will attract biters in the area
   if event.radar.name == "Thumper" then
         event.radar.surface.set_multi_command({type=defines.command.attack, target=event.radar, distraction=defines.distraction.by_enemy},10)
      end   
end)
There's problem with set_multi_command still, as it's not transparent which units does it find suitable.

The table removal code is interesting. I'm not sure entity is already invalid during the on_entity_die or other events, so for the most of time I've seen elsewhere the simple search is done:

Code: Select all

game.on_event({defines.events.on_built_entity,defines.events.on_robot_built_entity},function(event)
ent=event.entity;
    for n,e in pairs(global.ThumperTable) do
        if ent==e then table.remove(global.ThumperTable,n) end
    end
end)
The global.ThumperTable = nil fragment is also highly uncommon to be used, as you rarely have many of these global tables and it's sufficient to simply define them at the start of the game and have them laying around rather than constantly destroying them when they get empty and creating them when you have something to put in agan. (And you'd have to check and initialize your tables before using table.add on them that way (table=table or {}) )
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Need help with new building (Tables & On-Sector-Scan)

Post by TheSAguy »

This worked perfect Adil!
Thanks.
Post Reply

Return to “Modding help”