How do I use a variable as a condition?

I’m not sure I titled this correctly, but I can’t figure out why I can’t use a reference to a variable as a condition.

I’m setting up a MUD/MMO style combat, auto attack based on weapon speed, and this is just quick test, but eventually I’m going to make an equip system so I want to set the stats of the weapons into variables and set up from there.

The problem that I’m having is when I set the variable as the condition, it doesn’t work. It doesn’t change the speed or damage. I tried a few different ways which is why sword/dagger are different. However, if I set the icon (Sprite) as the condition, it DOES work, which is how I set Axe. Here’s some screenshots.

NOT WORKING

WORKING

VARIABLES

just wrapping my head around what you are doing. What is the desired function of this?

I haven’t made the actual gear system, but I was expecting to use a variable to reference the equipped weapon. From there the attack speed/damage would be referenced from the various weapons.

It’s possible I’m doing it wrong from the start, I just wanted to get the basics functional before moving on to what seemed like it would be more complicated. I have a couple videos in queue for the equipment systems, I just haven’t done them yet.

if you are tracking the equipped weapon, you could use a text variable PlayerWeapon and set its value to “Dagger”, “Sword”, etc. … i’ll mock it up


if this was your array:

Then you could refer to the damage value as this:

image

So if you changed playerweapon to “Sword”, then it would be pulling from the weapondamage.sword

That looks like what I’m using in my variables.

in your code where you set “playerWeaponList” that is a structure, but then you are setting its value to “PlayerWeaponList.Dagger” which is a child of that same structure. That’s like saying Set House to be Room inside of house.

I guess the main thing I think is confusing is that you are storing the list of possible weapons but wanting to treat it like a variable storing the currently active weapon. Just make a Text Variable “PlayerWeapon” that you update when a weapon changes.

I had kind of figured it out, but had another issue, but that looks to have fixed it. Thank you, that helped.

Related question would it make sense to turn weapons into functions?
Like Sword with all its stats would be one function and Axe would be a different one? Or just making them different external event?

Do you mean functions like external events? Or are you talking about how you organize the data within arrays?

It all depends on what works best with the way you think about things and how the different objects will interact. I’ve done multiple weapons as multiple animations with unique collision mask and stats, and i’ve also done multiple weapons each as their own object with object variables to define damage, etc.

If some are ranged and some are melee, then unique objects might make sense. if they are all melee then they can all follow the same code

I was looking at the right click menu and it had “Extract to a Function” and I was thinking of it like standard coding where you make functions to call later.

I’ve been trying to use external events to keep stuff as clean as possible.

Right now what I’m working on isn’t really going to need collision or anything as such. It’ll be more like two sprites just auto attacking (what I was working on at the moment) and using skills/abilities on cooldowns, like an old MUD or WoW. (No where near as complex as WoW, obviously) Right now, range and distance isn’t a factor, but it might be depending on how it feels.

Appreciate all your help.

1 Like

sounds cool. I reckon the “right” way to do it is whatever way matches the way you think about the code. Some projects I divide up into external events, and some i use a bunch of event groups so i can group the code into logical chunks and turn “off” the ones i’m not needing to scroll though.

You’re going in the right direction with a structure. It might be easier to use a structure of structures. You can then use a variable like CurrentWeapon to get or set the values dynamically.

Say you have these variables

You could use the variables like this.

You can setup one weapon and copy and paste copies. Then you just change the values.

The variable check might not be realistic it was just an example. You could apply the damage based on the variable or you could assign the value to a bullet object and use it.

You could use the variables to set how many bullets a weapon holds or how fast the projectile moves or the name of an object to create by name.

The main advantage is that you can use the same events regardless of the weapon. You could use Booleans to control features.

Weapons.Axe.Capacity
Is the same as
Weapons[StringVariable].Capacity
If StringVariable equaled Axe

It can be confusing if you’re new to it but it can be a huge time saver.

I was actually starting to mess with doing this already and this helps clear up a few things I was puzzling through. I like the codeless approach Gdevelop has (I did two years of coding in college 20 years ago, so I have a bit of a background) but sometimes I don’t know where to get something started at.

Thank you for the example, I appreciate it.

1 Like