[API Intro] What is a prototype?

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
Plawerth
Long Handed Inserter
Long Handed Inserter
Posts: 85
Joined: Thu Mar 02, 2017 12:57 am
Contact:

[API Intro] What is a prototype?

Post by Plawerth » Sat Feb 09, 2019 9:10 pm

For example:
Factorio API - Latest - LuaEntityPrototype
https://lua-api.factorio.com/latest/Lua ... otype.html

The full description is that this is "The prototype of an entity."

What exactly does this mean? No further detail seems to be available.

What is the purpose of a prototype? What does it do? What would not having it do?

,

My guess is that the game has its hardcoded initial values for how the game objects/entities should work. But rather than always creating new objects from the hardcoded data, it instead is "preloading" these initial hard values into this prototype object, which sits in memory like a template, and is from which all else is created.

Although the hardcoded initial values cannot be changed except via a mod, the running game may apparently be able to modify the template/prototype of an entity, so that all further objects created of that type, will have the customized settings of the prototype.

Or am I totally wrong here?

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 6607
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: [API Intro] What is a prototype?

Post by bobingabout » Mon Feb 11, 2019 9:33 am

Prototype is actually a term used extensively in the game code... The simplified description would be... "An Entity Prototype is a LUA data table that defines the configurable data of how any specific entity should behave, based on an entity template."

For examples of how significant of a difference a prototype can make, see the difference between the Assembling machine, Centrifuge, Chemical plant and Oil refinery. the Assembling machine and Centrifuge are very different from the Chemical plant and Oil refinery, not only in recipe, but functionality. the Assembling machine 1 for example will have an animation cycle that pauses when not in operation and continues from where it was paused when it continues to craft, and can only handle items. The Assembling machine 2 and 3 can handle fluids, but the pipe connection only appears if the recipe has a fluid. By contrast the Chemical plant has no active animation of this type, but does have working visualisations that are only shown when it is in use, which changes colour based on the recipe. Most of it's recipes are based around fluids. Pipe connections are always shown even when using recipes such as batteries that have no fluid using them. And the Centrifuge glows when working. These differing behaviours are all part of the Assembling machine entity template, but only the appropriate behaviours are programmed by the prototype.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 2719
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: [API Intro] What is a prototype?

Post by DaveMcW » Mon Feb 11, 2019 10:04 am

In control.lua, a prototype is a read-only copy of the stuff you defined in data.lua.

Entities in control.lua do not inherit from their prototype, you need to use a prototype object if you want to look up their properities.

Code: Select all

/c game.print(game.player.selected.max_health)  -- Error!
/c game.print(game.player.selected.prototype.max_health)  -- Works
/c game.print(game.entity_prototypes[game.player.selected.name].max_health)  -- Also works

Post Reply

Return to “Modding discussion”

Who is online

Users browsing this forum: No registered users