@Hoi:
Basically, it's like this. All functions in Lua must be closed- they have a start and an end.
In Spring, the special Sync loop for Gadget code is still considered a function, and must be ended.
Moreover, you cannot use Spring.CreateUnit(), which is a call-out (
a special line of code that communicates to Spring directly, telling the game engine to do something useful) as a function. It's calling a function within Spring, but Lua needs to have instructions about when and under what conditions to call it.
Here, let's look at doing this on the first game frame, via the GameFrame Call-in(
a Call-In is a function that is run either every sync frame, for sync code, as fast as possible, if unsynced).
I'm not going to get into how to do this via fancier methods, this is just to get you a little more familiar with basic concepts here:
Code: Select all
function gadget:GetInfo()
return {
name = "The spawner",
desc = "Spawns stuff",
author = "Hoi",
date = "30-6-2008",
license = "none",
layer = 0,
enabled = true -- loaded by default?
}
end
-------------------------
if (gadgetHandler:IsSyncedCode()) then ------ SYNCED CODE HERE
function gadget:GameFrame(f)
if f == 1 then
Spring.CreateUnit("corcom", 0, 0, 0, "s", 16)
end
end
else ---- END SYNCED CODE HERE
end ---- END THE GADGET HERE
That code should spawn a corcom at 0,0,0 in the worldspace, facing South, and assigned to Team 16, on Frame 1 of sync time.
Obviously, if you wanted this to be assigned to a specific player's Team, you'd have to use a different method.
As you can see, I've added quite a few things to this code. Firstly, I've added the correct Call-In, GameFrame, and have named the first argument, "f".
The Call-Ins all have different arguments- these are fixed things that Spring uses in these functions, and must have certain values (
for example, if a Call-In expects a Unit's name, you must pass it a string). You can have additional arguments past those, of course- for example, with GameFrame, you could have (f,g) where g is some variable you've set the value of somewhere else in your code, and then you could use that if-->then to evaluate the values of both variables.
Secondly, I added the if-->then loop, to evaluate whether it's game frame 1 or not. If you didn't do that, every frame a new corcom would be spawned- not ideal! However, if you wanted to spawn one every 600 frames, you could just use this code, with very minor changes to increment the value of f that we're looking for each time.
The most important thing I added was the "else" and "end". They look kind've funky like that, but they're totally necessary. Each Gadget may have
both Sync and UnSync code in it. That's why every Gadget starts with an if-->then.
If you have any unsync Call-Ins / Call-Outs you need to make (
read the Wiki documentation about which are which- Call-Ins are documented in API.txt, which is a file in the LuaUI folder of Spring {
should be Wiki'd, imo, I didn't even know it existed until somebody pointed it out})... you need to make them after the "else" in Gadgets, but before the final "end".
That final end closes the entire loop of the function. Without it, Spring doesn't know how to terminate the operations, and the Lua won't compile, let alone run.
Hope that helped clear things up a bit. You've really, really, really gotta sit down with the
Lua Reference 5.1, read Lua sourcecode from other game designers around here, and study the Wiki hard, though, if you're going to make any headway. Nobody is going to write your code for you. Trust me, I understand how frustrating it is, if you're new to programming... but you're just going to have to do it, because once we're past simple loop stuff, you're on your own around here.