Issues - Android (DEF-3098) (SOLVED)

Hi guys!

I am almost done with a game I am developing, and everything is working fine on windows.
However when I tested out the game on Android, there were some bugs:

  1. In my game I needed to draw a line from one fixed point to a variable point so I thought of using render.draw_line
    On Android: the line isn’t showing

  2. In my game I need to create multiple game objects in different random positions. On windows, it is working perfectly fine
    On Android: All objects are being spawned in the same location
    Note: I am using the following
    math.randomseed(os.time()* 1000000 * math.random() * math.random())
    local yPos = math.random(267, 700)

Note: the android device I’m using is Sony Xperia Z2

EDIT: I was able to solve the following issue: The problem was that I was checking for both mouse click and multi-touch event when checking for user input (i.e: if (action_id == hash(“click”)) or (action_id == hash(“touch”)) then) I removed the mouse trigger action and the problem was solved!

Solved issue: In my game there’s an option to buy multiple items, and when the user chooses to do so a message is sent to deduct coins from the player’s coins.
On Android: the message is being sent twice (However, this is working perfectly fine when I tested it in the Defold Editor)

1 Like

Are you seeding the RNG before every call to math.random()? Seed the RNG once in a script on startup and then call math.random() throughout your game.

1 Like

Thanks I fixed that. The problem is still the same though. It’s working perfectly fine in the defold editor but not on Android :confused:

Ok, just so I understand:

  • You randomise positions of game objects using math.random()
  • On your local machine the game objects all get random positions but on Android all game objects get the same position?

Can you show the code used when spawning the game objects and assigning their positions?

Yup exactly

After making the fix you told me to do, the objects are now being spawned at random positions, but every time I launch the game they are spawned at the same exact position, rather than having different positions.

        if go.get("/path", "Object1") then
		local object1Pos= go.get_position()
		
		local objectxPos= math.random(-10, 1290)

		local objectyPos= math.random(267, 700)
		object1Pos.x= objectxPos
		object1Pos.y= objectyPos
		object1Pos.z = 0.5
		local component= "#object1Factory"
		object1= factory.create(component, object1Pos)
	end

this is the init function for the same script:

function init(self)
math.randomseed(os.clock()*100000000000)
end

Note: this is done for multiple objects in the same script

That means your random seed is the same every time. If you’re setting the seed on startup then os.clock() may be the same or very similar every time. Try using os.time() for your seed, or maybe something like: socket.gettime() % 1 * 1000

1 Like

Ohh now I see it :laughing:
Thank you! It worked!

1 Like

But but, didn’t he use os time() already? I typically seed with just os.time or socket.gettime and perhaps throw away the first two values.

1 Like

I tried multiple cases including:

math.randomseed(os.time()* 100000000000)
math.randomseed(os.time()* 1000000 * math.random() * math.random())
and math.randomseed(os.time())

Only math.randomseed(os.time()) worked fine :hushed:

1 Like

That’s a huuuuge number.

This could potentially result in the first or second math.random() returning 0 and then you’d always get the same result.

This is the recommended way to seed th RNG (or possibly using socket.gettime())

1 Like

Unfortunately, it was accidentally removed in release 1.2.117, as part of an optimization effort.
We’ll add this back in the next release (DEF-3098)

2 Likes

Draw_line was reintroduced in Defold 1.2.120

2 Likes