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.

DarkBASIC Professional Discussion / Help getting info about a sprite with just its sprite number

Author
Message
Bago
18
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 10th Aug 2012 03:44 Edited at: 10th Aug 2012 03:46
Since keeping track of many game objects seems too much code is needed to tell a program manually what sprite has what statistics, it would be easier to just use its sprite number and retrive infomation with it.

My idea was to use a type like "objectinformation" and from that have Object1, Object2 etc matching sprite number to the object(insert sprite number).

Using "CollidedSpriteNo = SPRITE COLLISION(2,0)" sprite 2 being the player will have "CollidedSpriteNo" equal the other sprite that could be interacted with. Then from this I combine "Object" and the "CollidedSpriteNo" together by using "InteractingObject = "Object" + STR$(CollidedSpriteNo)". This will give me the types name and would be able to use it collect any info stored in the type. So to use the info in the type I use "IF InteractingObject.Name = Rock" I should be able to know the name of the object but all I get is "could not find field Name in type string at line blah".

So there goes a huge hole in my idea. Attached is a simple demo using this method but I commented out lines 80, 81, 83, and 84 which would be the retreival of information from the type that just gives errors.

Am I going about this the wrong way or is it just not possible?
Is there a better way of doing this type of simplified data retreival?

Any help is appreciated.
Stab in the Dark software
Valued Member
23
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 10th Aug 2012 06:03 Edited at: 10th Aug 2012 06:05
You have not declared "InteractingObject" variable as type "ResourceObjectType". So it does not contain the member variable "Name".

Also Strings must be in quotes example below




[img][/img]


WindowsXP SP3,Vista,Windows 7 SP1, DBpro v7.7RC7
Stab In The Dark Editor
The coffee is lovely dark and deep,and I have code to write before I sleep.
Bago
18
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 10th Aug 2012 07:14 Edited at: 10th Aug 2012 10:25
I thought that anytime a varible is used it would use its stored data and not it's name. e.g. IF InteractingObject.Name = "Rock", Lets say InteractingObject contains Object3 which is a type and has the .Name member varible so the line would be read as IF Object3.Name = "Rock".

As for the String errors you found, thanks that helps solve some issues.


Edit: Changing things around making InteractingObject = "Object" + STR$(CollidedSpriteNo) + ".Name" I thought it might work better but now get error Types 'FS@HandlePlayer@InteractingObject' and '$$7' are incompatable, when using it with IF InteractingObject = "Rock".
Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 10th Aug 2012 12:11 Edited at: 10th Aug 2012 12:14
If I'm understanding correctly, unfortunately what you are trying to do there will never work. 'InteractingObject' is just a string. Just because that string is the same as the name of another variable of another type you've created is irrelevant; it still only has the properties of a string. What you're trying to do would involve using a function to look for a pointer to the other variable based on its name. The best way to handle all this is going to be to have an array along with your objects so you can just refer to an item in that array to get any additional information about your object.


Previously TEH_CODERER.
Bago
18
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 10th Aug 2012 12:19 Edited at: 10th Aug 2012 14:49
I see so strings can't be used like that.

Looks like I need to learn how to use arrays, I've avoided it so far as it looks simple but confusing. This will take me some time to come up with a working demo.

Thanks this points me closer towards my inteded goal.
Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 10th Aug 2012 14:38
Don't worry; you'll get the hang of them quickly and they are pretty much a necessity for all projects. Here is a quick example:



Just hover the mouse over an object and it will show the object name. If you need any of it explained then just let me know.


Previously TEH_CODERER.
Bago
18
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 10th Aug 2012 14:48
Thats kind of funny you post as I had just finished my edit, oh well I'll remove the edit to here.

Ok I reworked the code to work with arrays and for simplicity used several one dimential arrays to replace the type for storing the objects information.

I'll also ditch the way of describing using "" it might be easier to read.

So using DIM Name$(4) with Name$(3) = "Tree" and still using CollidedSpriteNo = SPRITE COLLISION(2,0) to point to the correct spot in the array. It all works until I want to compair what is stored in the array with IF Name$(CollidedSpriteNo) = "Tree" and it does nothing, in the attached example its supposed to change a sprite's appearence. Now I'm stuck again.
Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 10th Aug 2012 15:29
At a quick glance it looks as though your main issue is that the variable 'CollidedSpriteNo' is not global, so it only has a value in your main loop and not when you try to access it in your function. Declare it towards the start of your program as 'global CollidedSpriteNo as integer'. I made this global and it all seemed to work. You should make a habit of using 'global' for all variables that will be used in multiple scopes (i.e. used in the main loop and in functions or even just in more than one function) so probably the 'player' variable should use it too 'Name$', 'ImageNumber' etc. (though I believe array default to global but its nice to keep your syntax consistent). Also, I think it would be cleaner to stick with a single array with the user defined type but that is probably more preference than anything.


Previously TEH_CODERER.
Bago
18
Years of Service
User Offline
Joined: 7th Jan 2008
Location:
Posted: 10th Aug 2012 15:47 Edited at: 10th Aug 2012 15:48
Yes thankyou it was because it wasn't global. It all works now thankyou. Now I have the way to do what I was after yay. Since I'm only staring with arrays I will try out the type with array. It's just to get it to work once while remaining as simple as possible to then make it more neater/advanced.
Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 10th Aug 2012 15:55
No problem; I'm glad that you're well on the way now. If you take a look at my example, it shows using the user defined type in an array as well as using a dynamically sized array so that you can add more objects dynamically without having to know before hand what size of array to dimension.


Previously TEH_CODERER.

Login to post a reply

Server time is: 2026-07-23 00:45:08
Your offset time is: 2026-07-23 00:45:08