Returning Entities assigned to Type Fields

Blitz3D Forums/Blitz3D Beginners Area/Returning Entities assigned to Type Fields

Shredster7(Posted 2013) [#1]
Hello there!
I'm getting started on the code for my biggest game yet, and (big surprise) I've run into an issue with the earliest stages of the code.

What I want to do is to make a function (CREATE_DUMMY) that creates a new instance of a Type called "CHAR_DUMMY" and assigns a sprite to the CHAR_DUMMY instance's "mesh" field via CopyEntity.

However, it seems that the CREATE_DUMMY function won't return any values assigned to the CHAR_DUMMY instance I've created.

Here's the part of the code that's giving me trouble:

;;;;;;;;;;;;;;;;;;;;
;LOAD SPRITE SHEETS;
;;;;;;;;;;;;;;;;;;;;
Global sprite_generic = LoadTexture("stand.png", 2)

.CREATE_SPRITE
Function CREATE_SPRITE(name, sprite_sheet)
	character = CreateSprite()
	EntityTexture character, sprite_generic
	NameEntity character, name
	Return name
End Function

.X

.CHAR_DUMMY
;;;;;;;;;;;;;;;;;;;
;"DUMMY" CHARACTER;
;;;;;;;;;;;;;;;;;;;

;Type variables
Type CHAR_DUMMY
	Field ID%
	Field damage
	Field lives
	Field xpos#
	Field ypos#
	Field direction
	Field airborne
	Field attacking
	Field stunned
	Field velx#
	Field vely#
	Field shield
	Field mesh
End Type

;Creation function
Function CREATE_DUMMY.CHAR_DUMMY(player, dummy1)

	Local player1.CHAR_DUMMY = New CHAR_DUMMY
	
	player1\lives = 5
	player1\direction = DIR_LEFT
	player1\mesh = CopyEntity(dummy1)
	
	Return player1.CHAR_DUMMY
End Function

	
;Update function
Function UPDATE_DUMMY.CHAR_DUMMY(character.CHAR_DUMMY)

	If KeyDown(KEY_LEFT)
		character\xpos = character\xpos - 2
	ElseIf KeyDown(KEY_RIGHT)
		character\xpos = character\xpos + 2
	EndIf
	
	PositionEntity character\mesh, character\xpos, character\ypos, 0
	
End Function

CREATE_SPRITE(dummy1, 0)
CREATE_DUMMY(one, dummy1)

While Not KeyDown(KEY_ESC)
	WaitTimer FRAMERATE

	UPDATE_DUMMY(player1.CHAR_DUMMY)
	
	UpdateWorld
	RenderWorld
	Flip
	
Wend
	
End


Thanks for your time!


Stevie G(Posted 2013) [#2]
To be honest there are alot of issues with your code.

You aren't actually assigning a variable to what the CREATE_DUMMY function is returning. You really need to understand the variable types and use better naming conventions before you go much further or you'll be in a world of pain later.

For example, the function below requires a name parameter which presumably should be a string ( $ ) in order for the entity to then be named. As it stands you pass in dummy1 as the name, which is an integer converts to 0. You return name so return 0. You should be returning the character variable as this is the entity. Even then, you don't actually have a variable to store what is returned!!

Function CREATE_SPRITE(name, sprite_sheet)
	character = CreateSprite()
	EntityTexture character, sprite_generic
	NameEntity character, name
	Return name
End Function


If you are going to be reusing the sprite in each dummy_type created then it's best to create a global reference to the sprite, hide it and just copyentity the global version.

Anywho, I made some modification which may make more sense to you ..

;;;;;;;;;;;;;;;;;;;;
;LOAD SPRITE SHEETS;
;;;;;;;;;;;;;;;;;;;;
Global TEXTURE = LoadTexture("stand.png", 2)

;create a global version of the sprite for re-use
Global SPRITE = CREATE_SPRITE( "This is the Name", 0 )

;Type variables
Type CharType
	Field ID%
	Field damage
	Field lives
	Field xpos#
	Field ypos#
	Field direction
	Field airborne
	Field attacking
	Field stunned
	Field velx#
	Field vely#
	Field shield
	Field mesh
End Type

;create a dummy and assign it to a global variable Player1 for later reference
Global Player1.CharType = CREATE_CHARACTER()

While Not KeyDown( KEY_ESC )

	WaitTimer FRAMERATE

	UPDATE_CHARACTER( Player1 )
	
	UpdateWorld
	RenderWorld
	Flip
	
Wend
	
End

;============================================================
;============================================================
;============================================================

Function CREATE_SPRITE( Name$, sprite_sheet = 0 )

	Local sprite = CreateSprite()
	
	EntityTexture sprite, TEXTURE
	NameEntity sprite, Name
	HideEntity sprite

	Return sprite

End Function

;============================================================
;============================================================
;============================================================

;Creation function
Function CREATE_CHARACTER.CharType( Player = 0, Dummy1 = 0 )

	Local this.CharType = New CharType
	
	this\lives = 5
	this\direction = DIR_LEFT
	this\mesh = CopyEntity( SPRITE )
	
	Return this

End Function

;============================================================
;============================================================
;============================================================
	
;Update function
Function UPDATE_CHARACTER( this.CharType )

	If KeyDown(KEY_LEFT)
		this\xpos = this\xpos - 2
	ElseIf KeyDown(KEY_RIGHT)
		this\xpos = this\xpos + 2
	EndIf
	
	PositionEntity this\mesh, this\xpos, this\ypos, 0
	
End Function



Shredster7(Posted 2013) [#3]
I've been playing around with that code you posted, and I have a much better idea of what I need to do now. I'm also going to have to study up on other suffixes (or whatever they're called) other than just '#' and '$'.

But anyways, thanks so much for helping me out! I'll remember to list you in the opening credits for the game. :)