Stranded: Alien Dawn

Stranded: Alien Dawn

Stranded: Alien Dawn Workshop
Explore other users workshop creations, upload, download and subscribe to new content
Scyth 2023 年 8 月 27 日 上午 9:02
Scripts with classes
Has anyone tried to write scripts with classes? I can't understand why even the example from the manual:

DefineClass.Building = { __parents = { "BaseBuilding", "Constructable" }, construction_cost = 200, entity = "Building_Placeholder", entrances = { "Entrance1", "Entrance2", "Entrance3" }, Update = function(self) end, } function Building:DailyUpdate() end local a = Building:new()

gives this:

Error loading AppData/Mods/testmod/Code/Script.lua: AppData/Mods/testmod/Code/Script.lua:16: attempt to call a nil value (method 'new')
< >
正在显示第 1 - 2 条,共 2 条留言
injto4ka 30 2023 年 8 月 27 日 下午 1:57 
This is one example how a class looks and another example how an object from that class is instantiated. Both are more or less abstract, being there just to show how a class looks, and thus it's not a part of a real game code. You cannot run it.
First in order to define successfully a class, which inherits other classes, those parent classes have to exist. In this case, there are no such base classes in the game code: "BaseBuilding" and "Constructable". Then in order to create an object from that class, you have to do it after the classes are built, which is marked by the event "ClassesBuilt". That is why the class "Building" doesn't have yet its method "new", even if the class has been correctly defined.
Scyth 2023 年 8 月 28 日 上午 3:29 
Thanks! It's much clearer now :steamthumbsup:
< >
正在显示第 1 - 2 条,共 2 条留言
每页显示数: 1530 50