3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Factorioでmodを作ってみる-その2-

Posted at

基本的には
Factorio API Documents
を参考にLua Scriptを記述すればいい

参考になるのは
英語wikiのModのTutorial

サンプルとして、ベルトを永遠に敷設できるModを開発してみる -> ソース

ファイルの配置は
%AppData%\Factorio\mods\[Mod名のフォルダ]
に下記画像のようにinfo.jsonluaスクリプトを配置すればよい

infinityBelt.png

特定のイベント発生時にイベントハンドラが呼び出されるように設定する

例としてはentityが建設された時にイベントハンドラが呼び出されるように設定

event.lua
script.on_event(defines.events.on_built_entity, on_built_entity_event)

なお、イベント一覧に関しては
Eventsを参照

イベントハンドラ内で自作関数を呼び出すように設定する

例としてはベルトが敷設された場合にプレイヤーとアイテムを引数として関数を呼び出すように設定

eventHandler.lua
local on_built_entity_event = function(event)
  local player = game.players[event.player_index]
  local item = event.created_entity
  if item.type == "transport-belt" or item.type == "underground-belt" then
    refill_belts(player.get_main_inventory(), item.name)
  end
end

やりたいことを関数内に記述する

例としては引数として渡されたプレイヤーが同じく引数として渡されたベルトを50未満しか所持していない場合は1追加するように設定

refillBelts.lua
local refill_belts = function(inventory, belt)
  local belt_count = inventory.get_item_count(belt)
  if belt_count < 50 then
    inventory.insert{name=belt, count=1}
  end	
end

なお、アイテムの種類や名前に関しては
Factorio wikiのData.rawあたりに揃っているので参照。また、デバッグして確認したければ

local player = game.players[event.player_index]
local item = event.created_entity
player.print(item.type)
player.print(item.name)

のようにprint出力させてみてもいい

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?