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.

AppGameKit Classic Chat / How to get the sprite ID

Author
Message
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 6th Feb 2017 01:45
Hello!

Using:
RightArrow=createsprite(loadimage("rightarrow.png"))

How can i get the sprite ID number generated?

I want to use in this function and i will need the ID:

function CheckSpritePressed(Sprite as integer)
Pressed as integer
if GetPointerReleased()
if GetSpriteHitTest(Sprite, getpointerx(), getpointery())
Pressed = 1
else
Pressed = 0
endif
endif
endfunction Pressed

Thanks in advance!!
Funnell7
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 6th Feb 2017 08:36
RightArrow will be an Interger which will be storing the ID. Calling CheckSpritePressed(RightArrow) will be sufficient.

However, should you want to see the ID for a particular variable, simply do Print(RightArrow).
Using AppGameKit V2 Tier 1
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 6th Feb 2017 10:38
RightArrow must also be global, if you want to use it in another function.

global RightArrow as integer
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th Feb 2017 11:09
you should avoid global variables.
maybe here "createsprite(loadimage("rightarrow.png"))" you can use
LoadSprite because else the imageid is lost
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
MikeHart
AGK Bronze Backer
21
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 6th Feb 2017 12:23
Markus wrote: "you should avoid global variables"


Why?
Running Windows 7 Home, 64 bit, 8 GB ram, Athlon II X2 255, ATI Radeon HD 4200. Using AGK2 Tier 1.
Funnell7
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 6th Feb 2017 13:14
Quote: "RightArrow must also be global, if you want to use it in another function."


True, although in this example, RightArrow is not being used in another function so global is not necessary.

Quote: "you should avoid global variables."


Also, why?
Using AppGameKit V2 Tier 1
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 6th Feb 2017 13:26 Edited at: 6th Feb 2017 15:33
Quote: "why?"

Because it is a good advice.
Unless we want everything in a global spaghetti code
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 6th Feb 2017 14:38
Quote: "Because it is a good advice."

Not necessarily. As with all things, it has its uses. Since we don't have classes, global variables are useful in certain situations.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 6th Feb 2017 15:09
It doesn't have to be Spaghetti code. I start all my globals with a lower case "g".
It is more efficient than passing variables to every function, if they are used frequently.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th Feb 2017 16:46 Edited at: 6th Feb 2017 19:15
Quote: "why?"

by experience. its difficult to explain in english.
global variables are often used inside a function, you lost overview what arguments get in, they are shared , if you will use the function as template somewhere else
you must search the spread source code in project.
if the function write values into this global variable it can break the logic if other function call write also a value into.
if you call a function for player1 and player2 and they wrote a value into it may result in a conflict.

a real example,
we had a web developer that used global variables for project, order no., order pos. which changed after selecting comboboxes. (designed for single window long ago)
now think what happened if the user open a new window (window 2) , change the comboboxes there and he save the data in the other data collect windows (window 1)
which used global variables. yeahh the supplier collect wrong data. arrrggrgrgr.

next real example if you are using objects.
if you are using global variables for database write access,
if the function which edit data not closing this recordset maybe the database table is unwanted locked or you end up in a deadlock.
would you use a recordset inside the function scope, the object will run through a destructor process.

Quote: "It is more efficient than passing variables to every function"

as well, that is part of optimization solution.
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 7th Feb 2017 01:53
Thanks everyone. I was curious to know which number the sprite was without declaration and just figured out it starts with 100001. Global or local, I will need to declare, anyway.

Now, about global variables, i think they are valuable in case of using many files. However, the documentation states that go sub and return are not recommended anymore.

After reading all threads I just realized I'm using some global variables inside functions and yes, I agree I should change!
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 7th Feb 2017 07:25
Quote: "Not necessarily. As with all things, it has its uses. Since we don't have classes, global variables are useful in certain situations."


I'd agree with this and say (as applies to AGK) that the use of ambiguous globals should be avoided, but that globals properly identified and handled through consistent convention can be a very useful tool.

Anything can turn into spaghetti if it isn't laid out clearly or not handled responsibly and consistently.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Feb 2017 12:03
I would disagree with making a sprite number global just for sake of using it inside functions. That most definitely should be an ID passed to the function. Too many globals make a less modular design.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 7th Feb 2017 16:17 Edited at: 7th Feb 2017 16:23
Quote: "I would disagree with making a sprite number global just for sake of using it inside functions. That most definitely should be an ID passed to the function. "


Agree here as well. Really, I would use an array/list of id's and pass the array to an update function that runs through all of them in the main loop, with individual management functions to get and set individual states and data.

Quote: "
Too many globals make a less modular design."


To be honest, the only time I use a global is to expose the public data of a module as a whole, and my modules create a sort of name space by convention. I will have globals for Camera, World, Characters, Animation etc each representing a given module and containing data that needs to be accessed by a number of functions. Functions are likewise namespaced: Camera_update()

I treat these as more an integral component of the application than as a basic variable of ambiguous scope and purpose.

When I type Camera.baseData.posX I know exactly what data I am accessing, and the exact effect that changing it will have at any point in the application. Given this, I don't feel the need to have to pass this around everywhere that might need it, the module name spacing convention is enough for me to manage it responsibly and so I make it global.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 8th Feb 2017 03:48
I rebuilt all my functions removing the global vars. Feels better now

There is just one that I can't figure out how to do:


Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 8th Feb 2017 03:59
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 8th Feb 2017 04:32
Thank you, Ortu!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 8th Feb 2017 08:39
One quirk of AppGameKit is that arrays are always global.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 8th Feb 2017 09:03
Yeah, Richard_6 your original function was correct as A) Arrays are always global, and B) you explicitly declared the array as global anyway: global AllCars as Cars[1]
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 8th Feb 2017 12:55 Edited at: 8th Feb 2017 14:22
Quote: "Arrays are always global,"

i believe only with the old syntax

try this, compiler say its not declared.
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 8th Feb 2017 13:52

The point of my example is that he was looking for a way to do it without using global

Quote: "One quirk of AppGameKit is that arrays are always global."


Is this still true if the array is part of a type on a non global variable?
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 8th Feb 2017 14:52
Quote: "local a as test[0]"

You explicitly made it a local! Try it without the local prefix. (However it does demonstrate that by using the LOCAL prefix we can make an array that is not global. (I always forget about this as I don't use the local prefix, as all variables are local within their scope unless explicitly declared global.)
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 8th Feb 2017 16:07
@Mobiius
yo, so it seems not
Quote: "always"

but it should local by default inside a function.
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 8th Feb 2017 17:37
Quote: "
but it should local by default inside a function"


I'd agree that they should, but they never have been with TGC languages, one of the quirks that people have gotten used to I guess.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.

Login to post a reply

Server time is: 2024-11-24 20:40:48
Your offset time is: 2024-11-24 20:40:48