Correct way to apply code to all objects in a table?

I should probably ask if anyone knows of a lua-specific forum where I could be asking these questions…!

I am trying to write a code that does parralax scrolling. All the sprites in the table need to shoot up into the air, then have their position reset to y=200 and x=randomnumber. Later on, I’m gonna make them smaller or bigger depending on how fast they’re moving for a true 3D effect.

Here’s a naive attempt:

local speed = math.random(250,1000)
pieces = { "sprite", "sprite1", "sprite2", "sprite3", "sprite4", "sprite5", "sprite6", "sprite7", "sprite8", "sprite9", "sprite10", "sprite11", "sprite12", "sprite13", "sprite14", "sprite15" } 

function update(self, dt)
	for i, v in ipairs(pieces) do
		p = go.get_position(v)
		p.y = p.y + speed * dt
		print(current)
		if p.y > 800  then
		p.y = -200
		p.x = math.random(1,25) * math.random(10,35)*2
		local speed = math.random(250,1000)
	end
	go.set_position(p)
end
end

If i print “v” inside the ipairs i get the ID of the object, so I thought that what I have above would work. It doesn’t. Any help?

I think your issue is related to the go.set_position§ outside of the for loop. The go.get_position seems ok. Also you should make the p local.

Ignore that… got tired and didn’t count the ends.

go.get_position(v) gives me an error:

ERROR:SCRIPT: main/starscript.script:6: Instance sprite not found
stack traceback:
[C]: in function 'get_position’
main/starscript.script:6: in function <main/starscript.script:4>

though print(v) correctly prints the ID of the sprite. I have checked the ID of the sprites and they are all correct. The script and the sprites are both part of the same game object.

You will need to pass the instance, not the instance name (string).

Either you put the instances in the table:

pieces = { go.get_id("/sprite"), go.get_id(" …
(assuming “sprite” and “sprite1”… are gameobjects)

or you do it in the loop
p = go.get_position(go.get_id(v))

1 Like

Thank you genius!

They are sprites. They have an ID, but they’re not game objects, right?

print(go.get_id(v))
returns
hash: [/sprite6]

but i still get an error “ERROR:SCRIPT: main/starscript.script:6: Instance (null) not found” when running go.get_position

edit: just checked, it doesn’t matter if the sprites are game objects are just sprites

By the way… I really am looking for a place to read up on lua. The official manual and basically all other resources are really complicated (at least for someone with a none coding background)

2 Likes

Okay, the correct answer is…

local speed = math.random(250,1000)
pieces = { "sprite1", "sprite2", "sprite3", "sprite4", "sprite5", "sprite6", "sprite7", "sprite8", "sprite9", "sprite10", "sprite11", "sprite12", "sprite13", "sprite14", "sprite15" } 


function update(self, dt)
	for i, v in ipairs(pieces) do
		p = go.get_position(v)
		p.y = p.y + speed * dt
		if p.y > 800  then
			p.y = math.random(200,800)/-1
			p.x = math.random(1,25) * math.random(10,35) * 2
			local speed = math.random(250,1000) *1
			go.set_position(p, v)
			else
			go.set_position(p, v)
		end	
	end
	end

the only problem is that the random numbers come out the same for each iteration. I am investigating.

You need to call math.randomseed(os.time()) sometime early in your code to seed the pseudo-random number generator with a value that will make it behave differently on each run.

Also, what is p in the example above? It looks like a global variable. You should try to avoid those unless you know what you’re doing (prefix it with the local keyword).

Finally the local speed = math.random(250,1000) *1 will do nothing. Are you supposed to change the speed declared at the top of the snippet of code you pasted? If that is the case you need to remove the local keyword in front of where you are redeclaring it.

Thanks britzl. That code is a bit of a mess, I was trying lots of different things without properly clearing up inbetween each one. In the end I gave up and just made a script and then copied it to each object.