September 23, 2018, 04:35:57 PMLatest Member: Rololobin

Author Topic: Learning Lua:  (Read 8106 times)

0 Members and 1 Guest are viewing this topic.

Ben Dyson

  • Registered member
  • *
  • Posts: 27
    • View Profile
Learning Lua:
« on: April 01, 2011, 05:45:41 PM »
Hey,
I am trying to learn lua to help out with the development, so for my first little project i am going to try and add a simple room, can somebody tell me what files i need to edit so i can get started,
Many Thanks
ben

tobylane

  • Registered member
  • *
  • Posts: 53
    • View Profile
Re: Learning Lua:
« Reply #1 on: April 01, 2011, 05:59:27 PM »
lua/rooms/
lua/diseases/

There aren't many left, I think the DNA fixer is one. Like many other things on the programming ideas page, it needs some animation research, and possibly some original theme hospital research. The IRC channel will be very useful.

Ben Dyson

  • Registered member
  • *
  • Posts: 27
    • View Profile
Re: Learning Lua:
« Reply #2 on: April 01, 2011, 07:28:00 PM »
Hi,
@tobylane
Thanks, I have started the lua rooms file at the moment, I am just trying to get the room build able(show up in the menu), I am just making a empty room and am trying to make it show up, here is my code?
Code: [Select]
--[[ Copyright (c) 2009 Peter "Corsix" Cawley

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. --]]

local room = {}
room.id = "empty"
room.level_config_id = 25
room.class = "empty"
room.name = _S.rooms_short.empty_room
room.tooltip = _S.tooltip.rooms.empty_room
room.objects_additional = { "extinguisher", "radiator", "plant", "bin", "desk" }
room.objects_needed = {  }
room.build_preview_animation = 900
room.categories = {
  facilities = 5,
}
room.minimum_size = 4
room.wall_type = "white"
room.floor_tile = 18

class "empty" (Room)

function empty:empty(...)
  self:Room(...)
end

return room

How do I get it to show up in the menu?
Thanks

Lego3

  • Project Owner
  • *****
  • Posts: 398
    • View Profile
Re: Learning Lua:
« Reply #3 on: April 02, 2011, 06:32:57 AM »
All files in the normal folders are automatically loaded, so the only thing the room needs is a category to be listed under. Your room meets that condition, however, if the interpreter encounters any error in a given lua file it skips that file and prints the error to the console. For once these errors are actually usually quite descriptive, most importantly they most often tell you which file and which row the error occurred at. This has saved me many hours, I can tell you that...

In this case it the name and tooltip that won't work at the moment. _S is the global list of strings, and an error will occur as long as there is no string called "empty_room" in the "rooms_short" list. Preferably added to the english.lua file since all languages inherit from that file, or just let it be a specific string.

There's a thread about the DNA fixer around here somewhere.
For the end of the world spell, press Control, Alt, Delete.

Ben Dyson

  • Registered member
  • *
  • Posts: 27
    • View Profile
Re: Learning Lua:
« Reply #4 on: April 02, 2011, 11:48:08 AM »
Hi,
Thanks for that, I thought it was something like that and have had a look in the english.lua file and cant find the list for the rooms_short where about is it in that file?

Another thing, I am getting the room to show up in the build menu and build it but when I click on the finish button, a error comes up, I have attached a picture of the console.

Thank you so much for this help :)
Ben

Lego3

  • Project Owner
  • *****
  • Posts: 398
    • View Profile
Re: Learning Lua:
« Reply #5 on: April 02, 2011, 05:01:56 PM »
The languages which are present in the original game only have new strings in their .lua files. The rest is taken from TH anyway. But it adds up in the end, so you can write rooms_short.empty_room = "An Empty Room" at the top level anywhere in english.lua to add it to the rooms_short list.

Regarding the second one it is once again most often very useful information in the stack trace. I usually start from the bottom, remembering what I was doing when the error occurred and what the actual error is (in this case "Non-existant string: empty").

So in this case:
Ok, app.lua is the mother of everything, so not usually much to get there...
window.lua on line 1271: in function "onMouseUp". That sounds good, we just pressed a button.
We want to "handleClick", world.lua on line 580 is about to "markRoomAsBuilt" which is also exactly what we were doing, so were getting close.
In room.lua on line 482 in function "roomFinished", the line says:
self.world.ui:addWindow(UIInformation(self.world.ui, _S.room_descriptions[self.room_info.id]))

Aha, so it tries to fetch another string which doesn't exist: _S.room_descriptions.empty

The reason I go from the bottom is that the context isn't always as clear as in this case and the last few function calls (top) may already be too later to start looking at. Easier to go in than out.
For the end of the world spell, press Control, Alt, Delete.

Ben Dyson

  • Registered member
  • *
  • Posts: 27
    • View Profile
Re: Learning Lua:
« Reply #6 on: April 02, 2011, 05:59:55 PM »
OK, Thanks
I understand both of them and what you are going one about but I can't seem to get the bottom one working,
Here is my code:
empty.lua -
Code: [Select]
local room = {}
room.id = "empty"
room.level_config_id = 25
room.class = "empty"
room.name = _S.rooms_short.empty
room.tooltip = _S.rooms_short.empty
room.description = _S.room_descriptions.empty
room.objects_additional = { "extinguisher","desk","sofa"}
room.objects_needed = {  }
room.build_preview_animation = 900
room.categories = {
  facilities = 5,
}
At the bottom of english.lua -
Code: [Select]
rooms_short.empty = "Empty Room"
room_descriptions.empty = "Empty Room"

Sorry it takes me long to get used to lua, it should be easy considering I already know a little visual basic and c#

Thank you

Ben

Lego3

  • Project Owner
  • *****
  • Posts: 398
    • View Profile
Re: Learning Lua:
« Reply #7 on: April 02, 2011, 07:35:12 PM »
Well, what does the error message say?
For the end of the world spell, press Control, Alt, Delete.

Ben Dyson

  • Registered member
  • *
  • Posts: 27
    • View Profile
Re: Learning Lua:
« Reply #8 on: April 02, 2011, 07:40:07 PM »
Notice: LuaJIT not being used.
Consider replacing Lua with LuaJIT to improve performance. Note that there is no
t currently a 64 bit version of LuaJIT.
Error loading rooms\cafe.lua:
Non-existant string: cafe
An error has occured while running the buttonup handler.
A stack trace is included below, and the handler has been disconnected.
C:\Program Files\CorsixTH\Lua\graphics.lua:284: bad argument #1 to 'sizeOf' (str
ing expected, got nil)
stack traceback:
        [C]: in function 'sizeOf'
        C:\Program Files\CorsixTH\Lua\graphics.lua:284: in function 'sizeOf'
        ...:\Program Files\CorsixTH\Lua\dialogs\information.lua:66: in function
'onChangeLanguage'
        ...:\Program Files\CorsixTH\Lua\dialogs\information.lua:56: in function
'constructor'
        C:\Program Files\CorsixTH\Lua\class.lua:73: in function 'UIInformation'
        C:\Program Files\CorsixTH\Lua\room.lua:482: in function 'roomFinished'
        C:\Program Files\CorsixTH\Lua\world.lua:580: in function 'markRoomAsBuil
t'
        C:\Program Files\CorsixTH\Lua\dialogs\edit_room.lua:228: in function 'ca
llback'
        C:\Program Files\CorsixTH\Lua\window.lua:590: in function 'handleClick'
        C:\Program Files\CorsixTH\Lua\window.lua:1287: in function 'onMouseUp'
        ...Program Files\CorsixTH\Lua\dialogs\place_objects.lua:440: in function
 <...Program Files\CorsixTH\Lua\dialogs\place_objects.lua:439>
        (tail call): ?
        C:\Program Files\CorsixTH\Lua\window.lua:1271: in function 'onMouseUp'
        C:\Program Files\CorsixTH\Lua\ui.lua:632: in function <C:\Program Files\
CorsixTH\Lua\ui.lua:619>
        (tail call): ?
        (tail call): ?
        (tail call): ?
        C:\Program Files\CorsixTH\Lua\app.lua:578: in function <C:\Program Files
\CorsixTH\Lua\app.lua:573>

Lego3

  • Project Owner
  • *****
  • Posts: 398
    • View Profile
Re: Learning Lua:
« Reply #9 on: April 02, 2011, 08:34:24 PM »
Ok, so it gets a little further down the road, starts to create the information dialog but gets stuck in a function called "onChangeLanguage" because "sizeOf" doesn't get a string as it expects. Instead it gets nothing. Why could that be? All (most) dialogs reside in lua files which have their class name except the "UI" part, in this case it is dialogs/information.lua. Having a look in there you'll find that the offending row lies within a for loop, going through the variable "text". So apparently it is trying to go through a list of strings whereas it only got an actual string. Looking at the description of the constructor (called from roomFinished) we also find it:

--! Constructor for the Information Dialog.
--!param text The text to show, held in a table. All elements of the table will be written
-- beneath each other. If instead a table within the table is supplied the texts
-- will be shown in consecutive dialogs.

One could of course argue that the dialog in some way should work out single strings too...
For the end of the world spell, press Control, Alt, Delete.

Ben Dyson

  • Registered member
  • *
  • Posts: 27
    • View Profile
Re: Learning Lua:
« Reply #10 on: April 02, 2011, 08:45:18 PM »
Sorry, but I don't understand what I have to do, sorry?

Lego3

  • Project Owner
  • *****
  • Posts: 398
    • View Profile
Re: Learning Lua:
« Reply #11 on: April 02, 2011, 08:54:12 PM »
You supply it with a string _S.room_descriptions.empty, but it should be a table. compare with another room, for example GP:

room_descriptions.gp = {
    "GP's Office//",
    "This is the fundamental diagnosis room in your hospital. New patients are sent here to find out what is wrong with them. They are then sent either to further diagnosis or to a room where they can be cured. You might want to build another GP's Office if this one gets too busy. The bigger the room, and the more extra objects you place in it, the higher the prestige for the Doctor. This is also true of all the occupied rooms.//",
    "The GP's Office requires any Doctor. ",
  },
For the end of the world spell, press Control, Alt, Delete.

Ben Dyson

  • Registered member
  • *
  • Posts: 27
    • View Profile
Re: Learning Lua:
« Reply #12 on: April 02, 2011, 09:20:20 PM »
Ok, the error code is getting shorter,

Error loading rooms\empty.lua:
C:\Program Files\CorsixTH\Lua\rooms\empty.lua:28: assign to undeclared variable
'rooms_description'

Thank you so much for this

Lego3

  • Project Owner
  • *****
  • Posts: 398
    • View Profile
Re: Learning Lua:
« Reply #13 on: April 03, 2011, 06:59:04 AM »
There is something wrong on line 28 in empty.lua...
For the end of the world spell, press Control, Alt, Delete.

Ben Dyson

  • Registered member
  • *
  • Posts: 27
    • View Profile
Re: Learning Lua:
« Reply #14 on: April 03, 2011, 10:37:24 AM »
I understand that it is line 28 of empty.lua,  but I don't understand what is wrong with it, here is line 28;
Code: [Select]
rooms_description = _S.rooms_descriptions.empty