UDT Help

Game development specific discussions.
Post Reply
Chronus001
Posts: 2
Joined: Oct 28, 2015 23:32

UDT Help

Post by Chronus001 »

I am writing a text based game, and I am trying to use UDTs to control what text options appear based on the value of the parameters created from my UDTs. I have created two different Types. A Player type and a NPC type. They look like this:

Code: Select all

Type Player                  
     Str as Integer             
     Int as Integer              
    Chr as Integer            
     Rel as Integer             
End Type                             

Type Npc
Str as Integer              
Int as Integer              
Chr as Integer            
Rel as Integer
End Type
So, I create my Player Character with this sub:

Code: Select all

Public Sub Player_Creation
    Dim MainChar as Player
    Randomize
    MainChar.Str = Int(Rnd*4)+1
    MainChar.Int = Int(Rnd*4)+1
    MainChar.Chr = Int(Rnd*4)+1
    MainChar.Rel = Int(Rnd*4)+1
    Print "These are your stats:"
    Print "Strength:" + " " + Str(MainChar.Str)
    Print "Intelligence:" + " " + Str(MainChar.Int)
    Print "Charisma:" + " " + Str(MainChar.Chr)
    Print "Relationship:" + " " + Str(MainChar.Rel)
End Sub
This works within the module but if I try to Print the stats outside the module then it gives me an error saying that I haven't Dim'd MainChar.Str as a variable. However that should happen when I call the module. It won't let me compile the code because of this. The error only goes away when I dim MainChar as Player inside the sub that I reference the parameters, but this only creates a new player type and doesn't show the correct values from the original.

I want the user to be able to reference these values so that they can choose options that increase the values to be able to access special text options in the game.

I want to make a sub like this:

Code: Select all

Sub PlayerStats
 Print "These are your stats:"
    Print "Strength:" + " " + Str(MainChar.Str)
    Print "Intelligence:" + " " + Str(MainChar.Int)
    Print "Charisma:" + " " + Str(MainChar.Chr)
    Print "Relationship:" + " " + Str(MainChar.Rel)
    Sleep
End Sub
This sub would reference the previously created player type.

I want to know how I can do this.

I am aware that I have to Declare all subs and Dim all variables before use in my code. Let's assume I have already done this.

I am trying to teach myself how to code in BASIC. So please try to keep your answers simple, or at least try to explain in small chunks of code so I can understand better.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT Help

Post by fxm »

Why two differents types ('Player' and 'Npc') with exactly the same fields?

When a variable or object (as your 'MainChar') is declared (with 'Dim') inside a procedure (as your Sub ' Player_Creation'):
- It is temporary and local to procedure.
- You cannot used it from outside this procedure.
Otherwise you must declare it at the main module level.

The initialization phase of player can be inserted inside a 'constructor' of 'Player', so that this initialization phase is automatically called when any player is declared.

The procedure to print the stats can be defined as a member procedure of 'Player'.

In my following proposal, I lengthened the field names of 'Player' because they were the same than existing keywords, no longer compatible with Types containing member procedures:

Code: Select all

Type Player
  Stre as Integer
  Inte as Integer
  Char as Integer
  Rela as Integer
  Declare Constructor ()
  Declare Sub printStats ()
End Type

Constructor Player ()
  Randomize
  This.Stre = Int(Rnd*4)+1
  This.Inte = Int(Rnd*4)+1
  This.Char = Int(Rnd*4)+1
  This.Rela = Int(Rnd*4)+1
End Constructor

Sub Player.printStats ()
  Print "These are your stats:"
  Print "Strength:" + " " + Str(This.Stre)
  Print "Intelligence:" + " " + Str(This.Inte)
  Print "Charisma:" + " " + Str(This.Char)
  Print "Relationship:" + " " + Str(This.Rela)
End Sub


Dim MainChar As Player
MainChar.printStats

Sleep

Code: Select all

These are your stats:
Strength: 4
Intelligence: 2
Charisma: 2
Relationship: 3
Drago
Posts: 116
Joined: Aug 10, 2005 13:15

Re: UDT Help

Post by Drago »

and to be extended for the NPC:

Code: Select all

enum PTyp
	Player = 1
	Computer = 2
end enum

Type Entity
	Stre	as Integer
	Inte	as Integer
	Char	as Integer
	Rela	as Integer
	Name	as String
	isWhat	as PTyp
	declare constructor ()
	declare constructor (What as PTyp)
	declare sub entityStats ()
End Type

constructor entity (What as pTyp)

	select case What
		case Computer
			this.constructor()
		case else
			Randomize
			this.Name = "Me"
			this.isWhat = Player
			this.Stre = Int(Rnd*4)+1
			this.Inte = Int(Rnd*4)+1
			this.Char = Int(Rnd*4)+1
			this.Rela = Int(Rnd*4)+1
	end select
End constructor

constructor Entity
	static as integer NumOfEntity

	Randomize
	this.Name = "NPC " & str(NumOfEntity)
	this.isWhat = Computer
	this.Stre = Int(Rnd*4)+1
	this.Inte = Int(Rnd*4)+1
	this.Char = Int(Rnd*4)+1
	this.Rela = Int(Rnd*4)+1
	
	NumOfEntity += 1
End constructor

Sub Entity.EntityStats ()
	print "Your Name: " & this.Name & " <-> You are a ";
	select case this.isWhat
		case 1
			Print "Player"
		case 2
			Print "NPC"
		case else
			Print "Unknown"
	end select
	
	Print "These are your stats:"
	Print "Strength:" + " " + Str(this.Stre)
	Print "Intelligence:" + " " + Str(this.Inte)
	Print "Charisma:" + " " + Str(this.Char)
	Print "Relationship:" + " " + Str(this.Rela)
	print "-----------------------------------"
End Sub

'------------------------------------------------


Dim as entity	MainChar = Player
dim as entity	NPC(5)

MainChar.entityStats

for x as integer = 1 to 5
	NPC(x).EntityStats
next x

sleep

Code: Select all

Your Name: Me <-> You are a Player
These are your stats:
Strength: 2
Intelligence: 1
Charisma: 1
Relationship: 1
-----------------------------------
Your Name: NPC 1 <-> You are a NPC
These are your stats:
Strength: 2
Intelligence: 1
Charisma: 1
Relationship: 2
-----------------------------------
...
Grüße
Rainer
Chronus001
Posts: 2
Joined: Oct 28, 2015 23:32

Re: UDT Help

Post by Chronus001 »

Thanks Everyone for all the help. I think I understand what I need to do.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT Help

Post by fxm »

We can also program as this:

Code: Select all

enum PTyp
    Player = 1
    Computer = 2
end enum

Type Entity
    Stre    as Integer = Int(Rnd*4)+1
    Inte    as Integer = Int(Rnd*4)+1
    Char    as Integer = Int(Rnd*4)+1
    Rela    as Integer = Int(Rnd*4)+1
    Name    as String
    isWhat  as PTyp
    declare constructor (byref What as PTyp = Computer)
    declare sub entityStats ()
End Type

constructor entity (byref What as pTyp = Computer)
    select case What
    case Computer
        static as integer NumOfEntity
        NumOfEntity += 1
        this.Name = "NPC " & str(NumOfEntity)
    case Player
        this.Name = "Me"
    end select
    this.isWhat = What
    Randomize
End constructor

Sub Entity.EntityStats ()
    print "Your Name: " & this.Name & " <-> You are a ";
    select case this.isWhat
    case Player
        Print "Player"
    case Computer
        Print "NPC"
    case else
        Print "Unknown"
    end select
    Print "These are your stats:"
    Print "Strength: " & Str(this.Stre)
    Print "Intelligence: " & Str(this.Inte)
    Print "Charisma: " & Str(this.Char)
    Print "Relationship: " & Str(this.Rela)
    print "-----------------------------------"
End Sub

'------------------------------------------------


Dim as entity   MainChar = Player
dim as entity   NPC(1 To 5)

MainChar.entityStats

for x as integer = 1 to 5
    NPC(x).EntityStats
next x

sleep
[edit]
- After have modify the constructor parameter to make it optionnel, I had forgotten to remove the previous default constructor.
- Some detail modifications.
Post Reply