Author Topic: Common coding tasks  (Read 2141 times)

0 Members and 1 Guest are viewing this topic.

Tomfloria

  • Shrub
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 232
  • First iOS modder :D
Re: Common coding tasks
« Reply #15 on: May 18, 2012, 05:20:18 PM »
I'll show you part of one of my levels


Thats in LevelLogic()

Code: [Select]
if IsiOS() then -- Defence, Find your allies
SetCameraZoom(2000)
SetCameraPosition(0,0)
SelectionClear()
WaitReal(2)
SetLevelDim(true)
Message("-Hired Defence-~Find your lost brothers and sisters, don't forget what you've learnt~This is unknown territory", true, 1.0, "Top")
WaitMessage(true)
SetLevelDim(false)
else
MessageBox("01_01")
WaitDialog()
end

if IsiOS() then -- Be Prepared First
WaitReal(2)
Message("There should be enough seedlings to plant 4 Defence Trees, and maybe more,~Search the Asteroid Belt for seedlings.", true, 1.0, "Top")
WaitMessage(true)
else
MessageBox("01_02")
WaitDialog()
end

while GetEmpire(1).NumSeedlings < 42 do
CheckConditions()
coroutine.yield()
end

if IsiOS() then -- Asteroid Belts are everchanging
WaitReal(2)
Message("Now you have enough seedlings to create 4 Defence Trees~Find an Asteroid that supports planting.", true, 1.0, "Top")
WaitMessage(true)
else
MessageBox("01_02")
WaitDialog()
end

while GetEmpire(1).NumTrees < 1 do
CheckConditions()
coroutine.yield()
end

This is whats in my GameRunning() loop

Code: [Select]
while GameRunning() do
CheckConditions()
coroutine.yield()
end

Tomfloria

  • Shrub
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 232
  • First iOS modder :D
Re: Common coding tasks
« Reply #16 on: May 18, 2012, 05:20:50 PM »
Show me what you can do baby :D

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 899
  • Eufloria: Yes
Re: Common coding tasks
« Reply #17 on: May 18, 2012, 05:33:59 PM »
The odd/even check can be simplified to:
Code: [Select]
if x % 2 == 1 then
  //is odd
else
  //is even
end

It misses out the nasty divides.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Common coding tasks
« Reply #18 on: May 18, 2012, 06:03:12 PM »
Ooh.  What does that % do? :>

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Common coding tasks
« Reply #19 on: May 18, 2012, 06:53:12 PM »
Well first let me point out what your method doesn't let you do;

You can't run any kind of additional mechanics during this sequence of delays that you have
EG you couldn't run gravity, or an AI, or a parallax/starfield engine, at the same time as this stuff.


So if you only ever want to make levels that are a sequence of events seperated by delays, your method works fine and is quite intuitive.  However, if you want to progress beyond that, it's worth figuring out how to do it in a while GameRunning() loop.



You didn't include your entire level so I'm guessing large parts of it, but here goes:

Code: [Select]
function LevelSetup()
SetCameraZoom(2000)
SetCameraPosition(0,0)
if IsiOS() then
SelectionClear()
end
latch = {}
latch[0] = false
latch[1] = false
latch[2] = false
end

function IsiOS()
return false
-- i can't make it run without defining this function...  it seems like IsiOS isn't recognised on my older PC version.
end

function CheckConditions()
-- presumably some code goes in here..
end

function LevelLogic()

while GameRunning() do -- Run this loop continuously
if GetGameTime() > 2 and latch[0] == false then
latch[0] = true
if IsiOS() then
SetLevelDim(true)
Message("-Hired Defence-~Find your lost brothers and sisters, don't forget what you've learnt~This is unknown territory", true, 1.0, "Top")
WaitMessage(true)
SetLevelDim(false)
else
SetVignetteAlpha(255)
MessageBox("-Hired Defence-                                             Find your lost brothers and sisters, don't forget what you've learnt.  This is unknown territory")
Pause()
WaitDialog()
Unpause()
SetVignetteAlpha(255)
end
end

if GetGameTime() > 4 and latch[1] == false then
latch[1] = true
if IsiOS() then
Message("There should be enough seedlings to plant 4 Defence Trees, and maybe more,~Search the Asteroid Belt for seedlings.", true, 1.0, "Top")
WaitMessage(true)
else
MessageBox("There should be enough seedlings to plant 4 Defence Trees, and maybe more.  Search the Asteroid Belt for seedlings.")
Pause()
WaitDialog()
Unpause()
end
end

if GetEmpire(1).NumSeedlings < 42 or GetEmpire(1).NumTrees < 1 then
CheckConditions() -- No idea what this does but you included it, so I will too
end

if GetEmpire(1).NumSeedlings > 42 and latch[2] == false then
latch[2] = true
if IsiOS() then
Message("Now you have enough seedlings to create 4 Defence Trees~Find an Asteroid that supports planting.", true, 1.0, "Top")
else
MessageBox("Now you have enough seedlings to create 4 Defence Trees.  Find an Asteroid that supports planting.")
end
end

coroutine.yield()
end
end


I still can't make IsiOS() work by the way.

I'll look for a way to fix that problem now..

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Common coding tasks
« Reply #20 on: May 18, 2012, 06:57:08 PM »
Not particularly tidy but it works. :>

Tomfloria

  • Shrub
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 232
  • First iOS modder :D
Re: Common coding tasks
« Reply #21 on: May 18, 2012, 06:57:51 PM »
So ISiOS() crashes your game?

CheckConditions btw is if something is something it will quit true or false

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Common coding tasks
« Reply #22 on: May 18, 2012, 07:22:13 PM »
So ISiOS() crashes your game?

Yes, it does.

And it's impossible to test if it exists without running it, and if you try to run it on PC, it crashes.

So it seems that won't work... however.. I have another plan.

Ipad3 resolution is 2048 x 1536 and Ipad 1 and 2 are both 1024 × 768.

2048 x 1536 is a resolution that is basically never used on a PC/Mac, as far as I know.  And 1024 x 768 is quite a low resolution for a PC... it's also 3/4 aspect ratio which is much less common than the widescreen 16:9 and 16:10 screens that tend to be used on computers these days.


So I think we could probably build a replacement function to put in our levels that will work on both PC and iPad.
The function would test the screen width and height to determine what platform it was on.

Something like this:

Code: [Select]
function IsThisiOS()
if GetScreenWidth() == 2048 or GetScreenWidth() == 1024 then
if GetScreenHeight() == 1536 or GetScreenHeight() == 768 then
return true
else
return false
end
else
return false
end
end

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Common coding tasks
« Reply #23 on: May 18, 2012, 07:23:19 PM »
I'll set up a test level that we can both try. :>

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Common coding tasks
« Reply #24 on: May 18, 2012, 07:26:25 PM »
Ok, I get PC detected.

What do you get? :>


Code: [Select]
function LevelSetup()

Globals.G.Asteroids=1

end


function LevelLogic()

if IsThisiOS() then
Message("iPad detected", true, 1.0, "Top")
else
MessageBox("PC detected")
end

end


function IsThisiOS()
if GetScreenWidth() == 2048 or GetScreenWidth() == 1024 then
if GetScreenHeight() == 1536 or GetScreenHeight() == 768 then
return true
else
return false
end
else
return false
end
end

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Common coding tasks
« Reply #25 on: May 18, 2012, 07:54:38 PM »
% called Modulo is kinda hard to explain with simple words, instead you can look at some examples:

Code: [Select]
1 % 2 = 1
2 % 2 = 0
0 % 2 = 0
77 % 7 = 0
69 % 7 = 6

This is what I get out of using it, it basically counts down how far away you are from the fraction of the first number. I don't know of an easy way to explain it, sorry...

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Common coding tasks
« Reply #26 on: May 18, 2012, 08:09:38 PM »
Ah cool... it returns the remainder of dividing the two results. :>

So if I do...

Code: [Select]
if x % 2 == 1 then
I'm saying "if x divided by two has a remainder of 1, then"

Then x must be odd :>
If it didn't have a remainder of 1, it must be even.

Fluffy understanding now.



Did you try the IsThisiOS() code? :>

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Common coding tasks
« Reply #27 on: May 18, 2012, 08:11:21 PM »
It's kinda nifty to use when you want to skip out some numbers, but include the others without using tons of if statements :)

Tomfloria

  • Shrub
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 232
  • First iOS modder :D
Re: Common coding tasks
« Reply #28 on: May 18, 2012, 08:13:03 PM »
You might like this :D

(click to show/hide)

Tomfloria

  • Shrub
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 232
  • First iOS modder :D
Re: Common coding tasks
« Reply #29 on: May 18, 2012, 08:16:28 PM »
Just to help you through the Message options aswell

Code: [Select]
Message("iPad detected", true/false, 0 to 1.0, "Top/Bottom/Centre/Left/Right")


Urm, I don't know if you can go any higher than 1.0, but thats basically the size, not transparency which I thought.