Page 1 of 1

Lindworm, LUA question

Posted: Wed Jun 01, 2016 8:54 pm
by fisch
Hey all,
I actually work on the Lindworm Fight for an extended LoG 2 main campaign. Just for fun and learn how the editor and LUA work. Who knows maybe the mode is even finished.

The Lindworm Brain with his basic attack and his ranged attack works fine and also the special actions like knockback, charge, ...
But now I’m stuck on a simple problem to spawn minions.
In his Brainscript he try to reach one of 2 Points on the map (done with triggers, just for testing) and then he perform the summon action. At this point he calls a delayed function in his Brain. This function does the spawn of the minions dependent of the stage. ... so far so good ...

Unfortunately, it is not possible for me to spawn monsters in this function. Lines with spawn("...", ...) are ignored and returned nil. When I use spawner
Objects, I become a bad self error.
SpoilerShow
Image
I can do all the thinks in this function e.g. decrement a counter or start a timer to activate a spawner. But not directly call spawn() or activate Spawners. I also can call another function in another script to spawn monsters with spawn(..).

And here is the second thing I don’t understand. If I call a function from another function with arguments, then the first argument is a Table.
I must write the function like

Code: Select all

function spawnHelper(dummyTable, obj, level, x, y, facing, elevation)
	spawn(obj, level, x, y, facing, elevation)
end
to use the variables. :shock:

What is this Table and which reason has it? And have anyone a more elegant solution or must I go detour. Brain > Function > function for spawn

Re: Lindworm, LUA question

Posted: Thu Jun 02, 2016 3:50 am
by Isaac
fisch wrote:And here is the second thing I don’t understand. If I call a function from another function with arguments, then the first argument is a Table.
I must write the function like

Code: Select all

function spawnHelper(dummyTable, obj, level, x, y, facing, elevation)
	spawn(obj, level, x, y, facing, elevation)
end
to use the variables. :shock:

What is this Table and which reason has it? And have anyone a more elegant solution or must I go detour. Brain > Function > function for spawn
I suspect that it is an issue with calling convention.

While a more technical [and perhaps more technically accurate] explanation may soon appear from among the other members... Essentially it is that in Lua/Grimrock, the colon is a shortcut for adding the calling object as the first parameter.

Calling a function in object_1 from object_2 like this...
object_1.script:myFunction(data)
is the same as
object_1.script.myFunction(object_2, data)

The table you mention is the script object that you called the function from.

You could try printing the object id to see... eg. print(dummyTable.go.id)

Code: Select all

function spawnHelper(dummyTable, obj, level, x, y, facing, elevation)
	print(dummyTable.go.id)
end
**Hook a pressure plate to that script, and step on it.

If you remove the dummyTable your spawnHelper def, and call the function using a period instead of a colon, it should work as you might have expected. However, you lose the calling reference; and that reference can be quite useful.

Re: Lindworm, LUA question

Posted: Thu Jun 02, 2016 8:25 am
by minmay
Isaac wrote:Calling a function in object_1 from object_2 like this...
object_1.script:myFunction(data)
is the same as
object_1.script.myFunction(object_2, data)
It's the same as
object_1.script.myFunction(object_1, data)
BIG difference.

You're getting "bad self" errors because you are calling something like

Code: Select all

spawner_5.controller.activate()
when it needs to be

Code: Select all

spawner_5.controller:activate()
All functions belonging to GameObjects and Components should be called bound, i.e. with ':', because they expect themselves as the first argument.

You don't have to guess whether a function should be called bound or not, because it's listed in the scripting reference: methods are written like

Code: Select all

ItemActionComponent:getBuildup()
whereas functions that should be called unbound (with '.') are written like

Code: Select all

GameMode.advanceTime(amount)

Re: Lindworm, LUA question

Posted: Thu Jun 02, 2016 10:14 am
by Isaac
minmay wrote:
Isaac wrote:Calling a function in object_1 from object_2 like this...
object_1.script:myFunction(data)
is the same as
object_1.script.myFunction(object_2, data)
It's the same as
object_1.script.myFunction(object_1, data)
BIG difference.
Damn peculiar. When I use a floor_trigger named 'object_2' to call the my_function() in object_1, and (and print the calling id string), it is the id for the floor_trigger (object_2) that prints. This is also true for hooks calling a script object IRRC.

** But you are right, I [now] find that when I call a function inside object_1 from a script object with the id of 'object_2', it [very unexpectedly to me] prints the id string of the script containing myFunction() ~object_1 rather than the calling object's id as I would have expected. So obviously I am missing (or have forgotten) an under-the-hood detail; and as they say, "It ain't what you know [that get's you]... it's what you know that ain't so ". :(

Re: Lindworm, LUA question

Posted: Thu Jun 02, 2016 7:40 pm
by minmay
Connectors are not bound calls. There isn't an under-the-hood detail you're missing. Grimrock does not change the syntax of Lua. You just made an unwarranted assumption and didn't test it until now.

Re: Lindworm, LUA question

Posted: Thu Jun 02, 2016 7:50 pm
by fisch
Thanks for your answers!
you're right spawner.spawner.activate() - damn - this was a copy/paste mistake :oops:
in the other function I typed spawner.spawner:activate() so it works. :roll:

Re: Lindworm, LUA question

Posted: Thu Jun 02, 2016 10:30 pm
by THOM
fisch, do I get you right, are working on an own brain for the lindworm?

Re: Lindworm, LUA question

Posted: Thu Jun 02, 2016 11:17 pm
by fisch
THOM wrote:fisch, do I get you right, are working on an own brain for the lindworm?
(yes) semi-own .. I use the existing code for a functional Lindworm like the original game.