Page 1 of 1

Syntax error

Posted: Fri Nov 22, 2019 9:38 pm
by Hyalskavran
Hello !

I'm trying to make a small mod, but it even it's current state it gives me an error.

Code: Select all

script.on_event(defines.events.on_console_chat, function(event)
message = new_message
if new_message == "baza" then
data.raw.unit-spawner["biter-spawner"].health = 0
data.raw.unit-spawner["spitter-spawner"].health = 0
end
end
)
Factorio tells me there is syntax error on line 4 near "-" but I can't figure it out since I copied the names from the wiki.

Re: Syntax error

Posted: Fri Nov 22, 2019 10:33 pm
by Pi-C
Hyalskavran wrote: Fri Nov 22, 2019 9:38 pm Hello !

I'm trying to make a small mod, but it even it's current state it gives me an error.

Code: Select all

script.on_event(defines.events.on_console_chat, function(event)
message = new_message
if new_message == "baza" then
data.raw.unit-spawner["biter-spawner"].health = 0
data.raw.unit-spawner["spitter-spawner"].health = 0
end
end
)
Factorio tells me there is syntax error on line 4 near "-" but I can't figure it out since I copied the names from the wiki.
Several things wrong with it:
  • It should be

    Code: Select all

    data.raw["unit-spawner"]["biter-spawner"].health = 0
    
    Otherwise, it's the substraction of data.raw.unit and spawner["biter-spawner"].health.
  • Even then, it wouldn't work because the table data isn't available in the control stage (where events are handled), but only in the data stage (where prototypes are defined).
  • Where does new_message on line 2 come from? Do you mean this, perhaps?

    Code: Select all

    local new_message = event.message
    

Re: Syntax error

Posted: Fri Nov 22, 2019 10:53 pm
by Pi-C
Does this do what you want?

Code: Select all

script.on_event(defines.events.on_console_chat, function(event)
    local new_message = event.message
    if new_message == "baza" then
        local spawners = game.surfaces["nauvis"].find_entities_filtered{type="unit-spawner"}
	for _, spawner in pairs(spawners) do 
	    spawner.destroy() 
	end
    end
end)

Re: Syntax error

Posted: Sat Nov 23, 2019 12:21 pm
by Hyalskavran
Yes, and yes. Thank you guys so much for such a quick help and an explanation. I will try to learn more about what I'm trying to do next time before doing it.