Page 1 of 1
Slime Bursters
Posted: Sat Mar 23, 2013 3:04 am
by Grimfan
I have a type of slime that spawns a shockburst when it attacks a target. However, I now realize that the slime can attack in any direction, even into an area it's not facing (I think that might be a feature of the slime brain). Therefore the script I have below means that the shockburst only occasionally hits its target. How do I change the script below so that the shockburst activates into any square the slime is attacking (I assuming using getForward is not the way to go)?
Code: Select all
onAttack = function(self)
local dx,dy = getForward(self.facing)
local x,y = self.x + dx, self.y + dy
spawn("shockburst", self.level, self.x + dx, self.y + dy, self.facing)
Thanks for any help guys (though this actually may be trickier than my usual requests).

Re: Slime Bursters
Posted: Sat Mar 23, 2013 3:18 am
by JohnWordsworth
You can actually get two parameters from the onAttack script, which should help to give you the answer you are looking for...
Code: Select all
cloneObject {
name = "sparky_slime",
baseObject = "green_slime",
onAttack = function(monster, attack_name)
local attackFacing = monster.facing; -- The default facing for normal attacks
if ( attack_name == "attackLeft" ) then attackFacing = (attackFacing - 1) % 4;
elseif ( attack_name == "attackRight" ) then attackFacing = (attackFacing + 1) % 4;
elseif ( attack_name == "attackBack" ) then attackFacing = (attackFacing + 2) % 4;
end
local dx,dy = getForward(attackFacing);
spawn("shockburst", monster.level, monster.x + dx, monster.y + dy, attackFacing);
end
}
The attack facing variable starts off in the direction that the monster is facing. However, the second parameter tells us if we are using an attack animation that means that the monster is attacking to the side / back. In this case, we have to shift the facing parameter by 1 or 2 steps left or right. The % 4 bit means "modulo 4", which means that the result is limited to (0, 1, 2 or 3) and will basically 'cycle' around if it your value goes outside of this set of values.
So, if facing is 3 and we see attack_name == "attack_back" then facing = 3 + 2 = 5. But 5 is not a valid value, but after '%4' it becomes 1 - which is valid.
This is untested - so there might be some errors above!
Re: Slime Bursters
Posted: Sat Mar 23, 2013 5:03 pm
by Grimfan
Unfortunately when I put this script in I get the warning that there is an "unexpected symbol next to { " and my dungeon won't open. When I take the curly brackets out (just to see whether the editor opes up) the editor starts up but as soon as the slime attacks the editor crashes ( I expected as much). It seems that the symbol that the curly brackets are having problems with is actually the word "then".
By the way, I can see what your script is trying to do. Pity I'm currently unable to execute it.

Re: Slime Bursters
Posted: Sat Mar 23, 2013 9:03 pm
by JohnWordsworth
Sorry, it's meant to be 'then' followed by 'end', I fell back to curly-braces as I was programming in a different language at the same time as writing that. Lol. I've updated the post - that should work now!
I figured I would actually test it in the engine - the above code should work now

Re: Slime Bursters
Posted: Sat Mar 23, 2013 10:18 pm
by Xanathar
I fell back to curly-braces as I was programming in a different language
Happens to me. Every. Freaking. Time.

Re: Slime Bursters
Posted: Sat Mar 23, 2013 11:52 pm
by Grimfan
This is working beautifully now. Thanks John!
And I never even knew that attack_name was a parameter.

I do have a small question however. Your script uses semi-colons, yet I haven't seen them being used in any other script I can recall. What function do they serve?
Re: Slime Bursters
Posted: Sun Mar 24, 2013 12:23 am
by JohnWordsworth
Semi-colons are used in many programming languages to mark the end of a statement. They are actually completely optional in Lua, so they are only there for style purposes. I think the general convention with Lua is to omit them normally, unless you are putting multiple statements on a single line. I'm just in the habit of typing them, as I like the way that they explicitly say "this is the end of the command".
For instance, you can do...
a = 10 b = 2*a
And that is completely valid in Lua, but it's much easier to see that the above is actually two commands on one line (the first setting a to 10, and the second setting b to twice a) when there is a semi-colon;
a = 10; b = 2*a;
Granted, there isn't much use when you only have one statement per line - I just prefer the way it looks.