Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: jakeflee on November 28, 2013, 05:45:19 PM

Title: Lag pls help
Post by: jakeflee on November 28, 2013, 05:45:19 PM
so i made a small bit of code to make an invisible creeper emiter that makes the game harder the longer the game goes but it causes lots of lag could some one help me ?
the 1 ->x thing was to prove to my self that i could use variables in this way.
this is so much more difficult than making little programs on a calculator :(
while 1 1 repeat
1 ->x
elapsedtime ->t
<-t 1000 div ->tm
(<-tm 3 mul) 100 add ->c

AddCreeper 1 <-x <-c

endwhile
Title: Re: Lag pls help
Post by: jakeflee on November 28, 2013, 05:47:00 PM
did you know adding a clearstack to the end of this code crashes the game :D and is does something weird in 1.06 it replaces the map with a prospector map but keeps all of your scripts  ???
Title: Re: Lag pls help
Post by: Clean0nion on November 28, 2013, 05:52:53 PM
Quote from: jakeflee on November 28, 2013, 05:47:00 PM
did you know adding a clearstack to the end of this code crashes the game :D and is does something weird in 1.06 it replaces the map with a prospector map but keeps all of your scripts  ???
Go to Documents/CreeperWorld3/WorldEditor/(name of your world)/
You'll see a file named (something).cw3
Rename it save.cw3
Title: Re: Lag pls help
Post by: jakeflee on November 28, 2013, 05:53:18 PM
ok i see save.cw3 and save-auto.cw3 w/e it was just a blank world that had nothing on it so i could learn crpl
Title: Re: Lag pls help
Post by: eduran on November 28, 2013, 06:03:33 PM
Check out http://knucklecracker.com/wiki/doku.php?id=crpl:crpltutorial:code_examples#create_an_emitter_that_increases_its_output_rate_over_time for a well documented example of an emitter with increasing output.

The lag/crash is caused by your while loop, which you turned into an infinite loop by setting the part between 'while' and 'repeat' to be a constant 1.
Title: Re: Lag pls help
Post by: Clean0nion on November 28, 2013, 06:40:45 PM
Quote from: eduran on November 28, 2013, 06:03:33 PM
Check out http://knucklecracker.com/wiki/doku.php?id=crpl:crpltutorial:code_examples#create_an_emitter_that_increases_its_output_rate_over_time for a well documented example of an emitter with increasing output.

The lag/crash is caused by your while loop, which you turned into an infinite loop by setting the part between 'while' and 'repeat' to be a constant 1.
The code is sorted, now to your map.
I'm assuming you just made a file named save.cw3 in that folder. Never do that. Always use the Worldeditor to create worlds. If you want a flat world, just go to the Worldeditor and hit create.
Title: Re: Lag pls help
Post by: jakeflee on November 28, 2013, 07:01:52 PM
no i didnt save the map when i created it
Title: Re: Lag pls help
Post by: jakeflee on November 28, 2013, 07:03:34 PM
i made a faster code after rewriting how it was warping in the code it is now something like this:
while 1 1
repeat
Elapsedtime ->t
(<-t 2 mul) 10 add ->c
1 1 <-c AddCreeper
delay 3
endwhile


just need to make it balance and it will work just fine.
Title: Re: Lag pls help
Post by: Grayzzur on November 28, 2013, 09:51:22 PM
It looks like you're a little off on how your CRPL scripts are executed and how the language syntax is structured. Your while loop is set up as an infinite loop and leaves an extra 1 on the stack. This is not necessary. CRPL scripts will be executed every game tick (30 times a second) unless waiting on a delay statement. Your use of parenthesis (the warp operator) is a little strange and may be causing unexpected results. The delay statement is probably picking up the 1 left over from the while loop and not that 3. I say "probably" because I'm not in front of a computer where I can run your code at the moment.

It's a stack based language. You push values on the stack and then perform operations on them. The warp operator warps a command to the end of the parenthesis, allowing you to write code that looks a bit more lik either languages. Your delay statement for instance, to delay 3 game ticks, should be:3 delay or with the warp operator: delay (3)

Your while loop is completely unnecessary as you have it written.

The line where you're working on t should either have the parenthesis removed, or use the warp operator as mul (<-t 2)
The entire line could be add ( 10 mul (<-t 2)) ->c or <-t 2 mul 10 add ->c
Title: Re: Lag pls help
Post by: jakeflee on November 28, 2013, 10:35:17 PM
it was practically my first file in this language im used to ti83/84 c++ and lua not an rpl i never learned a programming language like this. plus all of the tutorials looked kind of boring only making creeper at a constant rate, i could have probably looked at the syntax more before i tried to go head first in to a program but to do is the best way to learn. but yeah i had almost no clue what i was doing and using your tips and changing addcreeper to setcreeper it makes the code work
:D
the code looks like this now
while 1
repeat
elapsedtime ->t
<-t 1000 div 2 mul 10 add ->c

<-c 2000 lte if
47 47 <-c setcreeper
endif

<-c 2000 gt if
47 47 2000 setcreeper
endif
3 delay
endwhile

hey anyone by chance knows the max creeper that can be in a spot until it is turned to anticreeper?
Title: Re: Lag pls help
Post by: Grayzzur on November 29, 2013, 08:19:08 AM
You'll get the hang of it!

Creeper becomes AC a little over 2147.
Title: Re: Lag pls help
Post by: Annonymus on November 29, 2013, 08:48:37 AM
The while loop is still unnecessary, if you use the code without the while it will be executed 10 times a second, as it is now i think it gets executed 500 times per tick (1/30 of a second) which should cause very heavy lag.
Also to make the emitter more like the emitters of the storyline you could use setcreepernolower instead of setcreeper, so it wont remove creeper if there is more than the amount the emitter produces.
instead of 2 "if" you could use

<-c 2000 gt if
currentcoords <-c setcreeper
[u]else[/u]
currentcoords 2000 setcreeper
endif


Also you can use currentcoords instead of fixed coordinates, then it will take the coordinates of the core, that will also make it easier if you want to create another unit based on this one (for example if you want to create a moving emitter that gets stronger in time you could use this code + the code you will make for moving the emitter without having to rewrite anything)
Title: Re: Lag pls help
Post by: jakeflee on November 29, 2013, 12:21:12 PM
actually the loop made almost no lag when i ran the new revised code and there is a delay of 3 frames to stop the loop from killing the computer i used fixed cords so i could get the hang of putting creeper where the core was not.
Title: Re: Lag pls help
Post by: Grayzzur on November 29, 2013, 03:35:59 PM
The delay of 3 frames is what's keeping that loop from killing you. The while loop is still unnecessary. CRPL Core scripts are executed every game tick, you do not need a loop to make it run again. The loop would be useful if you wanted to do something like set creeper across a range of cells every time you updated.

The reason it lags when you don't have the delay is because of the loop. It's an infinite loop, which normally would try to run repeatedly inside of a single game tick (causing your initial lag problem) but with a delay in the middle of it now.
Title: Re: Lag pls help
Post by: knucracker on November 30, 2013, 10:10:04 AM
There is a maximum number of instructions that a core will execute per frame (I think it is either 500 thousand or 1 million currently).  This is to prevent something like an infinite loop from being infinite and totally hanging the game.  However, executing that many instructions per frame will definitely cause lag.