Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Internal variables for objects (DBPro)

Author
Message
Baiame
18
Years of Service
User Offline
Joined: 8th May 2006
Location:
Posted: 3rd Jun 2006 15:43
Say I wanted to have multiple enemies, each with a certain amount of health. How would I do this? I've done things with the "FOR" command before, but never thought of a way of referencing a variable based on the object's number. I suspect it may have something to do with an array, but I couldn't find much DBP-related info on these. Any help would be appreciated.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 3rd Jun 2006 18:23 Edited at: 3rd Jun 2006 18:25
Make a User Defined Type wich holds the different internal fields (variables) like Health etc etc. Then make an array of the type to have more than one (like many enemies). Then access the individual elements of the enemies array based on their index (MyEnemies(7)), then access that one's internal fields (MyEnemies(7).Health)

follow?

Baiame
18
Years of Service
User Offline
Joined: 8th May 2006
Location:
Posted: 3rd Jun 2006 21:17
Ah, I understand you in theory, but I can't seem to use the types. Every time I do I get a message upon compiling saying that the "variable 'MyType' does not exist at line X", as in this completely stupid program:

type enemy
health as integer
endtype

do
for a=1 to 100
enemy(a).health=1
next a
print a
loop

Sorry that it's not "code" markers, I'm told noobs can't use them. What am I doing wrong? Cheers.
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 3rd Jun 2006 21:38
usage1:

usage2:
Baiame
18
Years of Service
User Offline
Joined: 8th May 2006
Location:
Posted: 4th Jun 2006 00:44
Ah, I need to use an array. Thanks for your help, CattleRustler and CuCuMBeR.
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 4th Jun 2006 02:04
Incase you didnt realize, the problem was you weren't assigning the type to anything. If you just wanted to assign a type to a variable, you'd do this:



That makes Player have the Test type statistics, allowing you to use Player.Health# and Player.Name$. The advantage of using an array is you can store all of the enemies in one, so Enemy(1).health# would be different from Enemy(2).health#. Much quicker, and since the ID of each enemy is a number, it makes life easy when using loops.

Baiame
18
Years of Service
User Offline
Joined: 8th May 2006
Location:
Posted: 4th Jun 2006 03:41
Thanks RUCCUS, but I was aware of that.

Another question: how do I start the array from something other than 0? For a reason I'm not aware of, it is required that the upper bound of "Enemies" array is the same as that of the "enemy" loop later in the program. Why must a global object identifier be the same as an array entry? Thanks.
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 4th Jun 2006 13:05 Edited at: 4th Jun 2006 13:06
if you do this:

then your dim should be like this:
dim enemy(80) As enemyProperties, which means a greater memory reserve for the dimension, not so important in this case but will be if your game develops further.
so to your problem, if you have objects from 10 to 80 in your program that you want to save in an array, do this:
dim enemy(70) as characterproperties
i = 0
for objects = 10 to 80
inc i
enemy(i).objectnumber = objects
next objects
this will make so to objects numbers from 10 to 80 is saved in an array
of enemy(70) which is equal to the objects count.

RUCCUS:

if you set helth as a float, you dont need the #
and for name as string, you dont need the $, do it like this:
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 4th Jun 2006 16:15
Ah ok, wasnt aware of that, thanks CuCuMBeR.

Just one comment on your code, you can optimize it a bit:



No need for the i variable.

CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 4th Jun 2006 19:02
cool beans

Baiame
18
Years of Service
User Offline
Joined: 8th May 2006
Location:
Posted: 4th Jun 2006 21:08
Will doing that actually save the memory it would take to track the variables in "characterproperties" from object numbers 1-10? Can you please explain it?
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Jun 2006 21:54
I'd make health a word rather than float, don't really need decimal places or negative numbers, and I doubt health needs to reach beyond 65k.

"Using Unix is the computing equivalent of listening only to music by David Cassidy" - Rob Pike
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 4th Jun 2006 23:19 Edited at: 4th Jun 2006 23:20
I'd make health a byte rather than word, I doubt health needs to reach beyond 255 either
Baiame
18
Years of Service
User Offline
Joined: 8th May 2006
Location:
Posted: 4th Jun 2006 23:57
Oh, nevermind, I get it now.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Jun 2006 21:45
depends on the game. I got over 1000 in Diablo

"Using Unix is the computing equivalent of listening only to music by David Cassidy" - Rob Pike
X Trade
20
Years of Service
User Offline
Joined: 19th Feb 2004
Location: near bristol, UK
Posted: 7th Jun 2006 15:00 Edited at: 7th Jun 2006 15:02
if you were really worried about memory usage etc, then you would not use the object number as the index. you would use something else. for example

(not tested, im just writing off the top of my head here)




edit:
ok, tested, fixed

www.AoFP.co.uk

Login to post a reply

Server time is: 2024-09-25 01:21:05
Your offset time is: 2024-09-25 01:21:05