Sending an object to a function
BlitzMax Forums/BlitzMax Beginners Area/Sending an object to a function
| ||
Sorry if this has been asked before. I'm trying to work with Superstrict and trying to keep my code as clean as possible and am learning OOP as I go along...... anyway, here goes..... I need to pass an object into a function to check it's X and Y fields and possibly update them. It could be one of 3 types of object, Player:PlayerType, Monster:MonsterType or Bullet:Bullettype..... they all have the X and Y fields. So how do I pass them into the function and have the function update them? Bear in mind I'm using Superstrict. To call the function would it be MyFunction(Player:PlayerType) ? But I'm not sure what to put in Function MyFunction(TempType?... ?) Of course I could just use Globals in the function, update those and then copy the values back into my object upon returning, but that seems rather messy. Is there a better way to do it? |
| ||
greetings :) you are on the right track. all your types would need to extend from a base type that has the common functionality. the function parameter would be of that base type. you can use any objects of a child type as the base (so anything extending the base type could be used as the base type). an example would be:Type TPositionableObject Field x:Int Field y:Int EndType Type TMonsterType Extends TPositionalObject EndType Type TPlayerType Extends TPositionalObject EndType Type TBulletType Extends TPositionalObject EndType Function UpdateObject(game_object:TPositionalObject) If game_object.x = 0 And game_object.y = 0 Then game_object.x = 50 game_object.y = 50 EndIf EndFunction Local monster:TMonsterType = New TMonsterType Local player:TPlayerType = New TPlayerType Local bullet:TBulletType = New TBulletType UpdateObject(monster) UpdateObject(player) UpdateObject(bullet) hope that gets you going :) |
| ||
Thanks Gman. Luckily I'd already extended them all from a base type which does include the X and Y coords, so I'd got it right so far. I wasn't aware that I could pass the base type, so that's what I needed to know. Thanks also for the example, it's much easier to see these things written down and then translate them into my own code :) |
| ||
Type TPositionableObject 'this list will hold all positionable objects in the game world, for ease of updating. so you can have multiple monsters, bullets, even players! Global all_objects:TList = New TList Field x:Int Field y:Int Method New() 'this is automatically ran for you when a new object is created all_objects.AddLast(Self) End Method Method remove() 'there is a more efficent way, that won't need you to search the entire list, but i'll work on that tomorrow after sleep! all_objects.Remove(Self) End Method Function all_update() 'go through all the objects in the list, and update them For Local obj:TPositionableObject = EachIn all_objects obj.common_update() Next End Function Method common_update() 'do all checks common to every object here If x = 0 and y = 0 Then x = 50 y = 50 End If 'the update method will be unique to each kind of extended type! this is where OOP magic comes to play ;) update() End Method 'Abstract means we're not defining the method here, it will be defined in extended objects Method update() Abstract End Type Type TBullet Extends TPositionalObject 'possibility?? :- Field target:TPositionalObject Method update() 'code here to aim and move the bullet, and check for hit End Method End Type Type TLiving Extends TPositionalObject Field health Method update() 'update procedures unique to living things only! If health <= 0 Then remove() 'calls the method to remove from list End If End Method Method living_update() Abstract End Type Type TPlayer Extends TLiving Field score 'etc Method living_update() '..... player update code End Method End Type Type TMonster Extends TLiving Method living_update() '..... monster update code End Method End Type I forgot to specify the Ints, oh well I use Strict ;) It's just a skeleton, to give you a general idea of the layout, but hopefully should give you some ideas. |
| ||
As promised, I've devised a more efficient way of removing objects from lists. You can find it in my reply to this post: http://www.blitzbasic.com/Community/posts.php?topic=87018 |