How to make my game faster?

218 views
Skip to first unread message

עומר משי

unread,
Jan 2, 2022, 9:47:05 AM1/2/22
to DroidScript
Hello
I need help to make my game app faster and smoother.

1. I created a game based on images. I draw the game platform on an image object.

2. I use img.SetAutoUpdate(false) and  img.Update() 
For updating the screen I use setTimout(my_game_function, 0); and before I Update the screen I check if 20 milliseconds left since the last update. I also check if 1000 milliseconds left to update the score.

3. I also did: app.SetDebug( "console" );

What else can I do to make my game even more faster and smoother?
By the way, this is an online game (I created a python server).

wheelie tips

unread,
Jan 2, 2022, 10:19:50 AM1/2/22
to DroidScript

you could try also:
app.SetDebugEnabled( false )

Alan Hendry

unread,
Jan 2, 2022, 10:38:13 AM1/2/22
to DroidScript
HI,
setTimout(my_game_function, 0) 
will invoke my_game_function after zero milliseconds (thousandths of a second) once
To run it every 20 milliseconds (50 times a second) repeatedly I'd
st = setInterval(my_game_function,20)
When you want to stop it
clearInterval(st)
And to update the score have another interval timer
st2 = setInterval(update_score,1000)
To be stopped like this
clearInterval(st2)
Regards, ah

עומר משי

unread,
Jan 2, 2022, 10:38:44 AM1/2/22
to DroidScript
but than I will not be able to write to the console..
As I said, I use app.SetDebug( "console" ); instead..

ב-יום ראשון, 2 בינואר 2022 בשעה 17:19:50 UTC+2, wheeli...@gmail.com כתב/ה:

עומר משי

unread,
Jan 2, 2022, 1:47:13 PM1/2/22
to DroidScript
0 milliseconds means that when the application is ready this function will be called. I tried use setInterval but it didn't change the fps at all

ב-יום ראשון, 2 בינואר 2022 בשעה 17:38:44 UTC+2, עומר משי כתב/ה:

Alan Hendry

unread,
Jan 2, 2022, 4:10:18 PM1/2/22
to DroidScript
HI,

Are you doing this?
img.SetAutoUpdate(false) 
then draw on the image (points, lines, rectangles, circles, other images) and when ready
img.Update()

You could try turning debug off when you don't need it, and on when you do.

You could look at Batch which combines multiple  actions on one object
var q = app.AddText( qlay,qtxt )
    q.Batch({
        SetBackColor_: ["#333333"], // note underscore
        SetTextColor: [ "#999999" ],
        SetPadding_: [20,20,20,20,"px"],  // note underscore
        SetMargins_: [20,20,20,20,"px"],  // note underscore
        SetTextSize: [32,""],  
        SetOnTouchDown: [q_OnTouch]
    })
N.B. Methods need all their parameters (or null for unused parameters)
Some methods actually must be applied to a parent object, so need the trailing underscore
Perhaps something like
    img.Batch({
        Clear: [],
        DrawRectangle: [0,0,0.5,0.5 ],
        ...
        Update []
    })
You'd need to experiment.

Regards, ah

עומר משי

unread,
Jan 2, 2022, 5:27:10 PM1/2/22
to DroidScript
Can you explain how Batch will make the code faster? I don't familiar with that.

ב-יום ראשון, 2 בינואר 2022 בשעה 23:10:18 UTC+2, hendr...@gmail.com כתב/ה:

Alan Hendry

unread,
Jan 2, 2022, 6:38:07 PM1/2/22
to DroidScript
HI,

Have you seen https://symdstools.github.io/Docs/docs/app/CreateImage.htm ?
Example Advanced Clock Animating
It disables html and js Dom elements
    cfg.No_Dom
and instead of Timeout
    app.Animate( OnAnimate, 40 );

Batch is brand new with 2.50
You make one "call" to Batch passing in JSON, instead a lot of individual "calls"
Probably need the technical team to advise on what we can do, and how to do it
This works
    img7.Batch({
        Clear:[],
        SetBackColor_: ["red"],
    })
Regards, ah

עומר משי

unread,
Jan 3, 2022, 12:11:35 AM1/3/22
to DroidScript
The noDom isn't working as far as I know on the latest version of Android. 
The animate function also didn't change at all the fps
@Dave can you please explain how the batch works and if it makes the code run faster? 

ב-יום שני, 3 בינואר 2022 בשעה 01:38:07 UTC+2, hendr...@gmail.com כתב/ה:

Steve Garman

unread,
Jan 3, 2022, 6:37:10 AM1/3/22
to DroidScript
@Omer
Just a couple of comments on the previous dis cussion in this thread
1) You are correct that nodom is not available

2) If you  code consists of a single loop which consists of updating all the visible elements then calling setTimeout again and if a single call to setInterval as a replacement for the setTimeouts you dhould seriously consider app.Animate as a replacement because you should be able to use it to make your movement smoother

Now about Batch()
Probably the main speed bottleneck to DroidScript is the JavaScript > java bridge
It is the mechanism used to call java functions from DroidScript's JavaScript whe it calls an app.*** method

1single call across the bridge is barely noticeable but if your app makes multiple calls across the bridge before anything happens on the screen this begins to get noticeable

Batch addresses this for the specific situation where multiple values need to be set for one control by accomplishing them in a single call across the bridge

This is pasted from a post by Alan in the beta group:
//////////
For a Text Box these work


    var q = app.AddText( qlay,qtxt )
    q.Batch({
        SetBackColor_: ["#333333"], // note underscore
        SetTextColor: [ "#999999" ],
        SetPadding_: [20,20,20,20,"px"],  // note underscore
        SetMargins_: [20,20,20,20,"px"],  // note underscore
        SetTextSize: [32,""],  
        SetOnTouchDown: [q_OnTouch]
    })
//////////

A couple of complications:
a) Batch does not waste time counting methods so it is our responsibility to pass all the parameters. Any you would ignore when coding, pass null or ""

b) Some controls use base methods rather than methods specifically created for them, for instance the Text control does not actually have a SetBackColor method, it inherits that method from it's parent object.
In those circumstances we add an underscore to the method name as above
Gone() and Hide() are other examples

Steve Garman

unread,
Jan 3, 2022, 6:54:44 AM1/3/22
to DroidScript
Please don't start out creating your apps using Batch.
Start out with separate lines of code so your code is readable and debugging is not unnecessarily difficult

However if performance turns out to be a problem (usually inside loops) then it could come in handy

Dave

unread,
Jan 3, 2022, 9:28:34 AM1/3/22
to DroidScript
Hi Guys, the Batch() method will probably not help in this case (may not even work) as the image drawing mechanism already has a built-in batching facility which comes into play every time you draw to the image while the AutoUpdate mode is disabled.

Here are two suggestions for speeding up or smoothing your game - 

1. Covert it to use the GameView.  This will also have the benefit of allowing it to work in browsers, however if you have a lot of touch objects to deal with then it gets more complicated detecting which object has been touched.

2. Roll back to an older version of DS and use the cfg.NoDom option.  This option will make a come-back very soon as others want to make use of it.

In both cases, make sure your graphical objects are only as large as they have to be, so that less memory is consumed and the inevitable copying from system to graphics memory reduced.  With normal apps there is not point in setting the frame rate any higher than 60 fps as Android won't update the screen faster than that and it will probably slow things down instead.

Alan Hendry

unread,
Jan 3, 2022, 10:09:22 AM1/3/22
to DroidScript
HI,

The classical approach would to be to do as much as possible before running my_game_function,
and as little as possible inside  my_game_function.
For example before my_game_function ...
set background (color or image) of your large image img0.SetBackground("/Sys/Img/Sky.png","repeat")
(perhaps set paintcolor, paintstyle, linewidth, margins, padding)
set up images of player/missiles/etc bunny = app.CreateImage("/Sys/Img/Bunny.png")
In my_game_function ...
Draw the bunny on the large image img0.Drawimage(bunny,x,y,w,h,angle,mode)
(It may speed it up a bit if the images for the player/missile are the right size, and don't need scaling)

Have you seen the Sample (touch Rocket in top right) for Game Bunny Storm?
Watch it for 5 seconds, then touch the screen, repeat.

In Batch DrawLine and DrawText seem to work ,
but DrawImage doesn't draw, and DrawRectangle:[0.1,0.1,0.2,0.2] gives Debug error 
WARNING: Img.DrawRectangle) failed! (For input string: "ngle")
Probably best avoided

Cinemas used to project at 24 frames per second, 
but Disney originally used 12 drawings per second
So getting the movement right can be more important than high frame rate.

Regards, ah

עומר משי

unread,
Jan 3, 2022, 10:12:55 AM1/3/22
to DroidScript
Thanks a lot for you!
Dave, switching to gameview is a little bit a problem for me because I already wrote all the code and this game is for my Hight school final project.
How much the cfg.NoDom can make the game faster?
Also, I saw in some place that the app.Animate() is no longer supported and it behaves like setIntervel and not faster then it.

ב-יום שני, 3 בינואר 2022 בשעה 16:28:34 UTC+2, Dave כתב/ה:

Dave

unread,
Jan 3, 2022, 11:52:55 AM1/3/22
to DroidScript
Try these things - 

1. If you are Premium, then you could try switching to NodeJS mode by putting cfg.Node at the top of your main file.  

2. Try putting _Boost( true ) at the top of your main file.

Otherwise you will need to downgrade DS to 1.74 in order to get the cfg.NoDom option to work.  Previous tests have proved this method improves performance by as much as 5x


app.Animate is supported and you should use that instead of setInterval or setTimeout to render your frames (Especially if you use NoDom).


עומר משי

unread,
Jan 3, 2022, 12:12:49 PM1/3/22
to DroidScript
I get an error

ב-יום שני, 3 בינואר 2022 בשעה 18:52:55 UTC+2, Dave כתב/ה:
צילום מסך 2022-01-03 191222.png

Steve Garman

unread,
Jan 3, 2022, 12:25:57 PM1/3/22
to DroidScript
You just want
    _Boost( true ) 

עומר משי

unread,
Jan 3, 2022, 12:49:15 PM1/3/22
to DroidScript
know I get an error: Script Error: send_login_request is not defined, Line 47, school%20project.js
I get it for every function in my code..


ב-יום שני, 3 בינואר 2022 בשעה 19:25:57 UTC+2, steve....@gmail.com כתב/ה:

Dave

unread,
Jan 3, 2022, 1:09:06 PM1/3/22
to DroidScript
You shouldn't mix _Boost and cfg.Node, use one or the other.

If you use cfg.Node then you will need to include your other scripts using require not app.Script()

עומר משי

unread,
Jan 3, 2022, 3:41:49 PM1/3/22
to DroidScript
which one of them is better?

ב-יום שני, 3 בינואר 2022 בשעה 20:09:06 UTC+2, Dave כתב/ה:

Dave

unread,
Jan 3, 2022, 5:11:59 PM1/3/22
to DroidScript
I don't know because I can't see your code.  I would suspect that the cfg.Node would be the best of those two, but you might have to change some of your function declarations with the cfg.Node method, so I would test the _Boost( true ) option before the cfg.Node option as it will be easier to test.

The best speed up will probably result from rolling back and using cfg.NoDom, but that means using an older version of DS

עומר משי

unread,
Jan 4, 2022, 1:19:06 AM1/4/22
to DroidScript
This is my code. I didn't see any difference by using these 2 methods

ב-יום שלישי, 4 בינואר 2022 בשעה 00:11:59 UTC+2, Dave כתב/ה:
school project.spk

Alan Hendry

unread,
Jan 5, 2022, 6:44:28 AM1/5/22
to DroidScript
HI,

Presuming that Update() is the code run to update the screen.

Minor improvement -
            this.img_bases.SetPaintColor("#99ff99");
            this.img_bases.DrawRectangle(0, 0, 1, 1);
This seems to be equivalent to 
            this.img_bases.SetBackColor("#99ff99");
I believe that the SetBackColor could be done during initial set up (when game starts)
and don't do it in Game.
says that Clear() "Clears everything on the image except the background."

Possibly bigger speed up
                var base_img = app.CreateImage(this.bases[i].GetBase(), 0.2, -1);
                var plane_img = app.CreateImage(this.planes[i].GetPlane(), 0.1, -1);
are executed for every base/plane every frame
I believe they have to retrieve the image from your Images folder every time
I'd try to retrieve the images during initial set up and re-use them in Update.
Other possibilities are during initial set up to CreateImage for the 2 planes and 3 bases, 
GetPixelData for each and store them, 
then in Update CreateImage with no file name ( an empty image) then SetPixelData.

Regards, ah
Reply all
Reply to author
Forward
0 new messages