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 AppGameKit Corner / variable not defined in function?

Author
Message
Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 21st Jul 2017 18:32 Edited at: 21st Jul 2017 18:34
Hey guys, I am learning to create functions to tidy up my program. I have created a function that I want to execute during gameplay but now the program wont run because it says sprites haven't been defined. I would think that it wouldnt need to check for the sprite until I run the function, but I am getting an error before the game even loads.



it says variable mainchar is undefined. the sprite mainchar isn't created until you load the game and click start.
Supertino
6
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 21st Jul 2017 19:16
is mainchar define as Global? also make sure you're calling the function after you've created the sprite in question
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 22nd Jul 2017 11:06 Edited at: 22nd Jul 2017 11:08
you need to define variables as global or pass them into a function for the function to know what they are, otherwise all functions have local variables.

i.e.
Quote: "global mainchar = 0 "


or
Quote: "do
cashdrop (mainchar)
sync()
loop


function cashdrop (sprid)
//now sprid = mainchar
endfunction"


likewise to return a value from a function use () at endfunction with the value to return
so


obviously useless examples but hopefully you get the idea
life\'s one big game
spec= 2.6ghz, 1gb ram, 512mb gpu, directx 9.0c, dbpro and classic
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 22nd Jul 2017 19:52 Edited at: 26th Jul 2017 20:22
The three ways in AppGameKit of passing stuff into functions (and not use evil globals):



I created a type to hold the string, just to demonstrate the pass by reference method. That will only work with user-defined types - not the built in ones. Types are great for holding data that belong together. for instance with sprites, you may create a sprite properties type like so:



The '_t' suffix is just an old habit of mine to differentiate types from more normal arrays and variables. Using that type you can define as many sprites you'd like by creating variables and arrays of that type.

But that is not the topic here. So. In the code example, in the first function call, a value is simply passed to the function with no return. Whatever happens to that variable in that function stays in that function. Sort of like Las Vegas, only without gambling, booze and, uhm, ladies of the night?

In the second function, a value is passed in *and* passed out (aka returned). So you send it in in one state, and is returned in another. You do not need send and return the same variable - or send anything at all. You could replace it with this:



and call it in main() with foo = passNoValueWithReturn()

The possibilities are endless. Anyhow, the last is passing by reference - or by pointer in other languages. Without getting into technicalities, this is very useful for larger datasets. When passing by value, the program copies the value and pass it in. Which is fine for discrete variables. Not that great for a complex array of types. By passing by reference, there is no copying going on, it operates directly on the type where it is stored in memory instead. This is the fastest way to have a function change your data.

...well, apart from using globals. But using globals all over the place will ensure an unholy mess sooner or later. So save those for IDs of your assets and maybe one type holding a global state. Everything else, pass it around to keep control.
Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 26th Jul 2017 14:05
Ok, I see what you are saying. Thanks so much for the depthy reply! So I need to call for defined subitems and not try to call the sprite directly in the function. I have a lot of reworking to do, as I have recently learned so much from this forum. Now its just a matter of finding the time to do it! Previously, with my more simple projects, I didnt have a need for

but now that I am learning to use funtions, arrays and types somewhat properly, I can see clearly the need for proper structure and calls. The more I learn, the more I discover I don't know
One smart fellow, he felt smart. Two smart fellows, both felt smart. Three smart fellows all felt smart...
Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 26th Jul 2017 14:13
And SmallG, Thanks for making is simple enough for me to understand =D I am definitely going to give that a try.
One smart fellow, he felt smart. Two smart fellows, both felt smart. Three smart fellows all felt smart...
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 27th Jul 2017 03:44
Quote: "The more I learn, the more I discover I don't know "


The good news is, that will never change. For any of us

Login to post a reply

Server time is: 2024-04-25 01:53:48
Your offset time is: 2024-04-25 01:53:48