News:

SMF - Just Installed!

Main Menu

LUA-question

Started by doublejack, September 03, 2012, 03:22:17 PM

Previous topic - Next topic

Stephen

Quote from: ImpassIve_rus on September 09, 2012, 08:02:58 PM
Since 21th build of Knight isn't released yet, it is possible to add some features to it, right?

I'm sorry, but I have already started doing the build, so it's too late to add new features. It will have to wait until the next one I'm afraid.

Quote
So, is that hard to implement a function to retrieve a name from "Player" object?

No, this is not difficult at all. I'll add it in version 22.

Quote
Also, an ability to operate with Deathmatch scores table (displayed on the upper right side of the screen) would be nice for me, but with feature is a bit more specific, so that is not so important.

I agree - it should be possible to set the scores directly from Lua. That way, the Deathmatch mission could be coded in Lua instead of being a special case. I've added a Trac ticket (#205).

Quote
Which way to inform players about their scores is better? (using current functionality)

There's no way to access player names using current functionality, so the best you can do is tell the player their own score, and maybe the top score as well.

doublejack

hello again and sorry for my absence) now I'm trying to get into the knights scripting specifics)

I have some ideas and hope you would see something I created soon)

and Stephen, maybe it would be a good idea to create a forum section for mods and modding discussions?

Stephen

Quote from: doublejack on September 16, 2012, 01:33:30 PM
and Stephen, maybe it would be a good idea to create a forum section for mods and modding discussions?

Good idea.

Maybe there should be one forum for Mod Announcements (where people can announce completed mods) and a second forum for Lua Discussion (where people can ask questions about scripting and so on)?

doublejack

I think there is no need for two forums. just make topics with mods start with [mod] prefix ([mod] Spiders, [mod] Toolbox, etc.)

and create a main topic with mod list (links) and small descriptions. and make this topic main, so it would always be on the top)

Moo

Yes, with the level of activity here, no need to separate things like that...

Worst

I guess a separate mod forum is just fine, but I'd be wary of making too much new forums as that might make things look deserted, which is not something that'll attract new people.. And it would mean more browsing for the current users of the forum. (I wouldn't even make a separate forum for mods at this point, but this is just me :P)

If it's about organizing things, then with the low level of activity, I think thats the least of the worries right now.. And once it is a worry, then I think we can celebrate - for other reasons :)

Stephen

OK... let's just leave things as they are for the moment. When we have more users we can think about creating new forums.

If someone wants to make a mod list topic, then I can set it as sticky so that it always appears at the top...

ImpassIve

#22
Heh, I have some more questions)

1) I noticed a bug (?) with respawn function in lua:
kts.SetRespawnFunction(respawn_func)
function respawn_func(player)
print("Died!")
return nil
end


If one player will be eliminated (e.g. by using kts.EliminatePlayer(player) )- this function will be called for an infinity times. So, you will get
Died!
Died!
Died!
Died!
Died!
etc. in your chat.

2)
kts.AddMonsters(monster_type, number_to_place)
function is useful, because you don't have to thinks about "how to place this monster in this dungeon".
But if you want to control all your "special" monsters - you will have to add them one-by-one by using
cr = kts.AddMonster(pos, monster_type);
table.insert(My_mobs, cr);

But how can you find a correct place for it...
You will have to choose a random pos (hmm... from what range?), check tile on that position (it shouldn't be some kind of wall) and in some way check if somebody is already placed here (not so necessary, because in that case function just will return nil).

Is there any way to simplify this process?


Moo

Yes, a "return random dungeon square" function would be great. I needed something like that for my teleport wand, so I gave up with that... Worst did it the hard way with his wand in the meantime though, pretty much the same way you described.
If the function could return any "existant" square in the dungeon (not "air") but including walls, that would be good, then you could just keep trying if you got a wall (or table, or pit, or whatever) and didn't want that.

Another possibility would be if you could use the AddMonster method, then have another function which returns all existing creatures (or objects etc?) of that type.

Stephen

Quote from: ImpassIve_rus on September 27, 2012, 05:29:07 PM
1) I noticed a bug (?) with respawn function in lua:

Yeah, that sounds like a bug, there is no point in calling the respawn function if a player is eliminated. (Added to Trac)

Quote from: Moo on September 27, 2012, 06:01:06 PM
Yes, a "return random dungeon square" function would be great.

Indeed, you don't want to be doing it how Worst did it :)

I could add "kts.GetRandomPos()" that returns a random dungeon position, this would probably be the simplest solution.

Quote from: ImpassIve_rus on September 27, 2012, 05:29:07 PM
You will have to choose a random pos (hmm... from what range?), check tile on that position (it shouldn't be some kind of wall) and in some way check if somebody is already placed here (not so necessary, because in that case function just will return nil).

You don't need to check if it's a wall, because kts.AddMonster won't place a monster inside a wall (it will return nil instead).

You do have to worry about pits. kts.AddMonster will place a monster on a pit -- but the monster will instantly fall down the pit and die. You can check for this by calling "kts.IsAlive" immediately after creating the monster.

I should probably change kts.AddMonster so that it refuses to place monsters on tiles in the "ai_avoid" list. This would prevent monsters being created on tiles which they would not normally walk onto.

Stephen

OK, I have now added kts.GetRandomPos() and modified kts.AddMonster() so that it does not place monsters in tiles from the "ai_avoid" list (usually pits). The new build is available in http://www.solarflare.org.uk/snapshots/ (assuming my upload script worked properly!).

Romanio(Rus)

For some reason functions addMonster and getTiles are not working for me as supposed to do.

Here is a small example code:
module(...)
local C = require("classic")
local M = require("menu")

local thelayout = nil
local mapwidth = 0
local mapheight = 0

function getlayout()
if thelayout == nil then
thelayout = C.Dsetup.layout.func()
mapwidth  = (thelayout.width  * 12) + thelayout.width  + 1
mapheight = (thelayout.height * 12) + thelayout.height + 1
end
end

spawn_item = {
id = "spawn-bat",
text = "Spawn",
choice_min = 1,
choice_max = 1,
features = function(S)
getlayout()
kts.SetRespawnFunction(spawn())
end
}

function spawn()
    local pos = { x, y }
    local xmax = mapwidth
    local ymax = mapheight
print("Spawn begin!")
for i=1,10000 do
pos.x = kts.RandomRange(0, xmax)
pos.y = kts.RandomRange(0, ymax)
cr = kts.AddMonster(pos, C.m_vampire_bat)
--print("x",pos.x,"y",pos.y)
if cr ~= nil then
print("A creature was added")
end
end
print("Spawn end!")
end

table.insert(kts.MENU.items, spawn_item)


but value, returned by kts.addMonster is always == nil. Similar situation was with getTiles (returns an empty array), used in a same way.
What I am doing wrong?

Moo

With the new GetRandomPos you shouldn't need to use the layout or RandomRange to get a location...

Does it place the bats but return nil, or not do anything?

Stephen

What are you trying to do? Do you want monsters to be created every time a knight respawns?

If so, your problem is the following:

kts.SetRespawnFunction(spawn())


That should be:

kts.SetRespawnFunction(spawn)


Or do you want monsters to be created once, at the start of the game? In that case, the easiest way is to do this:

C.add_initial_monsters(C.m_vampire_bat, 10)

where 10 is the number of monsters you want.

("add_initial_monsters" is a function defined in classic/dungeon_setup.lua. You can see an example in the "Vampire Bats" entry in menus.lua.)

ImpassIve

#29
Well, I will answer instead of him, because he is offline right now and I was helping him to make his mod...

In original, that were not bats, but his own monsters and he should store them into an array to control their behavior (that is why he didn't use "add_initial_monsters" or "kts.AddMonsters")

QuoteDoes it place the bats but return nil, or not do anything?
That doesn't place any monster.

Quotekts.SetRespawnFunction(spawn)
instead of
kts.SetRespawnFunction(spawn())
Yes, this example was made in 10 minutes just to show the problem and that kind of mistake was made... So, this example is incorrect.

I am not sure (because I don't have a copy of Romanio's mod on my PC) but as I know he really have done the same error in his mod. But he noticed it and corrected even before that post

I hope, tomorrow he will either approve what the problem is solved or make a new example.