Lindworm, LUA question

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
fisch
Posts: 8
Joined: Thu May 05, 2016 6:00 pm
Location: Austria

Lindworm, LUA question

Post 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
---
Have forbearance with my bad English :)
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Lindworm, LUA question

Post 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.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Lindworm, LUA question

Post 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)
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Lindworm, LUA question

Post 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 ". :(
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Lindworm, LUA question

Post 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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
fisch
Posts: 8
Joined: Thu May 05, 2016 6:00 pm
Location: Austria

Re: Lindworm, LUA question

Post 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:
---
Have forbearance with my bad English :)
User avatar
THOM
Posts: 1274
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Lindworm, LUA question

Post by THOM »

fisch, do I get you right, are working on an own brain for the lindworm?
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
fisch
Posts: 8
Joined: Thu May 05, 2016 6:00 pm
Location: Austria

Re: Lindworm, LUA question

Post 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.
---
Have forbearance with my bad English :)
Post Reply