Posted: 7th Oct 2022 19:35
Hello all together, i am training with types and functions, i wan?t do the following and i would be happy if someone helps

The code to train is very simple:

+ Code Snippet
type player
	sprite as integer
	posx as integer
	posy as integer
endtype

Global player1 as player
Global player2 as player

global targetx=1000
global choose
global moveright=1

setup_players()

global d

do

update_players(1,player1)
update_players(2,player2)

 Sync()
loop

function setup_players()	
	player1.sprite = createsprite(loadimage("kugel_rot.png"))
	player2.sprite = createsprite(loadimage("kugel_blau.png"))
	player1.posx=200
	player1.posy=200
	player2.posx=600
	player2.posy=200
endfunction

function update_players (a as integer, d as player)
	
	if moveright=1 and choose=a and d.posx < targetx 
		inc d.posx, 5
	elseif choose=a
		moveright=0
	endif
 setspriteposition(player1.sprite,player1.posx,player1.posy)
 setspriteposition(player2.sprite,player2.posx,player2.posy)
	
endfunction



My plan is, that i have the update_players function 1 time and call with the player1 and player2.
The choose values with 1 and 2 works, but the player1 and player2 doens?t work, the sprites are not moving but the compiler don?t give an error.
Is there a way to handle this? This would shorten the code extremely.

Thanks for your help.
Posted: 7th Oct 2022 21:37
It looks like the problem is your are increasing the posx of d and then calling the setspriteposition on the original posx and posy of the players as they aren't being changed. What you might try doing is possibly set d as ref cause I think d is creating a copy of player type and not a reference to the original variables so nothing is getting updated so that may be why the sprites are moving. Then again not tested so can't be sure but you might try changing "d as player" to d ref as player" and see what happens.
Posted: 7th Oct 2022 22:13
you must use reference of player
at the function definition try this;

+ Code Snippet
function update_players (a as integer, d ref as player)
Posted: 9th Oct 2022 17:21
Thanks for your help this works.