Posted: 11th Jan 2022 4:54
Hello,

I am trying to use the insert function on my custom type array and I am getting this error:

Error: Cannot insert a user defined type that is different from the array type


Here is my type array declaration:
+ Code Snippet
Global playerdefense as Unit[]


Here is my type declaration:
+ Code Snippet
Type Unit
	id as integer
	x as float
	z as float
Endtype


Here is the function definition:

+ Code Snippet
function createDefense(cellx, cellz)
		id as integer
		id = CreateObjectSphere(1, 10, 10)
		playerdefense.insert(id, cellx * 1, cellz * 1)
		SetObjectPosition(id, cellx, 2, cellz)
		SetObjectColor(id, DEFENSE_COLOR_RED, DEFENSE_COLOR_GREEN, DEFENSE_COLOR_BLUE, 100)
		SetObjectCastShadow(id, 1)
endfunction
Posted: 11th Jan 2022 6:13
Here's something to point you in the right direction. You need to create the unit and sphere as their own things, create the sphere and place it in the unit, then insert the until into the playerdefense array.
id.x and id.z will only keep a record of the position in this case, you also need to update the sphere position to reflect this each time you change the position within the instance.

I hope this helps.

+ Code Snippet
// Project: TypesExample 
// Created: 22-01-11

// show all errors

SetErrorMode(2)

// set window properties
SetWindowTitle( "TypesExample" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )



createDefense(0, 0, 255, 0, 0)
createDefense(2, 4, 0, 255, 0)
createDefense(0, 8, 0, 0, 255)


do
    Sync()
     
loop


Global playerdefense as Unit[]


Type Unit
    sphere as integer
    x as float
    z as float
     
Endtype


function createDefense(cellx, cellz, r, g, b)
        id as Unit
        sphere as integer
        sphere = CreateObjectSphere(1, 10, 10)
         
        SetObjectPosition(sphere, cellx * 1, 2, cellz * 1)
        SetObjectColor(sphere, r, g, b, 100)
        SetObjectCastShadow(sphere, 1)
         
        id.sphere = sphere
        id.x = cellx
        id.z = cellz
         
        playerdefense.insert(id)
         
endfunction

Posted: 11th Jan 2022 7:17
Hey,

Thanks for that bit of code, my game is now much cleaner! The issue now is some code stopped working and I don't know why.

+ Code Snippet
function updateBullet()
	print ("Bullet Length " + str(bullet.length))
	if bullet.length < 1
		consoleLog(1, "Bullet Array Empty", 300, 200)
	endif
	for counter = 0 to bullet.length
		if bullet.length > 0
			consoleLog(1, "Bullet X: " + str(bullet[counter].x), 300, 200)
		else
			consoleLog(1, "Bullet Array Empty", 300, 200)
		endif
		bullet[counter].x = bullet[counter].x + 1
	next counter
endfunction

If i start the round it briefly displays "Bullet Array is empty" followed by 0. For some reason the bullet's x position isn't being updated in the array. I have attached the code for reference. I know my create Bullet function is firing because of the reason stated above, but for the life of me can't figure out why it isn't updating the bullets position.

UPDATE: I can confirm the array is being expanded. Too tired to continue, going to catch some ZZZ's 1/11/2022 2:24am

UPDATE 2: changing the line
+ Code Snippet
consoleLog(1, "Bullet X: " + str(bullet[counter].x), 300, 200)
to
+ Code Snippet
consoleLog(1, "Bullet " + str(counter) + " X: " + str(bullet[counter].x), 300, 200)
confirms the bullets are being created and the 0 refers to the newly created bullet. However, if that is the case then why don't i have a stream of bullets flying across the stream??? OK I am for sure going to catch some ZZZ's Night! 1/11/2022
Posted: 11th Jan 2022 8:26
but for the life of me can't figure out why it isn't updating the bullets position.


From what I see you are updating an array item not an object position??

Call SetObjectPosition(), maybe??
Posted: 11th Jan 2022 8:46
As I'm unable to see the entirety of your code I will just have to give you this, you should be able to use this to give you an idea where you went wrong.

+ Code Snippet
// Project: TypesExample 
// Created: 22-01-11

// show all errors

SetErrorMode(2)

// set window properties
SetWindowTitle( "TypesExample" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )



createDefense(0, 0, 255, 0, 0)
createDefense(2, 4, 0, 255, 0)
createDefense(0, 8, 0, 0, 255)


do
    Sync()
    
    updateplayerdefenses()
    updatebullets()
    clearinactivebullets()
    
loop


Global playerdefense as Unit[]
global bullets as Projectile[]


Type Unit
    sphere as integer
    x as float
    z as float
    firetimertarget as integer
    
Endtype


Type Projectile
	active as integer
	sphere as integer
    x as float
    z as float
    
endtype


function createDefense(cellx, cellz, r, g, b)
        id as Unit
        sphere as integer
        sphere = CreateObjectSphere(1, 10, 10)
        
        SetObjectPosition(sphere, cellx * 1, 2, cellz * 1)
        SetObjectColor(sphere, r, g, b, 100)
        SetObjectCastShadow(sphere, 1)
        
        id.sphere = sphere
        id.x = cellx
        id.z = cellz
        id.firetimertarget = GetMilliseconds() + 2000
        
        playerdefense.insert(id)
        
endfunction


function createbullet(cellx, cellz)
	bullet as Projectile
	sphere as integer
	
	sphere = CreateObjectSphere(0.4, 10, 10)
	
	SetObjectPosition(sphere, cellx * 1, 2, cellz * 1)
	SetObjectColor(sphere, 255, 0, 255, 100)
	SetObjectCastShadow(sphere, 1)
	
	bullet.active = 1
	bullet.sphere = sphere
	bullet.x = cellx
	bullet.z = cellz
	
	bullets.insert(bullet)
	
endfunction


function updateplayerdefenses()
	for i = 0 to playerdefense.length
		updateplayerdefense(playerdefense[i])
		
	next i
	
endfunction


function updateplayerdefense(defense ref as Unit)
	if GetMilliseconds() >= defense.firetimertarget
		createbullet(defense.x, defense.z)
		defense.firetimertarget = GetMilliseconds() + 2000
		
	endif
	
	updateplayerdefensesprite(defense)
	
endfunction


function updateplayerdefensesprite(defense ref as Unit)
	SetObjectPosition(defense.sphere, defense.x, 2, defense.z)
	
endfunction


function updatebullets()
	for i = 0 to bullets.length
		updatebullet(bullets[i])
		
	next i
	
endfunction


function updatebullet(bullet ref as Projectile)
	dec bullet.x, 0.3
	
	if bullet.x <= -10
		bullet.active = 0
		
	endif
	
	updatebulletsprite(bullet)
	
endfunction


function updatebulletsprite(bullet ref as Projectile)
	SetObjectPosition(bullet.sphere, bullet.x, 2, bullet.z)
	
endfunction


function clearinactivebullets()
	for i = 0 to bullets.length
		if bullets[i].active = 0
			DeleteObject(bullets[i].sphere)
			bullets.remove(i)
			
		endif
		
	next i
	
endfunction
Posted: 14th Jan 2022 2:31
@Olde Worlde Illusions
How did you get on?, hope you got your game working.