Function help

Blitz3D Forums/Blitz3D Beginners Area/Function help

Nike(Posted 2010) [#1]
Hello, for my game that I am building, I have lots of different kinds of monsters with there own pictures and locations, and I made a simple ai movement with lots a Rand's, and it works for one monster, but only one. The ai is in a function, and I thought that if I made the function with q%'s and w%'s instead of the ex% and ey% the the rat uses, then when I call the function, I could say something like aimovement(ex%,ey%). Once I made everything, it didnt work. Ive tried everything I know, so I decided to post on here. Here some of the code im useing for this part:

global exsub%,eysub%,ran1%,ran2%ex%,ey%,ox%,oy%

setbuffer backbuffer()
;the loop
while not keyhit(1)

     DrawImage rat,ex%,ey%
     DrawImage ogre,ox%,oy%
     aimovement(ex%,ey%)
     aimovement(ex%,ey%)

     flip 

wend 


Function aimovement(exsub%,eysub%)
	posibility% = Rand(1,500)
	If posibility% > 490
		ran1% = Rand (1,100)
		If ran1% < 50 Then exsub% = exsub% + Rand (0,4)
		If ran1% > 50 Then exsub% = exsub% - Rand (0,4)
		ran2% = Rand (1,100)
		If ran2% < 50 Then eysub% = eysub% + Rand (0,4)
		If ran2% > 50 Then eysub% = eysub% - Rand (0,4)
	EndIf 
End Function 



what am I doing wrong?


stanrol(Posted 2010) [#2]
exsub% isn't a pointer or reference to the global.


Nike(Posted 2010) [#3]
how would I make that function universial so that I would just have to have to say aimovement() with the monsters x and y in it?


Yasha(Posted 2010) [#4]
Are you familiar with the concept of lexical scope? If not, Wikipedia can give you a start...

Two things to consider. One, Blitz3D doesn't have any support for stack pointers, so you can't do some of the things you could in other languages, such as say aimovement(x,y) and have x and y in the current scope change as a result of the function call (unless they're global, but that's a second issue). The values in x and y at that point is copied into the second pair of variables declared in the function's argument list, and then there is no further connection to the original variables.

Global variables also exist, and are visible from every scope, but there's only one of them and they persist throughout the program. So unless you set them separately in advance of every call to aimovement(), they wouldn't be able to refer to more than one monster. Don't go down this path - it's horrifically inelegant and leads to all kinds of ugly, buggy code. Alternatively, you could pass the values in as parameters and have a "dedicated" pair of globals to set from inside, to act as return values, but this is also pretty messy.

Local variables will also "override" globals if there's a conflict: in the version of aimovement above, the globals aren't doing anything as the local declaration in the arglist takes priority.

The best way to get a value back from a function is using Return, but this can only hold a single variable so it can't do what you're asking above. One way around this might be to create an object capable of holding more than one variable, and return that; or you could try rearranging your code so you don't need a function to return two variables at once (there are several ways you could do this and achieve what you need).

Blitz3D does also supply one way to mutate local variables in place, but you need to use a local array for that. You can pass the array to the function and the elements will be the same ones from the previous frame - but this is quite advanced and you'll probably benefit more from trying to redesign the function.

Anyway, there are a couple of solutions to your problem (as well as "try doing it another way!") so I couldn't tell you which is the best way.


Serpent(Posted 2010) [#5]
If you pass a type containing the parameters (i.e. 'x' and 'y'), modifying the type in the function will affect it outside of the function (I think, at least). If you're already using types for your monsters, you could pass these types to the function and won't have to worry about returning values as I think types are passed as pointers to functions.