Author Topic: Another noobie lua coder  (Read 9447 times)

Terrial

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
Another noobie lua coder
« on: July 22, 2010, 03:46:04 AM »
So I've decided to start making my own levels after finally beating the Dark Matter mode (The level 200 took me SO many tries for some reason because they AI kept ganging up on me. Quite the challenge to say the least)

Anyway, I'm trying to make a level where it has a back story of you trying to stop two huge factions from warring (Ah war and peace, don't you just love those games?) So I want a main asteroid where the two sides are constantly fighting, like i want this to happen the entire game until you complete your quest(s) to stop it.  I was thinking of having the two factions with their homeworlds and have them sending their seedlings to the main disputed asteroid to fight, and this asteroid would be visible to the player, but intraversible to the player.  So I need a way to have their units spawn on the homeworlds whenever their seedling counts get below say 50.  When they spawn I need them to send them to the asteroid to fight, but as I'm new to C# as a language, I don't know how to go about creating a loop to accomidate this.

From what I've read on the forums I've come up with this, but sadly, I suck at C# and more often than not the things I try to implement result as syntax errors

      *A clever loop with correct syntax goes here*

   GetAsteroid(0):AddSeedlings(100)
   GetAsteroid(1):AddSeedlings(100)
   GetAsteroid(0):SendSeedlingsToTarget(2,100,GetAsteroid(2))
                GetAsteroid(1):SendSeedlingsToTarget(3,100,GetAsteroid(2))
   
end


edit: also how can you make it send a laser mine to an asteroid?
« Last Edit: July 22, 2010, 04:01:12 AM by Terrial »

w4tc

  • Achiever
  • Shrub
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 170
Re: Another noobie lua coder
« Reply #1 on: July 22, 2010, 04:17:59 AM »
Put it on your asteroid

Code: [Select]
a:AddMine(faction)
Your player 1
so it must be like this

Code: [Select]
a:AddMine(1)
Example :

Code: [Select]
-- Asteroid 0
a = AddAsteroidWithAttribs(200,0, 0.7,0.6,0.5)
a.Owner = 1
a.TreeCap = 5
a:SetRadius(650)
a:AddSeedlings(400)
a:AddMine(1)
s = a:AddDysonTree()
a.Moveable = False
« Last Edit: July 22, 2010, 04:22:36 AM by w4tc »

Terrial

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
Re: Another noobie lua coder
« Reply #2 on: July 22, 2010, 04:24:18 AM »
Sorry, I meant how to make it move, like I know there's the SendSeedlings function, is there a SendMine function?

Also another question, can you instantly add seedlings to an asteroid of one empire that is controlled by another?  Example: Asteroid 1 is owned by empire 1, can you add 10 seedlings of empire 2 to asteroid 1?  Or can they only be put on asteroids owned by that empire?

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Another noobie lua coder
« Reply #3 on: July 22, 2010, 04:29:06 AM »
SendTo(GetAsteroid(asteroid id))   nil   Attempt to send the flower/mine to the destination asteroid.

I'll answer your first question in a few minutes. I clicked back accidentally and lost what I was going to say.

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Another noobie lua coder
« Reply #4 on: July 22, 2010, 04:48:10 AM »
For this, I'm assuming that asteroids 0 and 1 are the two warring factions' homeworlds. Is that right?

Code: [Select]
-- Up here you need to add your asteroids.

function LevelLogic()  --holds code to run during the game
-- I'd use a timer to send waves of seedlings to the no-man's-land (no-seed's-land?) asteroid, otherwise the game will crash due to too many seedlings. I know, I've been there, though it's more annoying than anything
-- and it stops you playing the level. So, a timer.
timer = GetGameTime() + 30 -- this declares a variable named 'timer' (you could call it 'monkey', or 'frank', or 'MyNeighbourWhoIsCalledJoeBloggsHasAMassiveWartOnTheEndOfHisNoseAndItLooksReallyFunny', or whatever you want) that is 30 seconds after the current game's time.
--As this will run once, at the very start of the level, it will be 30, unless it is changed.

while GameRunning() do -- while the game is running...
if GetGameTime() >= then --check if the game has been running for a number of seconds that is higher than the number given to the varible 'timer'(or 'MyNeighbourWhoIsCalledJoeBloggsHasAMassiveWartOnTheEndOfHisNoseAndItLooksReallyFunny', if that's what makes you happy)
-- if it has, then...do something
GetAsteroid(0):AddSeedlings(100) --your code
GetAsteroid(1):AddSeedlings(100) --your code
GetAsteroid(0):SendSeedlingsToTarget(2,100,GetAsteroid(2)) --your code
GetAsteroid(1):SendSeedlingsToTarget(3,100,GetAsteroid(2)) --your code
timer = GetGameTime() + 30 --resets the timer, to thirty more than the game time.
end --end the check
coroutine.yield() --return control to something else, ready to be picked up again later if there is a need.
end --end the loop

end

PS, It's lua that you use for levels, not C#, but I'm just being picky.  ;D

PPS, I really, really recommend that you get a good text editor, like Notepad++  (that's the one I use, don't know about any others...)

EDIT: Hold on, that's not right. It'll work fine, but not the way you want. I'll post new stuff.
« Last Edit: July 22, 2010, 05:06:03 AM by Pilchard123 »

Terrial

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
Re: Another noobie lua coder
« Reply #5 on: July 22, 2010, 05:04:45 AM »
Alright cool! The syntax for lua (not C# then!) is much different from what I'm used to.  I took a programming class in High School and for the strangest reason they decided to teach us Visual Basic.  A fun language I have to admit, but it has few practical applications unless you want to add a mounatain of code just to turn it into something that resembles C.  Never the less, I love programming and on my own built my own game that is now about 250 pages of code (it's small i know but I really had to learn most of it as I was making it, looking it up online for functions)

The coroutine.yield() still confuses me though.  Once I think I've figured out it's use I see it in another place used for something similar but different.  Can you explain it's whole use? Like what the program does when it hits that line?

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Another noobie lua coder
« Reply #6 on: July 22, 2010, 05:14:19 AM »
Nope, it won't work at all. Sorry, I'm a bit lot tired.

If asteroid 0 is empire 2's homeworld and asteroid 1 is empire 3's homeworld then...

Code: [Select]
-- Up here you need to add your asteroids.

function LevelLogic()  --holds code to run during the game

timer = GetGameTime() + 30

while GameRunning() do -- while the game is running...
if GetGameTime() >= timer then
                        if GetAsteroid(2):GetNumSeedlings(2) < 50 then
     GetAsteroid(0):AddSeedlings(100)
                             GetAsteroid(0):SendSeedlingsToTarget(2,100,GetAsteroid(2))
                        end

                        if GetAsteroid(2):GetNumSeedlings(3) < 50 then
     GetAsteroid(1):AddSeedlings(100)
             GetAsteroid(1):SendSeedlingsToTarget(3,100,GetAsteroid(2))
                        end
timer = GetGameTime() + 30
end
coroutine.yield()
end

end

the timer is still there to allow the seedlings time to travel, otherwise thousands will spawn and crash your game before the waves get there.

The coroutine.yield() still confuses me though.  Once I think I've figured out it's use I see it in another place used for something similar but different.  Can you explain it's whole use? Like what the program does when it hits that line?

Y'know, it confuses me, too. Just read this, it seems to explain it, but I might just be in a fool's paradise.

Incidentally, that's the language I'm writing EUCLiD in, and the one I'll be doing in college (16-18, for those outside the UK).

Terrial

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
Re: Another noobie lua coder
« Reply #7 on: July 22, 2010, 05:33:24 AM »
So how does the level function know which asteroid is 1,2, etc. when you don't declare them by number?  Is it just the order they are put in?

Also on my level, I have 3 asteroids that I put in myself, put it spawns a whole bunch just inside the range of the 3 I've made, and I want to be able to customise the map difinitively, and not just have it be randomly produced.

Terrial

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
Re: Another noobie lua coder
« Reply #8 on: July 22, 2010, 06:25:27 AM »
Okay, So while trying to set this up, I've managed to break the level and I don't know what I did to screw it up.  Can someone look over my code?

I would just post the code, but it would be huge and I don't know how to make those spoiler boxes

Bonobo

  • Achiever
  • Old Oak
  • ****
  • Thank You
  • -Given: 139
  • -Receive: 12
  • Posts: 670
  • Eufloria: Yes
Re: Another noobie lua coder
« Reply #9 on: July 22, 2010, 06:39:45 AM »
[..] I don't know how to make those spoiler boxes
Well, if nothing else, then I can at least add the following to a dev thread:

(click to show/hide)

Cheers, Tom

Terrial

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
Re: Another noobie lua coder
« Reply #10 on: July 22, 2010, 06:48:52 AM »
cool boxes! heh heh

Here's the code then:

(click to show/hide)

Terrial

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
Re: Another noobie lua coder
« Reply #11 on: July 22, 2010, 12:35:06 PM »
Ah, I found the problem.  Asteroids need to have the send distance at a level where at least one can reach it.

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Another noobie lua coder
« Reply #12 on: July 22, 2010, 05:17:16 PM »
You don't actually need to set the send distance at all, and it will work it out when the level is started.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Another noobie lua coder
« Reply #13 on: July 22, 2010, 06:30:02 PM »
Hey Terrial, awesome to see someone else making levels.  :>

I'll see if I can cover some of the questions asked too, even though Pilchard already did a sterling job.  :>



Quote
      *A clever loop with correct syntax goes here*


Here are two different types of loops:

While

This is usually used to continually run stuff.  Generally you would use this within Function LevelLogic().

Syntax:

while something do

your commands

coroutine.yield()
end


So for example:

Code: [Select]
while GameRunning() do
  GetAsteroid(1):SendSeedlingsToTarget(1,2,GetAsteroid(0))
coroutine.yield()
end

The "coroutine.yield()" bit is necessary to close off a while loop.  If you forget to add it, the level will not load.
I think of it as being synonymous with "end", when it comes to this type of loop.



For

This can be used in LevelSetup or LevelLogic, or anywhere else for that matter.  It creates a "ticker" variable, and runs through the loop a specified number of times.  Each time it goes through the loop, the "ticker" is incremented by one, and then when it reaches the specified number, the loop ends.



Syntax:

for ticker = start-number, end-number do

your commands

end



Here is an example of a For loop in action:


Code: [Select]
for i = 0,20 do
  GetAsteroid(0):AddSeedlings(i)
end

So in this example, we are saying run the loop 20 times, and on each occasion, add a number of seedlings to Asteroid 0 that is equal to whichever loop number we are on.  So the game would add 0 seedlings, then 1 seedling, then 2 seedlings, then 3, and so on, until eventually i reaches 20, it adds 20 seedlings, and the loop ends.




--





Quote
edit: also how can you make it send a laser mine to an asteroid?

When we refer to asteroids in our scripts, the way we do it is by assigning a variable to represent them.
You can assign the variable in a couple of different ways, here's the easiest:

Suppose Asteroid 0 has a mine belonging to empire 2, and you want it to travel to Asteroid 1.

Code: [Select]
fluffysbeautifulmine = GetAsteroid(0):GetMine(2)
There is only one mine at Asteroid 0, so that code will select it and from now on we can refer to it as fluffysbeautifulmine


Next lets send it to the neighbouring asteroid:

Code: [Select]
fluffysbeautifulmine:SendTo(GetAsteroid(1))
In that line, we told Fluffy's Beautiful Mine to start the journey toward Asteroid 1.






--






Quote
Also another question, can you instantly add seedlings to an asteroid of one empire that is controlled by another?  Example: Asteroid 1 is owned by empire 1, can you add 10 seedlings of empire 2 to asteroid 1?  Or can they only be put on asteroids owned by that empire?


You can.  :>
Consider the following code:


Code: [Select]
-- Asteroid 0
a = AddAsteroidWithAttribs(500,500, 0.5,0.6,0.4)
a.Owner = 1
a.TreeCap = 5
a:SetRadius(350)
a:AddSeedlings(200)
s = a:AddDysonTree()
a.Moveable = False

We gave ourselves an asteroid, a tree, and 200 seedlings.
Now lets make some enemies on the same asteroid:

Code: [Select]
GetAsteroid(0):AddSeedlings(300,2,0.3,0.2,0.1)
This line will put seedlings onto Asteroid 0.  There will be 300 of them, belonging to Empire 2, and they will have energy 0.3, strength 0.2, and speed 0.1

So you can expect that as soon as the level starts, this asteroid will be the site of a massive battle!
I would ultimately expect the player seedlings to win, as their stats are so much better than those belonging to Empire 2.






Let us know how you get on.  :>