GameMaker How do I draw a sprite dependent on a variable?

K

komodo

Guest
I have a variable "energy" and a two sprites, "sprEnergyHalf" and "sprEnergyFull". How do I draw a sort of bar with these sprites?
i.e. If "energy" is at 2, draw one sprEnergyFull. If "energy" is at 3, draw one sprEnergyFull and one sprEnergyHalf. If "energy is at 1, draw one sprEnergyHalf. You get the point.
 

TsukaYuriko

☄️
Forum Staff
Moderator
You could use a for loop to iterate through every potential point of health, incrementing by 2 every iteration. Assuming the maximum health is 10 (so 5 full segments), you'd want to loop from 0 to 9, with an increment of 2, so 5 loop iterations.

You can then check whether (current iteration + 1) is less than energy in every iteration.

If it is, you know for sure that you have at least (i + 1) health and can draw a full segment.

If it isn't, you can then use the mod operator to figure out whether energy is even or odd.
If it's odd, draw a half segment. You can then break out of the loop as there is no more health left to draw.
If it's even, all health has already been drawn, so you can just break out of the loop.

(Edit: Logic flaw.)
 
K

komodo

Guest
This is probably a super amateur question, but in terms of syntax, how would you do any of this? Like, I genuinely don't understand anything you said, haha. I'm super new to coding, so forgive me if I don't really understand.
 
This is probably a super amateur question, but in terms of syntax, how would you do any of this? Like, I genuinely don't understand anything you said, haha. I'm super new to coding, so forgive me if I don't really understand.
In that case, you should get a programming language book (e.g. C++, Java, etc.) to learn the basics and acquire a general understanding on the logic of what to do. Otherwise you'll never get anywhere without knowing even the basics, especially since you wish to code instead of rely on drag & drop. For the above case, you'll need to read up about loop.
 

TsukaYuriko

☄️
Forum Staff
Moderator
This is probably a super amateur question, but in terms of syntax, how would you do any of this? Like, I genuinely don't understand anything you said, haha. I'm super new to coding, so forgive me if I don't really understand.
You can find an overview of GML syntax on the GML Overview page. Apart from GML syntax, the relevant subjects you would have to look up in the manual are for loops, if/else statements, the modulo operator and drawing functions (presumably for sprites). You can find these on the Language Features, Expressions and Drawing Sprites And Tiles pages.
 
Why would you tell anyone who wants to learn GML to go to read C++ or Java books. GML has very little in common with them. There is a place to learn GML and that is https://docs2.yoyogames.com/source/_build/3_scripting/3_gml_overview/index.html.
I wasn't telling him to learn GML by reading C++/Java. I was referring to the basics in general (in this case to gain understanding about loop) simply because as he stated he doesn't even understand what TsukaYuriko explained - which led me to think he has no prior exposure to programming which I might be mistaken. But the documentation as the naming itself - "gml_overview" doesn't cover in-depth, some of you might feel reading just that documentation will more than suffice - that's good. However after printing them all and spending weeks reading it, I personally don't think it is, especially for someone who started off with knowing nothing about programming (like myself) will find there are things mentioned in there that requires further looking up. In any case, if after reading that documentation link posted, he finds it more than sufficient - that's good. He probably doesn't need to go the distance as I did. :)
 
Top